/
webservice.py
62 lines (47 loc) · 1.44 KB
/
webservice.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
from flask import Flask,jsonify,request
from utilities import Utilities
from datastore import DataStore
import redis
app = Flask(__name__)
@app.route('/',methods=['POST'])
def get_tasks():
u = request.form['url'].lower()
url = Utilities.get_shortened_url(u)
url_3 = Utilities.get_shortened_url(u,3)
return_only_parent = False
# If url is same as parent url, return everything just for parent
# Dont redundantly return for parent and itself
if url == url_3 or url+'/' == url_3:
return_only_parent = True
ds = DataStore()
if not return_only_parent:
all_urls = Utilities.modify_url(url)
print all_urls
# If the same url is also a parent url, return all results of parent .
# And skip individual url results
for url in all_urls:
result = ds.fetch(url)
if result == False:
print " Tried for url " + url
else:
x = {"result":result}
return jsonify(x)
# If for our exact url and its modifications , nothing got returned
outer_url = "parent::" + Utilities.get_shortened_url(url,3)
print outer_url
result = ds.fetch_all_from_parent(outer_url)
if result :
x = {"result":result}
return jsonify(x)
else:
if outer_url[-1] == '/':
result = ds.fetch_all_from_parent(outer_url[:-1])
else:
result = ds.fetch_all_from_parent(outer_url + '/')
if result :
x = {"result":result}
return jsonify(x)
# If there is still nothing to show
return 'No Response'
if __name__ == '__main__':
app.run(debug=True)