service.py

changeset 74
3bd33720f1e3
parent 73
c56b0ef96514
child 76
5fd79554c3aa
--- a/service.py	Fri Nov 24 22:41:48 2017 +0200
+++ b/service.py	Fri Dec 01 08:04:57 2017 +0200
@@ -166,6 +166,18 @@
 		tr = tr,
 	)
 
+def week_schedule(bus_stop, **kwargs):
+	for i in range(-1, 7):
+		yield from bus_stop.schedule_for_day(today() + timedelta(i), **kwargs)
+
+def route_key(route):
+	match = re.search(r'^([a-zA-Z]*)(\d+)(.*)$', route)
+	if match:
+		groups = match.groups()
+		return (groups[0], int(groups[1]), groups[2])
+	else:
+		return (route,)
+
 @app.route('/stop_description/<reference>')
 def bus_stop_description(reference):
 	from buses import bus_stops
@@ -189,18 +201,36 @@
 			return type(names)()
 		else:
 			return names
-	for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True):
+	data = []
+	names = []
+	from collections import defaultdict
+	night_routes = defaultdict(lambda: True)
+	num_leaves = 0
+	for schedule_entry in week_schedule(bus_stop, arrivals = True): #bus_stop.schedule(max_amount = 500, arrivals = True):
 		sign_tuple = tuple(sign_elements(schedule_entry))
+		night_routes[schedule_entry['trip'].route.reference] &= is_night_time(schedule_entry['time'])
+		#for entry in sign_tuple:
+		#	if entry not in names:
+		#		names.append(entry)
+		#sign_tuple = tuple(names.index(place) for place in sign_tuple)
 		destinations_per_route[schedule_entry['trip'].route.reference][sign_tuple] += 1
+		num_leaves += 1
+	night_routes = {key for key, value in night_routes.items() if value}
 	routes_per_destination = defaultdict(set)
 	for route in destinations_per_route:
-		winner = filter_names(destinations_per_route[route].most_common()[0][0])
-		destinations_per_route[route] = winner and ' - '.join(winner) or ''
-		routes_per_destination[winner].add(route)
+		winner, count = destinations_per_route[route].most_common()[0]
+		if count >= 10 or count / num_leaves >= 0.01:
+			winner = filter_names(winner)
+			#destinations_per_route[route] = winner and ' - '.join(winner) or ''
+			routes_per_destination[winner].add(route)
 	for key in routes_per_destination:
 		routes_per_destination[key] = sorted(routes_per_destination[key], key = route_key)
 	from pprint import pformat
-	return pformat(routes_per_destination)
+	return '<pre>' + \
+		'names: ' + str(names) + '\n '+ \
+		'night_routes: ' + str(night_routes) + '\n '+ \
+		'destinations_per_route: ' + pformat(dict(destinations_per_route)) + '\n' + \
+		'routes_per_destination: ' + pformat(dict(routes_per_destination)) + '</pre>'
 
 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>')
 def describe_destination(trip_reference, stop_reference, index):

mercurial