-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·60 lines (53 loc) · 2.14 KB
/
Copy pathapp.py
File metadata and controls
executable file
·60 lines (53 loc) · 2.14 KB
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
#!/usr/bin/env python
import os, json, collections
from flask import Flask, request, render_template, Response, url_for, jsonify, send_from_directory
from werkzeug import SharedDataMiddleware
import ttc
app = Flask(__name__, template_folder='public')
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/': os.path.join(os.path.dirname(__file__), 'public')
})
@app.route('/')
def root():
return send_from_directory('public', 'index.html')
@app.route('/get', methods = ['GET'])
def getStops():
lat = request.args["lat"]
lon = request.args["lon"]
return jsonify(createResponse(lat,lon))
def createResponse(lat,lon):
predictions = json.loads(ttc.getPredictions(float(lat), float(lon)))
# uniqueStops = collections.OrderedDict()
stops = []
for stop in predictions['predictions']:
dList = []
for direction in stop['directions']:
directionDict = {}
for dirName,details in direction.iteritems():
mList = []
for prediction in details:
mList.append(prediction['minutes'])
directionDict.update({'direction': dirName, 'type': stop['routeTag'], 'times': mList})
dList.append(directionDict)
stops.append({'name': stop['stopTitle'], 'lat': stop['lat'], 'lon': stop['lon'], 'routes': dList})
# route = createRoute(stop['routeTag'], [0], stop['routeTitle'])
# stopend = createStop(stop['lat'], stop['lon'], stop['stopTitle'], dList)
# if str(stop['lat'])+str(stop['lon']) not in uniqueStops:
# uniqueStops.update({str(stop['lat'])+str(stop['lon']): stopend})
# else:
# val = uniqueStops[str(stop['lat']) + str(stop['lon'])]
# val['routes'].append(route)
# uniqueStops.update({str(stop['lat'])+str(stop['lon']): val})
# resp = []
# for k,v in uniqueStops.items():
# resp.append(v)
responseJSON = { 'stops' : stops }
return responseJSON
def createStop(lat, lon, intersection, routes):
return {'lat':lat,'lon':lon, 'name':intersection, 'routes':routes}
def createRoute(routeTag, times, direction):
return {'type':routeTag, 'times':times, 'direction':direction}
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
# print json.dumps(createResponse(43.7739, -79.41427), indent=4)