Implement even more caching

This commit is contained in:
RunasSudo 2019-01-11 22:02:57 +11:00
parent 4cca5f231f
commit 6553e77c02
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -9,6 +9,7 @@ import hashlib
import hmac import hmac
import json import json
import pytz import pytz
import traceback
from binascii import hexlify from binascii import hexlify
from datetime import datetime, timedelta from datetime import datetime, timedelta
from urllib.parse import urlencode from urllib.parse import urlencode
@ -32,7 +33,16 @@ def do_request(endpoint, args=None):
signature = hexlify(hmac.digest(config.PTV_API_KEY.encode('ascii'), url.encode('ascii'), 'sha1')).decode('ascii') signature = hexlify(hmac.digest(config.PTV_API_KEY.encode('ascii'), url.encode('ascii'), 'sha1')).decode('ascii')
req = Request('https://timetableapi.ptv.vic.gov.au' + url + '&signature=' + signature, headers={'User-Agent': 'virtual-metro/0.1'}) req = Request('https://timetableapi.ptv.vic.gov.au' + url + '&signature=' + signature, headers={'User-Agent': 'virtual-metro/0.1'})
resp = urlopen(req) try:
resp = urlopen(req)
except Exception as ex:
print('Unable to refresh cache')
traceback.print_exc()
if url in request_cache:
return request_cache[url][1]
else:
return None
data = json.load(resp) data = json.load(resp)
# Cache the response # Cache the response
@ -62,6 +72,7 @@ def stop_to_name(stop, route_id):
name = 'South Kensington' name = 'South Kensington'
return name return name
route_stops = {} # Cache lookup
def parse_departure(departure, departures, timenow): def parse_departure(departure, departures, timenow):
result = {} result = {}
result['dest'] = departures['runs'][str(departure['run_id'])]['destination_name'] result['dest'] = departures['runs'][str(departure['run_id'])]['destination_name']
@ -78,11 +89,13 @@ def parse_departure(departure, departures, timenow):
pattern_stops = [(x['stop_id'], stop_to_name(pattern['stops'][str(x['stop_id'])], departure['route_id']), False) 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
stops = do_request('/v3/stops/route/{}/route_type/{}'.format(departure['route_id'], ROUTE_TYPE), {'direction_id': departure['direction_id']}) if (departure['route_id'], departure['direction_id']) not in route_stops:
stops['stops'].sort(key=lambda x: x['stop_sequence']) stops = do_request('/v3/stops/route/{}/route_type/{}'.format(departure['route_id'], ROUTE_TYPE), {'direction_id': departure['direction_id']})
stops['stops'].sort(key=lambda x: x['stop_sequence'])
route_stops[(departure['route_id'], departure['direction_id'])] = stops['stops']
route_stops_dir = [] route_stops_dir = []
for stop in stops['stops']: for stop in route_stops[(departure['route_id'], departure['direction_id'])]:
# Cut off at Flinders Street # Cut off at Flinders Street
route_stops_dir.append(stop) route_stops_dir.append(stop)
if stop_to_name(stop, departure['route_id']) == 'Flinders Street': if stop_to_name(stop, departure['route_id']) == 'Flinders Street':