-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
50 lines (43 loc) · 1.54 KB
/
server.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
from json import dumps
from flask import Flask, request
from flask_cors import CORS
from suburbs import get_suburbs
from crashcount import getAllCrashesOnRoute
from traffic_data import getAllTrafficOnRoute
def defaultHandler(err):
response = err.get_response()
print('response', err, err.get_response())
response.data = dumps({
"code": err.code,
"name": "System Error",
"message": err.get_description(),
})
response.content_type = 'application/json'
return response
APP = Flask(__name__)
CORS(APP)
APP.config['TRAP_HTTP_EXCEPTIONS'] = True
APP.register_error_handler(Exception, defaultHandler)
# POST STUFF:
# JSON input
# source : string
# destination : string
@APP.route("/suburbs", methods=['POST'])
def http_get_suburbs():
data = request.get_json()
# data = {}
# data['source'] = '29 Riverview Rd, Pleasure Point NSW 2172'
# data['destination'] = 'Zeus Street Greek Broadway Sydney, 1-21 Bay Street Level 2, Broadway, NSW 2007'
suburb_data = get_suburbs(str(data['source']), str(data['destination']))
information = []
for route in suburb_data:
information.append({})
for index, route in enumerate(suburb_data):
information[index]['crashes'] = getAllCrashesOnRoute(route)
information[index]['traffic'] = getAllTrafficOnRoute(route)
print(information)
answer = [*map(lambda a: (a['crashes'] * 100000)/a['traffic'], information)]
return dumps(answer)
if __name__ == "__main__":
# http_get_suburbs()
APP.run(port=8000) # Do not edit this port