service.py

changeset 73
c56b0ef96514
parent 72
65408ed066b3
child 74
3bd33720f1e3
equal deleted inserted replaced
72:65408ed066b3 73:c56b0ef96514
8 from misc import * 8 from misc import *
9 from busroute import reduce_schedule 9 from busroute import reduce_schedule
10 import buses 10 import buses
11 11
12 app = Flask(__name__) 12 app = Flask(__name__)
13 suffix_regions = {'naantalin pikatie', 'helsingin valtatie'} 13 suffix_regions = {'naantalin pikatie', 'helsingin valtatie', 'kansanpuisto'}
14 14
15 # Varmista ettei järjestelmän kieliasetukset sotke muotoiluja 15 # Varmista ettei järjestelmän kieliasetukset sotke muotoiluja
16 def reset_locale(): 16 def reset_locale():
17 locale.setlocale(locale.LC_ALL, locale.getdefaultlocale()) 17 locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
18 18
62 if request.args.get(language_name) is not None: 62 if request.args.get(language_name) is not None:
63 return language_name 63 return language_name
64 else: 64 else:
65 return request.accept_languages.best_match(tr.languages) 65 return request.accept_languages.best_match(tr.languages)
66 66
67 def sign(schedule_entry, long = False): 67 def sign_elements(schedule_entry, long = False):
68 from math import ceil 68 from math import ceil
69 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance 69 trip_length = schedule_entry['trip'].length - schedule_entry['stop'].traveled_distance
70 sign = reduce_schedule(schedule_entry['trip'].concise_schedule(schedule_entry['stop']), trip_length = trip_length, long = long) 70 return reduce_schedule(
71 schedule_entry['trip'].concise_schedule(schedule_entry['stop']),
72 trip_length = trip_length,
73 long = long)
74
75 def sign(schedule_entry, long = False):
76 sign = sign_elements(schedule_entry, long = long)
71 if sign: 77 if sign:
72 sign_representation = ' - '.join(tr(place, 'places') for place in sign if place not in suffix_regions) 78 sign_representation = ' - '.join(tr(place, 'places') for place in sign if place not in suffix_regions)
73 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions) 79 sign_representation += ''.join(' ' + tr(place, 'suffix-places') for place in sign if place in suffix_regions)
74 return sign_representation 80 return sign_representation
75 else: 81 else:
158 location = bus_stop.location, 164 location = bus_stop.location,
159 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None, 165 cluster = bus_stop.cluster.url_name if len(bus_stop.cluster.stops) > 1 else None,
160 tr = tr, 166 tr = tr,
161 ) 167 )
162 168
169 @app.route('/stop_description/<reference>')
170 def bus_stop_description(reference):
171 from buses import bus_stops
172 schedule = []
173 try:
174 bus_stop = bus_stops[reference]
175 except KeyError:
176 abort(404)
177 from collections import defaultdict, Counter
178 from busroute import simplify_name
179 destinations_per_route = defaultdict(Counter)
180 def route_key(route_ref):
181 from re import search
182 match = search(r'^([^0-9]*)([0-9]+)(.*)$', route_ref).groups()
183 if match:
184 return match[0], int(match[1]), match[2]
185 else:
186 return ()
187 def filter_names(names):
188 if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)):
189 return type(names)()
190 else:
191 return names
192 for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True):
193 sign_tuple = tuple(sign_elements(schedule_entry))
194 destinations_per_route[schedule_entry['trip'].route.reference][sign_tuple] += 1
195 routes_per_destination = defaultdict(set)
196 for route in destinations_per_route:
197 winner = filter_names(destinations_per_route[route].most_common()[0][0])
198 destinations_per_route[route] = winner and ' - '.join(winner) or ''
199 routes_per_destination[winner].add(route)
200 for key in routes_per_destination:
201 routes_per_destination[key] = sorted(routes_per_destination[key], key = route_key)
202 from pprint import pformat
203 return pformat(routes_per_destination)
204
163 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>') 205 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>')
164 def describe_destination(trip_reference, stop_reference, index): 206 def describe_destination(trip_reference, stop_reference, index):
165 from buses import bus_stops, all_trips 207 from buses import bus_stops, all_trips
166 from busroute import simplify_name 208 from busroute import simplify_name
167 from flask import jsonify 209 from flask import jsonify
194 schedule = [] 236 schedule = []
195 try: 237 try:
196 bus_stop = bus_stops[reference] 238 bus_stop = bus_stops[reference]
197 except KeyError: 239 except KeyError:
198 abort(404) 240 abort(404)
199 for i, schedule_entry in enumerate(bus_stop.schedule(max_amount = 6, arrivals = False)): 241 for i, schedule_entry in enumerate(bus_stop.schedule_for_day(today(), arrivals = False)[:6]):
200 schedule.append({ 242 schedule.append({
201 'time_data': schedule_entry['time'], 243 'time_data': schedule_entry['time'],
202 'time': time_representation(schedule_entry['time']), 244 'time': time_representation(schedule_entry['time']),
203 'route': schedule_entry['trip'].route.reference, 245 'route': schedule_entry['trip'].route.reference,
204 'sign': long_form_sign(schedule_entry), 246 'sign': long_form_sign(schedule_entry),

mercurial