-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (39 loc) · 1.7 KB
/
app.py
File metadata and controls
47 lines (39 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import flask
from flask import request,jsonify,make_response
import requests
from googlesearch import search
app = flask.Flask(__name__)
app.config["DEBUG"] = True
def finder(q):
for site in search(q):
if 'geeksforgeeks' in site:
source = requests.get(site).text
try:
lines = source.split('\n')
for line in lines:
if '..' in q:
q = q.split('..')[0]
if q.replace('?','') in line:
answer_option = line.split('Answer:</b> <b>')[1].split('</b>')[0]
answer = line.split(answer_option)[1].split('<br>')[0].replace('</b>','')
answer = answer.replace('<sup>','').replace('</sup>','')
except:
locate_q = source.split(q.replace('?',''))[1]
answer_option = locate_q.split('<p>Answer ')[1].split('</p><br>')[0]
answer = locate_q.split('<p>Answer ')[0].split(answer_option)[1].split('<br>')[0]
return answer
@app.route('/', methods=['GET'])
def home():
try:
q = request.args.get('question')
answer = finder(q)
response = make_response(jsonify( {"answer": answer.replace(' ','',1)}),200)
except:
response = make_response(jsonify( {"AppName":"Quick Search Advanced","Version":"1.0.2"}),200)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Content-Type"]= "application/json"
response.headers["Access-Control-Allow-Origin"] = "*"
return response
if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port=5000)