From bcb0b6e080d7beda5c64e08347eebef1940e31d6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 28 Mar 2021 11:47:52 +0200 Subject: fall back to stop id and proximity search on error This is useful for non-VRR stops, which may be split into several sub-stops. For instance, Aachen Rothe Erde is listed as de:05334:1645 -- which is the bus stop. The train station is de:05334:1646. proximity search returns results for both stops. Requires vrr-fakedisplay 1.6.0 or later --- bin/efa-gw | 40 +++++++++++++++++++++++++++++----------- bin/merge-haltestellen-and-iris | 24 +++++++++--------------- 2 files changed, 38 insertions(+), 26 deletions(-) (limited to 'bin') diff --git a/bin/efa-gw b/bin/efa-gw index d56604c..1f73c70 100755 --- a/bin/efa-gw +++ b/bin/efa-gw @@ -25,6 +25,19 @@ def get_occupancy(occupancy): return None +def build_response(content): + train_data = dict() + for train in content["raw"]: + if train["train_no"]: + train_data[train["train_no"]] = { + "occupancy": get_occupancy(train["occupancy"]) + } + + reply = {"train": train_data} + + return web.Response(body=json.dumps(reply), headers=headers) + + async def handle_eva(request): try: eva = int(request.match_info.get("eva")) @@ -32,7 +45,7 @@ async def handle_eva(request): return web.HTTPBadRequest(text="EVA must be a number") try: - station = eva_to_name[eva] + station, optid = eva_to_name[eva] except KeyError: return web.HTTPNotFound(text="Unknown EVA") @@ -42,17 +55,22 @@ async def handle_eva(request): ) as response: content = await response.text() content = json.loads(content) - train_data = dict() - for train in content["raw"]: - if train["train_no"]: - train_data[train["train_no"]] = { - "occupancy": get_occupancy(train["occupancy"]) - } + print(content) + + if not content["error"]: + return build_response(content) + + 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) - reply = {"train": train_data} + print(content) - return web.Response(body=json.dumps(reply), headers=headers) + return build_response(content) if __name__ == "__main__": @@ -66,8 +84,8 @@ if __name__ == "__main__": args = parser.parse_args() with open("share/vrr.json", "r") as f: - for eva, (city, stop, name) in json.load(f).items(): - eva_to_name[int(eva)] = name + for eva, (name, optid) in json.load(f).items(): + eva_to_name[int(eva)] = name, optid app = web.Application() app.add_routes([web.get(f"{args.prefix}{{eva}}.json", handle_eva)]) diff --git a/bin/merge-haltestellen-and-iris b/bin/merge-haltestellen-and-iris index 4cc64ca..b801c5a 100755 --- a/bin/merge-haltestellen-and-iris +++ b/bin/merge-haltestellen-and-iris @@ -17,13 +17,11 @@ vrr_stops = list() with open("haltestellenliste.csv", "r", encoding="iso-8859-1") as f: f.readline() cr = csv.reader(f, delimiter=";") - for city, place, _, _, _, lon, lat, stop, _, _ in cr: + for _, _, _, _, optid, lon, lat, stop, _, _ in cr: lon = lon[:5] + lon[6:] lat = lat[:6] + lat[7:] try: - vrr_stops.append( - (stop.strip(), float(lat), float(lon), city.strip(), place.strip()) - ) + vrr_stops.append((stop.strip(), float(lat), float(lon), optid.strip())) except ValueError: # invalid entry pass @@ -38,24 +36,20 @@ for iris_stop in ProgressBar(max=len(iris_stops)).iter(iris_stops): iris_name = iris_stop["name"] iris_lat, iris_lon = iris_stop["latlong"] - for stop, lat, lon, city, place in vrr_stops: + for stop, lat, lon, optid in vrr_stops: if stop == iris_name: - output[eva] = city, place, stop + output[eva] = stop, optid break if eva in output: continue candidates = list() - for stop, lat, lon, city, place in vrr_stops: + for stop, lat, lon, optid in vrr_stops: if abs(lat - iris_lat) < 0.01 and abs(lon - iris_lon) < 0.01: candidates.append( ( - stop, - lat, - lon, - city, - place, + (stop, lat, lon, optid), distance((lat, lon), (iris_lat, iris_lon)).m, ) ) @@ -63,10 +57,10 @@ for iris_stop in ProgressBar(max=len(iris_stops)).iter(iris_stops): if not candidates: continue - candidates.sort(key=lambda x: x[5]) + candidates.sort(key=lambda x: x[1]) - stop, _, _, city, place, _ = candidates[0] - output[eva] = city, place, stop + stop, lat, lon, optid = candidates[0][0] + output[eva] = stop, optid with open("share/vrr.json", "w", encoding="utf-8") as f: json.dump(output, f, ensure_ascii=False) -- cgit v1.2.3