diff --git a/main.py b/main.py index 3e5f5f4..222cb10 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ import discord import random -import time -from discord.ext import tasks, commands +from discord.ext import tasks from src.config import config from src.handlers import rename_vc from src.commands import setup_commands diff --git a/src/embeds.py b/src/embeds.py index 46ecd2f..87e2458 100644 --- a/src/embeds.py +++ b/src/embeds.py @@ -24,12 +24,12 @@ def format_iso_timestamp(isostr): return discord.utils.format_dt(parsed_time, style="t") def build_info_embed() -> discord.Embed | None: - if handlers.current is None: - return None - conn = handlers.current info = handlers.train_info + if handlers.current is None and handlers.train_info is None: + return None + current_operator = resolve_operator(info["operators"]) arrival = format_iso_timestamp(info["arrival"]) departure = format_timestamp(conn['departure']) diff --git a/src/handlers.py b/src/handlers.py index 7657b86..2717939 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -22,7 +22,7 @@ async def rename_vc(bot: discord.Bot): attempt = 0 while True: - if attempt > 3: + if attempt == 3: logger("Zu viele Fehlversuche. Füge einen anderen Bahnhof hinzu.") return "Es konnte kein Zug gefunden werden." @@ -39,11 +39,12 @@ async def rename_vc(bot: discord.Bot): train = parts[1] train_ID = current['train_number'] - train_info = get_train_info(station=current['station'], train_ID=train_ID, train_type=train_type) + station = current['station'] + train_info = get_train_info(station=station, train_ID=train_ID, train_type=train_type) if train_info and train_info.get('operators') and train_info.get('arrival'): break - logger(f"Versuch {attempt}: Keine Ankunftszeit für {current['train']} verfügbar, versuche neue Verbindung...") + logger(f"Versuch {attempt}: Fehler bei {current['train']} von {station}, versuche neue Verbindung...") train_name = f"{train} nach {current['destination']} von {current['station']}" logger(f"Vorbereitung auf {train_name} (typ: {train_type})") @@ -65,7 +66,7 @@ async def _schedule_next_umstieg(bot, arrival): wait_seconds = (arrival - datetime.now()).total_seconds() if wait_seconds > 0: remaining = str(timedelta(seconds=wait_seconds)) - logger(f"SCHEDULER: Nächster Umstieg in {remaining.split(".")[0]} ({arrival.strftime('%H:%M:%S')} Uhr)") + logger(f"Nächster Umstieg in {remaining.split(".")[0]} ({arrival.strftime('%H:%M:%S')} Uhr)") await asyncio.sleep(wait_seconds) logger("Zug angekommen, wähle neue Verbindung...") await rename_vc(bot) \ No newline at end of file diff --git a/src/utils.py b/src/utils.py index 5bd8a73..08ba1a0 100644 --- a/src/utils.py +++ b/src/utils.py @@ -38,6 +38,7 @@ def random_connection(): departures = [ d for d in data.get("departures", []) if d.get("scheduledDeparture") and d.get("destination") != station + and not d.get("train", "").startswith("S ") ] if not departures: @@ -60,23 +61,28 @@ def random_connection(): def get_train_info(station, train_ID, train_type): url = f"https://dbf.finalrewind.org/z/{train_type}%20{train_ID}/{station}.json" + logger(f"Fetche {url}") try: response = requests.get(url) response.raise_for_status() data = response.json() - except requests.RequestException: + except requests.RequestException as e: + logger(f"ReqestException Fehler: {e}") return None dep = data.get("departure", []) if not dep: + logger("Kein departure Feld gefunden") return None route_post = dep.get("route_post_diff") if not route_post: + logger("Kein route_post_diff Feld gefunden") return None arrival_iso = route_post[-1].get("sched_arr") if not arrival_iso: + logger("Keine Ankunftszeit gefunden") return None return {