forked from GDGVIT/iris-web-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (140 loc) · 4.56 KB
/
main.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import wikipediaapi
import networkx as nx
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
from decouple import config
import requests
G = nx.Graph()
wiki = wikipediaapi.Wikipedia('en')
def smaller_graph(pages):
links = pages.links
for title in sorted(links.keys()):
G.add_edge(pages.title, title, weight=1)
def build_graph(pages, start_page, end_page):
links = pages.links
for title in sorted(links.keys()):
G.add_edge(pages.title, title, weight=1)
while True:
for title in sorted(links.keys()):
try:
k = nx.shortest_path(G, start_page, end_page, weight='weight')
return k
except Exception:
smaller_graph(wiki.page(title))
def sm_graph(page):
links = page.links
for title in sorted(links.keys())[0:10]:
G.add_node(title, size=20, title=(wiki.page(title)).summary[0:60])
G.add_edge(page.title, title, weight=1)
link1 = wiki.page(title).links
for t in sorted(link1.keys())[0:10]:
G.add_edges_from([(title, t)])
return G
app = Flask(__name__)
CORS(app)
@app.route("/getPath", methods=['POST'])
def shortest_path():
secret_key = config('SECRET_KEY')
token = request.headers.get('g-recaptcha-response')
remote_address = request.remote_addr
if (not token) or (not secret_key):
payload = {
"error": True,
"message": 'No ReCaptcha Response',
"code": 400
}
return make_response(jsonify(payload), 400)
url = f'https://www.google.com/recaptcha/api/siteverify?secret={secret_key}&response={token}&remoteip={remote_address}'
response = requests.get(url)
try:
res = response.json()
if 'success' in res:
if not res['success']:
payload = {
"error": True,
"message": 'No ReCaptcha Response',
"code": 400
}
return make_response(jsonify(payload), 400)
except Exception as e:
payload = {
"error": True,
"message": e,
"code": 500
}
return make_response(jsonify(payload), 500)
start_page = request.json['start']
end_page = request.json['end']
start = wiki.page(start_page)
end = wiki.page(end_page)
if (start.exists()) and (end.exists()):
payload = {
"error": False,
"graph": build_graph(start, start_page, end_page),
"message": 'A shortest path was found',
"code": 200
}
G.clear()
return make_response(jsonify(payload), 200)
else:
payload = {
"error": True,
"message": "No such page exists",
"code": 404
}
return make_response(jsonify(payload), 404)
@app.route("/explore", methods=['POST'])
def explore():
secret_key = config('SECRET_KEY')
print(secret_key)
token = request.headers.get('g-recaptcha-response')
remote_address = request.remote_addr
if (not token) or (not secret_key):
payload = {
"error": True,
"message": 'No ReCaptcha Response',
"code": 400
}
return make_response(jsonify(payload), 400)
url = f'https://www.google.com/recaptcha/api/siteverify?secret={secret_key}&response={token}&remoteip={remote_address}'
response = requests.get(url)
try:
res = response.json()
print(res)
if 'success' in res:
if not res['success']:
payload = {
"error": True,
"message": 'No ReCaptcha Response',
"code": 400
}
return make_response(jsonify(payload), 400)
except Exception as e:
payload = {
"error": True,
"message": e,
"code": 500
}
return make_response(jsonify(payload), 500)
start_page = request.json['start']
start = wiki.page(start_page)
if start.exists():
graph = sm_graph(start)
payload = {
"error": False,
"edges": list(graph.edges),
"nodes": list(graph.nodes),
"message": 'Here is the path',
"code": "200"
}
G.clear()
return make_response(jsonify(payload), 200)
else:
payload = {
"error": True,
"message": "No such page exists",
"code": 404
}
return make_response(jsonify(payload), 404)
if __name__ == '__main__':
app.run()