From 8cfb1b9a719b4fd0ec51e1c489e1f67a90e5aa99 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 6 Jul 2021 18:34:47 +0200 Subject: prepare for serving a generic efa-json-frontend --- bin/efa-gw | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bin/efa-gw b/bin/efa-gw index 077007f..4b28828 100755 --- a/bin/efa-gw +++ b/bin/efa-gw @@ -20,7 +20,7 @@ headers = { occupancy_map = {"MANY_SEATS": 1, "FEW_SEATS": 2, "STANDING_ONLY": 3} -cache = dict() +occupancy_cache = dict() eva_to_name = dict() @@ -31,7 +31,7 @@ def get_occupancy(occupancy): return None -def build_response(content, eva, now): +def build_occupancy_response(content, eva, now): train_data = dict() for train in content["raw"]: if train["train_no"]: @@ -41,12 +41,12 @@ def build_response(content, eva, now): reply = {"train": train_data} - cache[eva] = (now, reply) + occupancy_cache[eva] = (now, reply) return web.Response(body=json.dumps(reply), headers=headers) -async def handle_eva(request): +async def get_occupancy_by_eva(request): try: eva = int(request.match_info.get("eva")) except ValueError: @@ -59,27 +59,25 @@ async def handle_eva(request): now = datetime.now().timestamp() - if eva in cache and now - cache[eva][0] < 300: - return web.Response(body=json.dumps(cache[eva][1]), headers=headers) + if eva in occupancy_cache and now - occupancy_cache[eva][0] < 300: + return web.Response(body=json.dumps(occupancy_cache[eva][1]), headers=headers) async with aiohttp.ClientSession() as session: async with session.get( f"https://vrrf.finalrewind.org/{station}.json?line=RE,RB,S&backend=efa.VRR2" ) as response: - content = await response.text() - content = json.loads(content) + content = await response.json() if not content["error"]: - return build_response(content, eva, now) + return build_occupancy_response(content, eva, now) async with aiohttp.ClientSession() as session: async with session.get( f"https://vrrf.finalrewind.org/{optid}.json?line=RE,RB,S&backend=efa.VRR2&proximity_search=1" ) as response: - content = await response.text() - content = json.loads(content) + content = await response.json() - return build_response(content, eva, now) + return build_occupancy_response(content, eva, now) if __name__ == "__main__": @@ -94,5 +92,7 @@ if __name__ == "__main__": eva_to_name[int(eva)] = name, optid app = web.Application() - app.add_routes([web.get(f"{args.prefix}{{eva}}.json", handle_eva)]) + app.add_routes( + [web.get(f"{args.prefix}occupancy-by-eva/{{eva}}.json", get_occupancy_by_eva)] + ) web.run_app(app, host="localhost", port=args.port) -- cgit v1.2.3