-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbordertimes.py
55 lines (40 loc) · 1.33 KB
/
bordertimes.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
from flask import Flask, request, jsonify, render_template, abort
from scraper import scrape
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False # to maintain OrderedDict order
@app.route('/')
def root():
return render_template('index.html')
@app.route('/api')
def all():
wait_times = scrape()
if not wait_times:
abort(400)
return jsonify(meta=dict(status=200, message='OK'), data=wait_times)
@app.route('/api/list')
def ports():
wait_times = scrape()
if not wait_times:
abort(400)
return jsonify(meta=dict(status=200, message='OK'),
data=sorted(list(wait_times.keys())))
@app.route('/api/<port>')
def single(port):
try:
wait_times = scrape(port)
except KeyError:
abort(404, {'message': 'Invalid `port` value.'})
if not wait_times:
abort(400)
return jsonify(meta=dict(status=200, message='OK'), data=wait_times)
@app.errorhandler(400)
@app.errorhandler(404)
def bad_request(error):
status, message = error.code, error.description['message'] or \
'Couldn\'t retrieve data.'
response = jsonify(meta=dict(status=status, message=message), data=None)
return response, error.code
if __name__ == '__main__':
import os
port = os.environ.get('PORT', 8000)
app.run(host='127.0.0.1', port=int(port), debug=True)