From 842c944faa2596db3dfca5a6eee5a22fc5584a19 Mon Sep 17 00:00:00 2001 From: Kaaninchen <124433727+kaaninchen@users.noreply.github.com> Date: Mon, 17 Aug 2026 15:33:14 +0200 Subject: [PATCH] transitous rewrite: improved via --- src/api/transitous.py | 25 ++++++++++++++++++++----- src/data/operators.py | 4 ++++ src/dc/embeds.py | 1 + src/dc/handlers.py | 1 - src/utils.py | 35 +++++++++++++++++++++++++++++------ 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/api/transitous.py b/src/api/transitous.py index e124988..986684b 100644 --- a/src/api/transitous.py +++ b/src/api/transitous.py @@ -232,23 +232,38 @@ def get_trip_details(random_connection: dict | None) -> dict | None: "arrival_dt": arrival_dt, "mode": mode, "stops": {} - } + } - trip_details["stops"][train_from] = departure_dt + train_from_importance = legs["from"]["importance"] + trip_details["stops"][train_from] = { + "arrival": departure_dt, + "importance": train_from_importance + } + departure_time_iso = start_time for stop in legs["intermediateStops"]: stop_arrival_dt = parse_iso(stop["arrival"]) - trip_details["stops"][stop["name"]] = stop_arrival_dt + stop_importance = stop["importance"] + stop_details = { + "arrival": stop_arrival_dt, + "importance": stop_importance + } + trip_details["stops"][stop["name"]] = stop_details if stop.get("name") == from_station: departure_time_iso = stop["departure"] trip_details["departure"] = parse_iso(departure_time_iso).strftime("%H:%M") - trip_details["stops"][goes_to] = arrival_dt + train_to_importance = legs["to"]["importance"] + trip_details["stops"][goes_to] = { + "arrival": arrival_dt, + "importance": train_to_importance + } valid = validate_connection(start_time, end_time, departure_time_iso) if not valid: return None - + + logger(json.dumps(trip_details, indent=4, ensure_ascii=False, default=str)) return trip_details diff --git a/src/data/operators.py b/src/data/operators.py index a3bd292..a187f78 100644 --- a/src/data/operators.py +++ b/src/data/operators.py @@ -106,6 +106,10 @@ OPERATORS = { "Vr": { "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Vr_Logo.png/330px-Vr_Logo.png", "color": 0x00B451 + }, + "GVB": { + "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a3/GVB_Amsterdam_Logo_001.svg/1280px-GVB_Amsterdam_Logo_001.svg.png", + "color": 0x2B62AF } } diff --git a/src/dc/embeds.py b/src/dc/embeds.py index a383f58..610c229 100644 --- a/src/dc/embeds.py +++ b/src/dc/embeds.py @@ -18,6 +18,7 @@ def build_embed_footer(mode: str, slogans): "icon": icon } + def build_info_embed() -> discord.Embed: from src.dc.handlers import trip diff --git a/src/dc/handlers.py b/src/dc/handlers.py index a4052f6..c7a502c 100644 --- a/src/dc/handlers.py +++ b/src/dc/handlers.py @@ -18,7 +18,6 @@ async def rename_vc(bot: discord.Bot, voice_channel, from_scheduler: bool = Fals trip = choose_connection() while trip is None and attempt < max_attempt: attempt += 1 - logger(f"Attempt {attempt}: Failed to select route, retrying...", "error") trip = choose_connection() if trip is None: diff --git a/src/utils.py b/src/utils.py index f0ab591..5b7146b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -150,7 +150,8 @@ def get_sound_path(destination) -> str | None: def get_next_station(stops: dict, train_from :str) -> dict | None: now = datetime.now(LOCAL_TZ) - for name, arrival_dt in stops.items(): + for name, info in stops.items(): + arrival_dt = info["arrival"] if arrival_dt >= now: if name == train_from: return None @@ -164,20 +165,42 @@ def get_next_station(stops: dict, train_from :str) -> dict | None: def format_via_list(stops: dict) -> str: if len(stops) > 2: - count = min(3, len(stops)) - random_stops = random.sample(list(stops), k=count) - via = f"{', '.join(random_stops[:-1])} und {random_stops[-1]}" + stations = list(stops.keys()) + trip_from = stations[0] + trip_to = stations[-1] + + important_stops = sort_stations_by_importance(stops)[:3] + if trip_from in important_stops: + important_stops.remove(trip_from) + if trip_to in important_stops: + important_stops.remove(trip_to) + + via = f"{', '.join(important_stops[:-1])} und {important_stops[-1]}" return via return None +def sort_stations_by_importance(stops: dict) -> list: + sorted_stations_dict = dict( + sorted( + [(name, info["importance"]) for name, info in stops.items()], + key=lambda x: x[1], + reverse=True) + ) + + sorted_stations = list(sorted_stations_dict.keys()) + return sorted_stations + def format_stop_list(stops: dict, next_stop: str | None) -> list[tuple[str, str]]: fields = [] field_lines, field_length, part = [], 0, 1 - for name, stop_arrival in stops.items(): + for name, info in stops.items(): if name == next_stop: + stop_arrival = info["arrival"] + print(stop_arrival) line = f"• __{name}__ ({stop_arrival.strftime("%H:%M")} Uhr)" else: + stop_arrival = info["arrival"] line = f"• {name} ({stop_arrival.strftime("%H:%M")} Uhr)" if field_length + len(line) + 1 > 1024: @@ -192,4 +215,4 @@ def format_stop_list(stops: dict, next_stop: str | None) -> list[tuple[str, str] route_page_name = "Route" if part == 1 else "Route (Fortsetzung)" fields.append((route_page_name, "\n".join(field_lines))) - return fields \ No newline at end of file + return fields