service.py

changeset 94
e27c18f080d1
parent 93
53ee9e945673
child 98
c07a77c8a070
--- a/service.py	Thu Apr 26 16:12:23 2018 +0300
+++ b/service.py	Tue May 01 13:21:00 2018 +0300
@@ -157,7 +157,7 @@
 	except KeyError:
 		return place
 
-def trip_abbreviation(trip):
+def trip_description(trip):
 	entries = [trip.from_place]
 	old_places = None
 	starting_halt = None
@@ -177,7 +177,10 @@
 		old_places = places
 	if trip.to_place not in entries:
 		entries += [trip.to_place]
-	return trip.route.reference + ':' + '-'.join(map(place_abbreviation, entries))
+	return entries
+
+def trip_abbreviation(trip):
+	return trip.route.reference + ':' + '-'.join(map(place_abbreviation, trip_description(trip)))
 
 def split_route_ref(route_ref):
 	try:
@@ -782,6 +785,45 @@
 		schedule = schedule,
 	)
 
+@app.route('/interesting')
+def interesting():
+	from buses import all_trips, services_for_day
+	from busroute import simplify_name
+	from collections import Counter, defaultdict
+	from datetime import datetime
+	trip_counts = Counter()
+	interesting_trips = defaultdict(list)
+	for trip_reference, trip in all_trips.items():
+		if len(trip.service.dates) <= 3:
+			for date in trip.service.dates:
+				moment = trip.schedule[0].departure_datetime(date)
+				if moment > datetime.now():
+					interesting_trips[date].append((trip, moment))
+	for date in interesting_trips:
+		interesting_trips[date].sort(key = lambda tuple: tuple[1])
+	interesting_trips = [(date, interesting_trips[date]) for date in sorted(interesting_trips.keys())]
+	data = [
+		{
+			'date': date,
+			'leaves': [{
+				'route': trip.route.reference,
+				'departure': moment,
+				'trip': trip.reference,
+				'stop': trip.schedule[0].stop,
+				'description': ' - '.join(
+					simplify_name(place, replace = True)
+					for place in trip_description(trip)
+				),
+			} for trip, moment in leaves]
+		}
+		for date, leaves in interesting_trips
+	]
+	return render_template(
+		'interesting.html',
+		data = data,
+		tr = tr,
+	)
+
 @app.route('/')
 def index():
 	return redirect('stop_cluster/kauppatori')

mercurial