--- a/service.py Thu Oct 26 18:02:27 2017 +0300 +++ b/service.py Fri Nov 24 22:41:48 2017 +0200 @@ -10,7 +10,7 @@ import buses app = Flask(__name__) -suffix_regions = {'naantalin pikatie', 'helsingin valtatie'} +suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'} # Varmista ettei järjestelmän kieliasetukset sotke muotoiluja def reset_locale(): @@ -64,10 +64,16 @@ else: return request.accept_languages.best_match(tr.languages) -def sign(schedule_entry, long = False): +def sign_elements(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, long = long) + return reduce_schedule( + schedule_entry['trip'].concise_schedule(schedule_entry['stop']), + trip_length = trip_length, + long = long) + +def sign(schedule_entry, long = False): + sign = sign_elements(schedule_entry, long = long) if sign: sign_representation = ' - '.join(tr(place, 'places') 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) @@ -160,6 +166,42 @@ tr = tr, ) +@app.route('/stop_description/<reference>') +def bus_stop_description(reference): + from buses import bus_stops + schedule = [] + try: + bus_stop = bus_stops[reference] + except KeyError: + abort(404) + from collections import defaultdict, Counter + from busroute import simplify_name + destinations_per_route = defaultdict(Counter) + def route_key(route_ref): + from re import search + match = search(r'^([^0-9]*)([0-9]+)(.*)$', route_ref).groups() + if match: + return match[0], int(match[1]), match[2] + else: + return () + def filter_names(names): + if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)): + return type(names)() + else: + return names + for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True): + sign_tuple = tuple(sign_elements(schedule_entry)) + destinations_per_route[schedule_entry['trip'].route.reference][sign_tuple] += 1 + 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) + 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) + @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>') def describe_destination(trip_reference, stop_reference, index): from buses import bus_stops, all_trips @@ -196,7 +238,7 @@ bus_stop = bus_stops[reference] except KeyError: abort(404) - for i, schedule_entry in enumerate(bus_stop.schedule(max_amount = 6, arrivals = False)): + for i, schedule_entry in enumerate(bus_stop.schedule_for_day(today(), arrivals = False)[:6]): schedule.append({ 'time_data': schedule_entry['time'], 'time': time_representation(schedule_entry['time']),