Bug-Fixes bei Zugauswahl

This commit is contained in:
Kaaninchen
2026-07-20 12:11:52 +02:00
parent d25d1f6f60
commit 9f1ce60aa6
4 changed files with 16 additions and 10 deletions
+1 -2
View File
@@ -1,7 +1,6 @@
import discord import discord
import random import random
import time from discord.ext import tasks
from discord.ext import tasks, commands
from src.config import config from src.config import config
from src.handlers import rename_vc from src.handlers import rename_vc
from src.commands import setup_commands from src.commands import setup_commands
+3 -3
View File
@@ -24,12 +24,12 @@ def format_iso_timestamp(isostr):
return discord.utils.format_dt(parsed_time, style="t") return discord.utils.format_dt(parsed_time, style="t")
def build_info_embed() -> discord.Embed | None: def build_info_embed() -> discord.Embed | None:
if handlers.current is None:
return None
conn = handlers.current conn = handlers.current
info = handlers.train_info info = handlers.train_info
if handlers.current is None and handlers.train_info is None:
return None
current_operator = resolve_operator(info["operators"]) current_operator = resolve_operator(info["operators"])
arrival = format_iso_timestamp(info["arrival"]) arrival = format_iso_timestamp(info["arrival"])
departure = format_timestamp(conn['departure']) departure = format_timestamp(conn['departure'])
+5 -4
View File
@@ -22,7 +22,7 @@ async def rename_vc(bot: discord.Bot):
attempt = 0 attempt = 0
while True: while True:
if attempt > 3: if attempt == 3:
logger("Zu viele Fehlversuche. Füge einen anderen Bahnhof hinzu.") logger("Zu viele Fehlversuche. Füge einen anderen Bahnhof hinzu.")
return "Es konnte kein Zug gefunden werden." return "Es konnte kein Zug gefunden werden."
@@ -39,11 +39,12 @@ async def rename_vc(bot: discord.Bot):
train = parts[1] train = parts[1]
train_ID = current['train_number'] 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'): if train_info and train_info.get('operators') and train_info.get('arrival'):
break 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']}" train_name = f"{train} nach {current['destination']} von {current['station']}"
logger(f"Vorbereitung auf {train_name} (typ: {train_type})") 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() wait_seconds = (arrival - datetime.now()).total_seconds()
if wait_seconds > 0: if wait_seconds > 0:
remaining = str(timedelta(seconds=wait_seconds)) 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) await asyncio.sleep(wait_seconds)
logger("Zug angekommen, wähle neue Verbindung...") logger("Zug angekommen, wähle neue Verbindung...")
await rename_vc(bot) await rename_vc(bot)
+7 -1
View File
@@ -38,6 +38,7 @@ def random_connection():
departures = [ departures = [
d for d in data.get("departures", []) d for d in data.get("departures", [])
if d.get("scheduledDeparture") and d.get("destination") != station if d.get("scheduledDeparture") and d.get("destination") != station
and not d.get("train", "").startswith("S ")
] ]
if not departures: if not departures:
@@ -60,23 +61,28 @@ def random_connection():
def get_train_info(station, train_ID, train_type): def get_train_info(station, train_ID, train_type):
url = f"https://dbf.finalrewind.org/z/{train_type}%20{train_ID}/{station}.json" url = f"https://dbf.finalrewind.org/z/{train_type}%20{train_ID}/{station}.json"
logger(f"Fetche {url}")
try: try:
response = requests.get(url) response = requests.get(url)
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
except requests.RequestException: except requests.RequestException as e:
logger(f"ReqestException Fehler: {e}")
return None return None
dep = data.get("departure", []) dep = data.get("departure", [])
if not dep: if not dep:
logger("Kein departure Feld gefunden")
return None return None
route_post = dep.get("route_post_diff") route_post = dep.get("route_post_diff")
if not route_post: if not route_post:
logger("Kein route_post_diff Feld gefunden")
return None return None
arrival_iso = route_post[-1].get("sched_arr") arrival_iso = route_post[-1].get("sched_arr")
if not arrival_iso: if not arrival_iso:
logger("Keine Ankunftszeit gefunden")
return None return None
return { return {