164 location = bus_stop.location, |
164 location = bus_stop.location, |
165 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, |
166 tr = tr, |
166 tr = tr, |
167 ) |
167 ) |
168 |
168 |
|
169 def week_schedule(bus_stop, **kwargs): |
|
170 for i in range(-1, 7): |
|
171 yield from bus_stop.schedule_for_day(today() + timedelta(i), **kwargs) |
|
172 |
|
173 def route_key(route): |
|
174 match = re.search(r'^([a-zA-Z]*)(\d+)(.*)$', route) |
|
175 if match: |
|
176 groups = match.groups() |
|
177 return (groups[0], int(groups[1]), groups[2]) |
|
178 else: |
|
179 return (route,) |
|
180 |
169 @app.route('/stop_description/<reference>') |
181 @app.route('/stop_description/<reference>') |
170 def bus_stop_description(reference): |
182 def bus_stop_description(reference): |
171 from buses import bus_stops |
183 from buses import bus_stops |
172 schedule = [] |
184 schedule = [] |
173 try: |
185 try: |
187 def filter_names(names): |
199 def filter_names(names): |
188 if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)): |
200 if len(names) == 1 and names[0] == (bus_stop.region and simplify_name(bus_stop.region)): |
189 return type(names)() |
201 return type(names)() |
190 else: |
202 else: |
191 return names |
203 return names |
192 for schedule_entry in bus_stop.schedule(max_amount = 100, arrivals = True): |
204 data = [] |
|
205 names = [] |
|
206 from collections import defaultdict |
|
207 night_routes = defaultdict(lambda: True) |
|
208 num_leaves = 0 |
|
209 for schedule_entry in week_schedule(bus_stop, arrivals = True): #bus_stop.schedule(max_amount = 500, arrivals = True): |
193 sign_tuple = tuple(sign_elements(schedule_entry)) |
210 sign_tuple = tuple(sign_elements(schedule_entry)) |
|
211 night_routes[schedule_entry['trip'].route.reference] &= is_night_time(schedule_entry['time']) |
|
212 #for entry in sign_tuple: |
|
213 # if entry not in names: |
|
214 # names.append(entry) |
|
215 #sign_tuple = tuple(names.index(place) for place in sign_tuple) |
194 destinations_per_route[schedule_entry['trip'].route.reference][sign_tuple] += 1 |
216 destinations_per_route[schedule_entry['trip'].route.reference][sign_tuple] += 1 |
|
217 num_leaves += 1 |
|
218 night_routes = {key for key, value in night_routes.items() if value} |
195 routes_per_destination = defaultdict(set) |
219 routes_per_destination = defaultdict(set) |
196 for route in destinations_per_route: |
220 for route in destinations_per_route: |
197 winner = filter_names(destinations_per_route[route].most_common()[0][0]) |
221 winner, count = destinations_per_route[route].most_common()[0] |
198 destinations_per_route[route] = winner and ' - '.join(winner) or '' |
222 if count >= 10 or count / num_leaves >= 0.01: |
199 routes_per_destination[winner].add(route) |
223 winner = filter_names(winner) |
|
224 #destinations_per_route[route] = winner and ' - '.join(winner) or '' |
|
225 routes_per_destination[winner].add(route) |
200 for key in routes_per_destination: |
226 for key in routes_per_destination: |
201 routes_per_destination[key] = sorted(routes_per_destination[key], key = route_key) |
227 routes_per_destination[key] = sorted(routes_per_destination[key], key = route_key) |
202 from pprint import pformat |
228 from pprint import pformat |
203 return pformat(routes_per_destination) |
229 return '<pre>' + \ |
|
230 'names: ' + str(names) + '\n '+ \ |
|
231 'night_routes: ' + str(night_routes) + '\n '+ \ |
|
232 'destinations_per_route: ' + pformat(dict(destinations_per_route)) + '\n' + \ |
|
233 'routes_per_destination: ' + pformat(dict(routes_per_destination)) + '</pre>' |
204 |
234 |
205 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>') |
235 @app.route('/api/describe_destination/<trip_reference>/<stop_reference>/<int:index>') |
206 def describe_destination(trip_reference, stop_reference, index): |
236 def describe_destination(trip_reference, stop_reference, index): |
207 from buses import bus_stops, all_trips |
237 from buses import bus_stops, all_trips |
208 from busroute import simplify_name |
238 from busroute import simplify_name |