service.py

changeset 85
62e753b7d3ff
parent 79
ba854da8c424
child 87
9139a94e540c
equal deleted inserted replaced
84:30cc6a1d1903 85:62e753b7d3ff
79 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) 79 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions)
80 return sign_representation 80 return sign_representation
81 else: 81 else:
82 return schedule_entry['trip'].schedule[-1].stop.name 82 return schedule_entry['trip'].schedule[-1].stop.name
83 83
84 def long_form_sign(schedule_entry): 84 def long_form_sign(schedule_entry, long = True):
85 from math import ceil 85 from math import ceil
86 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance 86 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
87 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = True) 87 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = long)
88 if sign: 88 if sign:
89 return { 89 return {
90 'destination': tr(sign[-1], 'places'), 90 'destination': tr(sign[-1], 'places'),
91 'via': [tr(place, 'places') for place in sign[:-1]], 91 'via': [tr(place, 'places') for place in sign[:-1]],
92 } 92 }
272 abort(404) 272 abort(404)
273 return jsonify({ 273 return jsonify({
274 'abbreviation': trip_abbreviation(trip), 274 'abbreviation': trip_abbreviation(trip),
275 }) 275 })
276 276
277 def current_bus_day():
278 from datetime import date, datetime, timedelta
279 day = date.today()
280 if datetime.now().hour < 5:
281 day -= timedelta(1)
282 return day
283
277 @app.route('/stop_display/<reference>') 284 @app.route('/stop_display/<reference>')
278 def stop_display(reference): 285 def stop_display(reference):
279 from buses import bus_stops 286 from buses import bus_stops
280 schedule = [] 287 schedule = []
281 try: 288 try:
282 bus_stop = bus_stops[reference] 289 bus_stop = bus_stops[reference]
283 except KeyError: 290 except KeyError:
284 abort(404) 291 abort(404)
285 for i, schedule_entry in enumerate(bus_stop.schedule_for_day(today(), arrivals = False)): 292 for i, schedule_entry in enumerate(bus_stop.schedule_for_day(current_bus_day(), arrivals = False)):
286 schedule.append({ 293 schedule.append({
287 'time_data': schedule_entry['time'], 294 'time_data': schedule_entry['time'],
288 'time': time_representation(schedule_entry['time']), 295 'time': time_representation(schedule_entry['time']),
289 'route': schedule_entry['trip'].route.reference, 296 'route': schedule_entry['trip'].route.reference,
290 'sign': long_form_sign(schedule_entry), 297 'sign': long_form_sign(schedule_entry, long = False),
291 'trip': schedule_entry['stop'].trip.name, 298 'trip': schedule_entry['stop'].trip.name,
292 'night': is_night_time(schedule_entry['time']), 299 'night': is_night_time(schedule_entry['time']),
293 'imminent': imminent(schedule_entry), 300 'imminent': imminent(schedule_entry),
294 'index': i, 301 'index': i,
295 }) 302 })

mercurial