Revamp calculation of stopping patterns because "/v3/stops/route/{}/route_type/{}" endpoint is a bit dodgy

This commit is contained in:
RunasSudo 2019-01-01 19:01:41 +11:00
parent 75567e2653
commit c351302f71
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 38 additions and 28 deletions

View File

@ -62,7 +62,7 @@ def parse_departure(departure, departures, timenow):
# Get stopping pattern # Get stopping pattern
result['stops'] = [] result['stops'] = []
pattern = do_request('/v3/pattern/run/{}/route_type/{}'.format(departure['run_id'], ROUTE_TYPE), {'expand': 'all'}) pattern = do_request('/v3/pattern/run/{}/route_type/{}'.format(departure['run_id'], ROUTE_TYPE), {'expand': 'all'})
pattern_stops = set(x['stop_id'] for x in pattern['departures']) pattern_stops = [(x['stop_id'], stop_to_name(pattern['stops'][str(x['stop_id'])], departure['route_id']), False) for x in pattern['departures']]
# Get all stops on route # Get all stops on route
if (departure['route_id'], departure['direction_id']) not in route_stops: if (departure['route_id'], departure['direction_id']) not in route_stops:
@ -73,34 +73,44 @@ def parse_departure(departure, departures, timenow):
route_stops_dir = route_stops[(departure['route_id'], departure['direction_id'])] route_stops_dir = route_stops[(departure['route_id'], departure['direction_id'])]
# Calculate stopping pattern # Calculate stopping pattern
express_stops = [] # express_stop_count is unreliable for the city loop result['stops'] = []
num_city_loop = 0
done_city_loop = False # Find this run along pattern_stops
for j, stop in enumerate(route_stops_dir): ps_route_start = next((k for k, stop in enumerate(pattern_stops) if stop[0] == int(flask.request.args['stop_id'])), 0)
if stop['stop_id'] == int(flask.request.args['stop_id']): ps_route_end = next((k for k, stop in enumerate(pattern_stops) if stop[0] == pattern['runs'][str(departure['run_id'])]['final_stop_id']), len(pattern_stops)-1)
break
for stop in route_stops_dir[j+1:]: # Find this run along route_stops_dir
if stop['stop_name'].replace(' Station', '') in LOOP_STATIONS: rsd_route_start = next((k for k, stop in enumerate(route_stops_dir) if stop['stop_id'] == int(flask.request.args['stop_id'])), 0)
# Calculate stopping pattern in city loop rsd_route_end = next((k for k, stop in enumerate(route_stops_dir) if stop['stop_id'] == pattern['runs'][str(departure['run_id'])]['final_stop_id']), len(route_stops_dir)-1)
if done_city_loop:
continue # Add express stops
for k, stop in enumerate(route_stops_dir[rsd_route_start:rsd_route_end+1]):
pattern['departures'].sort(key=lambda x: x['scheduled_departure_utc']) if any(x[0] == stop['stop_id'] for x in pattern_stops):
for k, stop2 in enumerate(pattern['departures']): continue
if stop2['stop_id'] == stop['stop_id']: if stop_to_name(stop, departure['route_id']) in LOOP_STATIONS:
break continue
for stop in pattern['departures'][k:]: # Express stop
result['stops'].append(pattern['stops'][str(stop['stop_id'])]['stop_name'].replace(' Station', '')) # Identify location based on previous stop
num_city_loop += 1 if rsd_route_start+k == 0:
ps_index = 0
done_city_loop = True
elif stop['stop_id'] in pattern_stops:
result['stops'].append(stop['stop_name'].replace(' Station', ''))
else: else:
ps_index = next((l+1 for l, stop in enumerate(pattern_stops) if stop[0] == route_stops_dir[rsd_route_start+k-1]['stop_id']), None)
if ps_index is None:
continue
pattern_stops.insert(ps_index, (stop['stop_id'], stop_to_name(stop, departure['route_id']), True))
ps_route_end += 1
# Convert to string
express_stops = []
num_city_loop = 0
for _, stop_name, is_express in pattern_stops[ps_route_start+1:ps_route_end+1]:
if is_express:
result['stops'].append(' ---') result['stops'].append(' ---')
express_stops.append(stop['stop_name'].replace(' Station', '')) express_stops.append(stop_name)
if stop['stop_id'] == departures['runs'][str(departure['run_id'])]['final_stop_id']: else:
break result['stops'].append(stop_name)
if stop_name in LOOP_STATIONS:
num_city_loop += 1
# Impute remainder of journey # Impute remainder of journey
if result['stops'][-1] == 'Parliament': if result['stops'][-1] == 'Parliament':
@ -140,7 +150,7 @@ def latest():
# Invalid stop ID, platform ID, no departures, etc. # Invalid stop ID, platform ID, no departures, etc.
return flask.jsonify(result) return flask.jsonify(result)
result['stop_name'] = departures['stops'][flask.request.args['stop_id']]['stop_name'].replace(' Station', '') result['stop_name'] = stop_to_name(departures['stops'][flask.request.args['stop_id']], None)
# Next train # Next train