From 640df543aad9267c3a15922ab7fb6e4ae14a25ce Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 12 Jul 2021 19:32:52 +0200 Subject: show stop directly if searched name is an exact match --- bin/nvm | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/nvm b/bin/nvm index 610fe3d..fcca040 100755 --- a/bin/nvm +++ b/bin/nvm @@ -250,11 +250,13 @@ class Line: return self.name -async def show_departure_board(request): - try: - eva = int(request.match_info.get("eva")) - except ValueError: - return web.HTTPBadRequest(text="EVA must be a number at the moment") +async def show_departure_board(request, eva=None): + + if eva is None: + try: + eva = int(request.match_info.get("eva")) + except ValueError: + return web.HTTPBadRequest(text="EVA must be a number at the moment") request_url = ( f"{db_rest_api}/stops/{eva}/departures?results=60&duration=120&stopovers=true" @@ -314,6 +316,11 @@ async def redirect_to_departure_board(request): async with aiohttp.ClientSession() as session: async with session.get(request_url) as response: stops = await response.json() + + for stop in stops: + if stop_name.lower() == stop["name"].lower(): + return await show_departure_board(request, stop["id"]) + stops_page = env.get_template("stops.html") return web.Response( body=stops_page.render(title=f"Suche nach „{stop_name}“", stops=stops), @@ -333,7 +340,9 @@ async def ajax_geolocation(request): request_data = await request.json() lat = request_data["lat"] lon = request_data["lon"] - request_url = f"{db_rest_api}/stops/nearby?latitude={lat}&longitude={lon}&results=10" + request_url = ( + f"{db_rest_api}/stops/nearby?latitude={lat}&longitude={lon}&results=10" + ) logging.debug(f"Requesting stops near {lat}/{lon} from {request_url}") async with aiohttp.ClientSession() as session: async with session.get(request_url) as response: -- cgit v1.2.3