service.py

changeset 68
382dd85b83a2
parent 64
6dbda3b1015f
child 71
d2e19670b772
--- a/service.py	Sat Sep 23 01:23:55 2017 +0300
+++ b/service.py	Mon Sep 25 10:46:23 2017 +0300
@@ -61,23 +61,37 @@
 		else:
 			return request.accept_languages.best_match(tr.languages)
 
-def sign(schedule_entry):
+def sign(schedule_entry, long = False):
 	from math import ceil
 	trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
-	sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length)
+	sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = long)
 	if sign:
 		sign_representation = ' - '.join(tr(place, 'paikat') for place in sign if place not in suffix_regions)
 		sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions)
-		#if len(sign_representation) > 25:
-		#	k = ceil(len(sign) / 2)
-		#	sign_representation = ' - '.join(sign[:k]) + '\n' + ' - '.join(sign[k:])
 		return sign_representation
 	else:
 		return schedule_entry['trip'].schedule[-1].stop.name
 
+def long_form_sign(schedule_entry):
+	from math import ceil
+	trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
+	sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = True)
+	if sign:
+		return {
+			'destination': tr(sign[-1], 'paikat'),
+			'via': [tr(place, 'paikat') for place in sign[:-1]],
+		}
+	else:
+		return {
+			'destination': schedule_entry['trip'].schedule[-1].stop.name,
+			'via': [],
+		}
+	
+
 def imminent(schedule_entry):
 	return (schedule_entry['time'] - now()) <= timedelta(minutes = 3)
 
+
 @app.route('/stop/<reference>')
 def bus_stop_schedule(reference):
 	from buses import bus_stops
@@ -106,6 +120,104 @@
 		tr = tr,
 	)
 
+@app.route('/stop_display/<reference>')
+def stop_display(reference):
+	from buses import bus_stops
+	schedule = []
+	try:
+		bus_stop = bus_stops[reference]
+	except KeyError:
+		abort(404)
+	for i, schedule_entry in enumerate(bus_stop.schedule(max_amount = 6, arrivals = False)):
+		schedule.append({
+			'time_data': schedule_entry['time'],
+			'time': time_representation(schedule_entry['time']),
+			'route': schedule_entry['trip'].route.reference,
+			'sign': long_form_sign(schedule_entry),
+			'trip': schedule_entry['stop'].trip.name,
+			'night': is_night_time(schedule_entry['time']),
+			'imminent': imminent(schedule_entry),
+			'index': i,
+		})
+	from pprint import pprint
+	pprint(schedule)
+	if schedule:
+		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)]))
+	else:
+		num_imminent_leaves = 1
+	return render_template(
+		'stop_display.html',
+		schedule = schedule,
+		ref = bus_stop.code,
+		name = tr(bus_stop.name, 'bus-stops'),
+		link_to_map = bus_stop.location.link_to_map,
+		region = bus_stop.region,
+		location = bus_stop.location,
+		cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
+		num_imminent_leaves = num_imminent_leaves,
+		tr = tr,
+	)
+
+@app.route('/test')
+def test():
+	from buses import bus_stops
+	bus_stop = bus_stops['16']
+	schedule = [{'imminent': True,
+		'index': 0,
+		'night': False,
+		'route': '2A',
+		'sign': {'destination': 'Kohmo', 'via': ['Nummenmäki', 'Kurala']},
+		'time': '1m',
+		'trip': '00012501__3798generatedBlock'},
+		{'imminent': True,
+		'index': 1,
+		'night': False,
+		'route': '54',
+		'sign': {'destination': 'Ylioppilaskylä', 'via': []},
+		'time': '2m',
+		'trip': '00014359__5656generatedBlock'},
+		{'imminent': True,
+		'index': 2,
+		'night': False,
+		'route': '1',
+		'sign': {'destination': 'Lentoasema ✈', 'via': ['Urusvuori']},
+		'time': '3m',
+		'trip': '00010281__1281generatedBlock'},
+		{'imminent': False,
+		'index': 3,
+		'night': False,
+		'route': '56',
+		'sign': {'destination': 'Räntämäki', 'via': ['Nummenmäki', 'Halinen']},
+		'time': '8m',
+		'trip': '00014686__5983generatedBlock'},
+		{'imminent': False,
+		'index': 4,
+		'night': False,
+		'route': '42',
+		'sign': {'destination': 'Varissuo', 'via': ['Kupittaa as', 'Itäharju']},
+		'time': '18:30',
+		'trip': '00014010__5307generatedBlock'},
+		{'imminent': False,
+		'index': 5,
+		'night': False,
+		'route': '2B',
+		'sign': {'destination': 'Littoinen',
+				'via': ['Nummenmäki', 'Kurala', 'Kohmo']},
+		'time': '18:35',
+		'trip': '00012629__3926generatedBlock'}]
+	return render_template(
+		'stop_display.html',
+		schedule = schedule,
+		ref = bus_stop.code,
+		name = tr(bus_stop.name, 'bus-stops'),
+		link_to_map = bus_stop.location.link_to_map,
+		region = bus_stop.region,
+		location = bus_stop.location,
+		cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
+		num_imminent_leaves = max(1, sum(schedule_entry['imminent'] for schedule_entry in schedule)),
+		tr = tr,
+	)
+
 def time_representation(time, relative = True):
 	time_difference = time - now()
 	if relative and timedelta(minutes = -1) < time_difference < timedelta(minutes = 1):

mercurial