Implement station select

This commit is contained in:
RunasSudo 2019-01-01 02:46:55 +11:00
parent 0d73a00a3a
commit d561350082
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 45 additions and 4 deletions

View File

@ -40,7 +40,7 @@ timezone = pytz.timezone('Australia/Melbourne')
@app.route('/') @app.route('/')
def index(): def index():
return app.send_static_file('index.html') return flask.render_template('index.html')
@app.route('/latest') @app.route('/latest')
def latest(): def latest():
@ -107,10 +107,28 @@ def latest():
for departure in departures['departures'][i+1:i+3]: for departure in departures['departures'][i+1:i+3]:
result['next'].append({}) result['next'].append({})
result['next'][-1]['dest'] = dest_to_service_name(departures['runs'][str(departure['run_id'])]['destination_name']) result['next'][-1]['dest'] = dest_to_service_name(departures['runs'][str(departure['run_id'])]['destination_name'])
result['next'][-1]['desc'] = 'Express' if num_express > 4 else 'Stops All Stations' if num_express == 0 else 'Limited Express' result['next'][-1]['desc'] = 'Stops All Stations' if num_express == 0 else 'Limited Express'
result['next'][-1]['sch'] = parse_date(departure['scheduled_departure_utc']).strftime('%I:%M').lstrip('0') result['next'][-1]['sch'] = parse_date(departure['scheduled_departure_utc']).strftime('%I:%M').lstrip('0')
result['next'][-1]['min'] = '{} min'.format(round((parse_date(departure['estimated_departure_utc'] or departure['scheduled_departure_utc']) - timenow).total_seconds() / 60)) result['next'][-1]['min'] = '{} min'.format(round((parse_date(departure['estimated_departure_utc'] or departure['scheduled_departure_utc']) - timenow).total_seconds() / 60))
return flask.jsonify(result) return flask.jsonify(result)
... # Cache list of stations
stns = None
@app.route('/stations')
def stations():
global stns
if not stns:
stn_list = {}
routes = do_request('/v3/routes', {'route_types': ROUTE_TYPE})
for route in routes['routes']:
stops = do_request('/v3/stops/route/{}/route_type/{}'.format(route['route_id'], ROUTE_TYPE), {})
for stop in stops['stops']:
stn_list[stop['stop_id']] = stop['stop_name'].replace(' Station', '')
stns = [(k, v) for k, v in stn_list.items()]
stns.sort(key=lambda x: x[1])
return flask.render_template('stations.html', stations=stns)

View File

@ -73,7 +73,7 @@
return false; return false;
} }
</script> </script>
<div id="topbar">Stop <span id="stopinfo">?</span> [change] Platform <span id="platinfo">?</span> [<a href="#" onclick="return changePlatform();">change</a>]</div> <div id="topbar">Stop <span id="stopinfo">?</span> [<a href="stations">change</a>] Platform <span id="platinfo">?</span> [<a href="#" onclick="return changePlatform();">change</a>]</div>
<object id="mainsvg" data="static/template.svg" type="image/svg+xml" onload="start();"></object> <object id="mainsvg" data="static/template.svg" type="image/svg+xml" onload="start();"></object>
<script> <script>
document.getElementById("stopinfo").innerText = stop_id; document.getElementById("stopinfo").innerText = stop_id;

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Virtual Metro</title>
<style type="text/css">
body, html {
color: #eee;
background-color: black;
}
a, a:active, a:visited {
color: #eee;
}
</style>
</head>
<body>
<ul>
{% for stop_id, stop_name in stations %}
<li><a href="/?stop_id={{stop_id}}">{{stop_name}}</a></li>
{% endfor %}
</ul>
</body>
</html>