Implement PTV data fetching

This commit is contained in:
Kenneth Allen 2025-05-14 22:35:55 +10:00
parent 020873fc40
commit f8cc9f213c
5 changed files with 53 additions and 25 deletions

View File

@ -55,7 +55,7 @@ pal_image.putpalette((0x00,0x00,0x00, 0xff,0xff,0xff, 0xff,0xff,0x00, 0xff,0x00,
image_4color = image.quantize(palette=pal_image, dither=Image.NONE)
image_4color.save('test_after_palette.png')
show_on_screen = True
show_on_screen = False
if show_on_screen:
import epd7in3g as epd
try:

View File

@ -7,14 +7,13 @@ class Cache:
self.db = db
def get(self, key, timeout, f):
now = datetime.datetime.now()
now = datetime.datetime.now(datetime.timezone.utc)
earliest_acceptable = now - timeout
match self.db.execute(
'SELECT value FROM cache WHERE key = ? AND timestamp >= ?',
(key, earliest_acceptable.isoformat()),
).fetchall():
case [(value,)]:
self.db.commit()
return value
value = f()

63
ptv.py
View File

@ -17,14 +17,7 @@ def sign(request):
def fetch(path):
res = httpx.get(sign(path))
try:
res.raise_for_status()
except:
try:
print(res.json())
except:
pass
raise
return res.json()
# Lilydale route 9 (type 0) (dir 1 in, 8 out) (stop 1229)
@ -46,22 +39,50 @@ local_routes = [
Route('Lilydale', 9 , 0, [(1, 'in'), (8, 'out')] , [1229] ),
Route('Belgrave', 2 , 0, [(1, 'in'), (2, 'out')] , [1229] ),
Route('109' , 722 , 1, [(2, 'E'), (3, 'W')] , [2415, 2460]),
Route('70' , 940 , 1, [(28, 'E'), (29, 'W')] , [2415, 2460]),
Route('766' , 15800, 2, [(13, 'N'), (207, 'S')] , [17861] ),
Route('612' , 13024, 2, [(13, 'N'), (158, 'S')] , [17861] ),
Route('70' , 940 , 1, [(28, 'E'), (29, 'W')] , [2162, 2161]),
Route('766' , 15800, 2, [(13, 'N'), (188, 'S')] , [17861] ),
Route('612' , 13024, 2, [(13, 'N'), (139, 'S')] , [17861] ),
]
# exp 951624
# non-exp 951826
def fetch_departures(stop_id, route_type_id):
deps = fetch(f'/v3/departures/route_type/{route_type_id}/stop/{stop_id}')['departures']
for dep in deps:
for key in ['estimated_departure_utc', 'scheduled_departure_utc']:
if dep[key]:
dep[key] = datetime.fromisoformat(dep[key])
deps.sort(key=lambda dep: dep['estimated_departure_utc'] or dep['scheduled_departure_utc'])
return deps
return fetch(f'/v3/departures/route_type/{route_type_id}/stop/{stop_id}')['departures']
def fetch_run(run_ref):
return fetch(f'/v3/runs/{run_ref}')['runs']
def departure_time(dep):
return datetime.fromisoformat(dep['estimated_departure_utc'] or dep['scheduled_departure_utc'])
def get_departure_data():
by_route_type = defaultdict(list)
for route_type, stop in set((r.route_type_id, s) for r in local_routes for s in r.stops):
by_route_type[route_type] += fetch_departures(stop, route_type)
return by_route_type
bus = fetch_departures(17861, 2)
train = fetch_departures(1229, 0)
tram_109w = fetch_departures(2460, 1)
tram_70w = fetch_departures(2161, 1)
next_express_dep = None
train.sort(key=departure_time)
for dep in train:
if dep['direction_id'] == 1:
match fetch_run(dep['run_ref']):
case [run]:
if run['express_stop_count'] > 1: # Ignore East Richmond
next_express_dep = dep
break
def earliest(deps):
return min((departure_time(dep) for dep in deps), default=None)
return {
'612 S': earliest(dep for dep in bus if dep['route_id'] == 13024 and dep['direction_id'] == 139),
'612 N': earliest(dep for dep in bus if dep['route_id'] == 13024 and dep['direction_id'] == 13),
'766 N': earliest(dep for dep in bus if dep['route_id'] == 15800 and dep['direction_id'] == 13),
'Union W': earliest(dep for dep in train if dep['direction_id'] == 1 and dep is not next_express_dep),
'Union W Express': departure_time(next_express_dep) if next_express_dep else None,
'Union E': earliest(dep for dep in train if dep['direction_id'] in {2, 8}),
'109 W': earliest(tram_109w),
'70 W': earliest(tram_70w),
}

View File

@ -1,5 +1,13 @@
anyio==4.9.0
certifi==2025.4.26
colorzero==2.0
gpiozero==2.0.1
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.10
lgpio==0.2.2.0
pillow==11.2.1
sniffio==1.3.1
spidev==3.6
typing_extensions==4.13.2

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB