266 schedule = [] |
266 schedule = [] |
267 try: |
267 try: |
268 bus_stop = bus_stops[reference] |
268 bus_stop = bus_stops[reference] |
269 except KeyError: |
269 except KeyError: |
270 abort(404) |
270 abort(404) |
271 for i, schedule_entry in enumerate(bus_stop.schedule_for_day(today(), arrivals = False)[:6]): |
271 for i, schedule_entry in enumerate(bus_stop.schedule_for_day(today(), arrivals = False)): |
272 schedule.append({ |
272 schedule.append({ |
273 'time_data': schedule_entry['time'], |
273 'time_data': schedule_entry['time'], |
274 'time': time_representation(schedule_entry['time']), |
274 'time': time_representation(schedule_entry['time']), |
275 'route': schedule_entry['trip'].route.reference, |
275 'route': schedule_entry['trip'].route.reference, |
276 'sign': long_form_sign(schedule_entry), |
276 'sign': long_form_sign(schedule_entry), |
279 'imminent': imminent(schedule_entry), |
279 'imminent': imminent(schedule_entry), |
280 'index': i, |
280 'index': i, |
281 }) |
281 }) |
282 from pprint import pprint |
282 from pprint import pprint |
283 pprint(schedule) |
283 pprint(schedule) |
|
284 def destination_key(schedule_entry): |
|
285 sign = schedule_entry['sign'] |
|
286 return (sign['destination'],) + tuple(sign['via']) |
|
287 def filter_schedule(schedule, *, key): |
|
288 used = set() |
|
289 for schedule_entry in schedule: |
|
290 key_value = key(schedule_entry) |
|
291 if key_value not in used: |
|
292 used.add(key_value) |
|
293 yield schedule_entry |
|
294 schedule = list(filter_schedule(schedule, key = destination_key))[:6] |
284 if schedule: |
295 if schedule: |
285 num_imminent_leaves = max(1, len([schedule_entry for schedule_entry in schedule if schedule_entry['time_data'] - schedule[0]['time_data'] < timedelta(minutes = 3)])) |
296 num_imminent_leaves = max(1, len([schedule_entry for schedule_entry in schedule if schedule_entry['time_data'] - schedule[0]['time_data'] < timedelta(minutes = 3)])) |
286 else: |
297 else: |
287 num_imminent_leaves = 1 |
298 num_imminent_leaves = 1 |
288 return render_template( |
299 return render_template( |