-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
118 lines (103 loc) · 3.71 KB
/
app.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
from flask import Flask, request, Response, redirect
from warnings import warn
from dairy_queen.theatre import Theatre
import requests
from json import dumps
app = Flask(__name__)
@app.route('/')
def home():
return """
<p>main-endpoint:
<a href='/double-dips'>
dairy-queen.herokuapp.com/double-dips?{location[, days_from_now, max_wait_mins, max_overlap_mins]}
</a>
</p>
<p>docs (apiary.io):
<a href='/docs'>
dairy-queen.herokuapp.com/docs
</a>
</p>
<p>github:
<a href='https://github.com/stevenpollack/dairy_queen'>
https://github.com/stevenpollack/dairy_queen
</a>
</p>
"""
@app.route('/docs')
def route_to_apiary():
apiary_io = 'http://docs.dairyqueen1.apiary.io/'
return (redirect(apiary_io, code=302))
@app.route('/double-dips', methods=['GET'])
def get_doubledips():
location = request.args.get('location')
days_from_now = request.args.get('days_from_now')
max_waiting_time = request.args.get('max_wait_mins')
max_overlap_time = request.args.get('max_overlap_mins')
status = None
msg = None
mimetype = 'application/json'
if location is None or not isinstance(location, str):
status = 400
msg = "'location' is mandatory and must be a string."
resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
if days_from_now is not None:
try:
days_from_now = int(days_from_now)
except Exception:
status = 400
msg = "'days_from_now' must be a base-10 integer."
resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
else:
days_from_now = 0
if max_waiting_time is not None:
try:
max_waiting_time = int(max_waiting_time)
except Exception:
status = 400
msg = "'max_waiting_time' must be a base-10 integer"
resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
else:
max_waiting_time = 45
if max_overlap_time is not None:
try:
max_overlap_time = int(max_overlap_time)
except Exception:
status = 400
msg = "'max_overlap_time' must be a base-10 integer"
resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
else:
max_overlap_time = 5
gms_url = 'http://google-movies-scraper.herokuapp.com/v2/movies'
gms_params = {
'near': location,
'date': days_from_now,
'militaryTime': True
}
# should definitely build some logic to handle response code of r...
r = requests.get(gms_url, params=gms_params)
theatres_json = r.json()
output = []
for theatre in theatres_json:
try:
tmp_theatre = Theatre(name=theatre.get('name'),
showtimes=theatre.get('showtimes'),
info=theatre.get('info'))
tmp_json = tmp_theatre.to_json(max_waiting_time=max_waiting_time,
max_overlap_time=max_overlap_time)
output.append(tmp_json)
except TypeError as e:
warn(str(e))
status = 200
resp = Response(dumps(output), status=status, mimetype=mimetype)
resp.headers['Access-Control-Allow-Origin'] = '*'
return(resp)
if (__name__ == '__main__'):
app.run(debug=True, host='0.0.0.0', port=5000)