service.py

changeset 48
428cfdf64ccc
parent 42
0b53b7c70875
child 49
5b288b0ac271
equal deleted inserted replaced
47:18953691af29 48:428cfdf64ccc
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 from flask import Flask, render_template, abort, send_from_directory 2 from flask import Flask, render_template, abort, send_from_directory, redirect
3 from datetime import datetime, date, time, timedelta 3 from datetime import datetime, date, time, timedelta
4 from os import path, listdir 4 from os import path, listdir
5 from configparser import ConfigParser 5 from configparser import ConfigParser
6 import locale 6 import locale
7 7
75 return schedule_entry['trip'].schedule[-1].stop.name 75 return schedule_entry['trip'].schedule[-1].stop.name
76 76
77 def imminent(schedule_entry): 77 def imminent(schedule_entry):
78 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3) 78 return (schedule_entry['time'] - now()) <= timedelta(minutes = 3)
79 79
80 @app.route('/pysäkki/<reference>') 80 @app.route('/stop/<reference>')
81 def bus_stop_schedule(reference): 81 def bus_stop_schedule(reference):
82 from buses import bus_stops 82 from buses import bus_stops
83 schedule = [] 83 schedule = []
84 try: 84 try:
85 bus_stop = bus_stops[reference] 85 bus_stop = bus_stops[reference]
118 return time.strftime('%-a %H:%M').replace(' ', '\xa0') 118 return time.strftime('%-a %H:%M').replace(' ', '\xa0')
119 else: 119 else:
120 with activate_locale(): 120 with activate_locale():
121 return time.strftime('%-d.%-m. %H:%M').replace(' ', '\xa0') 121 return time.strftime('%-d.%-m. %H:%M').replace(' ', '\xa0')
122 122
123 @app.route('/pysäkkiryhmä/<cluster_name>') 123 @app.route('/stop_cluster/<cluster_name>')
124 def cluster_schedule(cluster_name): 124 def cluster_schedule(cluster_name):
125 from buses import bus_stops, clusters_by_name 125 from buses import bus_stops, clusters_by_name
126 schedule = [] 126 schedule = []
127 try: 127 try:
128 cluster = clusters_by_name[cluster_name] 128 cluster = clusters_by_name[cluster_name]
157 stops_in_cluster = stops_in_cluster, 157 stops_in_cluster = stops_in_cluster,
158 amount_of_stops_in_cluster = len(stops_in_cluster), 158 amount_of_stops_in_cluster = len(stops_in_cluster),
159 tr = tr, 159 tr = tr,
160 ) 160 )
161 161
162 @app.route('/ajovuoro/<trip_reference>') 162 @app.route('/trip/<trip_reference>')
163 def trip(trip_reference): 163 def trip(trip_reference):
164 from flask import request 164 from flask import request
165 from buses import all_trips 165 from buses import all_trips
166 from busroute import simplify_name 166 from busroute import simplify_name
167 try: 167 try:
203 night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time), 203 night = is_night_time(datetime.combine(today(), time()) + trip.schedule[-1].arrival_time),
204 tr = tr, 204 tr = tr,
205 length = trip.length / 1000, 205 length = trip.length / 1000,
206 ) 206 )
207 207
208 @app.route('/linja/<name>') 208 @app.route('/route/<name>')
209 def route_page(name): 209 def route_page(name):
210 from buses import routes 210 from buses import routes
211 route = routes[name.upper()] 211 route = routes[name.upper()]
212 schedule = [] 212 schedule = []
213 for trip in route.trips: 213 for trip in route.trips:
222 name = route.reference + ' ' + route.description, 222 name = route.reference + ' ' + route.description,
223 tr = tr, 223 tr = tr,
224 schedule = schedule, 224 schedule = schedule,
225 ) 225 )
226 226
227 @app.route('/')
228 def index():
229 return redirect('/stop_cluster/kauppatori')
230
231 @app.route('/pysäkki/<reference>')
232 def redirect_pysäkki(reference):
233 return redirect('/stop/' + str(reference))
234
235 @app.route('/pysäkkiryhmä/<reference>')
236 def redirect_pysäkkiryhmä(reference):
237 return redirect('/stop_cluster/' + str(reference))
238
239 @app.route('/ajovuoro/<reference>')
240 def redirect_ajovuoro(reference):
241 return redirect('/trip/' + str(reference))
242
227 @app.route('/static/<path:path>') 243 @app.route('/static/<path:path>')
228 def static_file(path): 244 def static_file(path):
229 return send_from_directory(path.join('static', path)) 245 return send_from_directory(path.join('static', path))
230 246
231 from argparse import ArgumentParser 247 from argparse import ArgumentParser

mercurial