From 8a7282a97afc2cf721b73a35421be5f1fe1f1006 Mon Sep 17 00:00:00 2001 From: Kaaninchen <124433727+kaaninchen@users.noreply.github.com> Date: Mon, 20 Jul 2026 14:33:48 +0200 Subject: [PATCH] =?UTF-8?q?=C3=9Cberpr=C3=BCfe=20ob=20Bahnh=C3=B6fe=20exis?= =?UTF-8?q?tieren=20und=20lass=20den=20Bot=20auch=20ohne=20Blacklist=20Lis?= =?UTF-8?q?te=20weiterlaufen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 -- src/data/operators.py | 10 ++++++---- src/utils.py | 26 +++++++++++++++++++------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6be85a5..c7ab611 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,6 @@ pip install -r requirements.txt ### Config `$ mv config.json.example` - -Aktuell gibt es kein error handling für die config (kommt bald!!), also BITTE stell sicher dass du keine unerwarteten Daten eingegeben hast ``` { "token": "", // Discord Token diff --git a/src/data/operators.py b/src/data/operators.py index c92e1ae..be37cc0 100644 --- a/src/data/operators.py +++ b/src/data/operators.py @@ -25,6 +25,10 @@ OPERATORS = { "color": 0xEC0016, "slogan": ["Senk ju vor träwelling wis Deutsche Bahn.", "Bitte beachten Sie die umgekehrte Wagenreihung.", "Zurückbleiben bitte!", "Alle reden vom Wetter. Wir nicht.", "Die Bahn macht mobil.", "Grün abgefahren"] }, + "db_bayern": { + "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Bahnland_Bayern_Logo_2021.svg/500px-Bahnland_Bayern_Logo_2021.svg.png", + "color": 0x0095DB + }, "db_bawü": { "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Bwegt_Logo.svg/960px-Bwegt_Logo.svg.png", "color": 0xFFBF34, @@ -47,10 +51,6 @@ OPERATORS = { "logo": "https://cdn.discordapp.com/attachments/1383843132906537023/1528006572805062776/Arverio_Avi_Bayern_blau_RGB.png?ex=6a5cba83&is=6a5b6903&hm=5781ae6c372c92ab4f52409c6fc91e5014ac34ccf24db665ade052e8135bdde0&animated=true", # alternative nötig! "color": 0x0083BE }, - "Bayerische Regiobahn": { - "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Bahnland_Bayern_Logo_2021.svg/500px-Bahnland_Bayern_Logo_2021.svg.png", - "color": 0x0095DB - }, "Abellio Rail Mitteldeutschland GmbH": { "logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/Abellio_logo.svg/1920px-Abellio_logo.svg.png", "color": 0xD7002E @@ -85,6 +85,8 @@ OPERATOR_ALIASES = { "DB Regio AG Baden-Württemberg": OPERATORS["db_bawü"], "DB Regio Stuttgart GmbH": OPERATORS["db_bawü"], "Arverio Baden-Württemberg": OPERATORS["db_bawü"], + "Bayerische Regiobahn": OPERATORS["db_bayern"], + "DB Regio AG Bayern": OPERATORS["db_bayern"], "DB Fernverkehr AG": OPERATORS["db_allgemein"], "DB Regio AG NRW": OPERATORS["db_allgemein"], "DB Regio AG Nord": OPERATORS["db_allgemein"], diff --git a/src/utils.py b/src/utils.py index c8f0cca..21531a5 100644 --- a/src/utils.py +++ b/src/utils.py @@ -24,27 +24,38 @@ def _reload_operators_if_changed(): logger("operators.py wurde automatisch neu geladen (Änderungen erkannt)") def random_connection(): + available_stations = config["stations"].copy() while True: - station = random.choice(config["stations"]) + if not available_stations: + logger("Keine validen Bahnhöfe. Schlag den richtigen Bahnhofsnamen auf https://dbf.finalrewind.org/ nach", "fatal") + + station = random.choice(available_stations) url = f"https://dbf.finalrewind.org/{station}.json" + blacklist = config.get("blacklist", []) try: response = requests.get(url) response.raise_for_status() data = response.json() - except requests.RequestException: - continue + except requests.RequestException as e: + logger(f"Fehler beim aussuchen der Verbindung: {e}", "fatal") + return + if response.status_code == 300: + logger(f"Bahnhof '{station}' konnte nicht gefunden werden ({url})", "error") + available_stations.remove(station) + continue + departures = [ d for d in data.get("departures", []) if d.get("scheduledDeparture") and d.get("destination") != station - and not d.get("train", "").startswith(tuple(config["blacklist"])) + and not d.get("train", "").startswith(tuple(blacklist)) ] if not departures: continue - if config['random']: + if config['random'] is False: dep = random.choice(departures) else: dep = departures[0] @@ -118,7 +129,8 @@ def operator_metadata(operator): return operators_module.OPERATORS.get(operator, operators_module.OPERATORS["fallback"]) def logger(msg, log_type="info"): + status = log_type.upper() current_time = datetime.now().strftime('%X') - print(f"{current_time}: {log_type.upper()}: {msg}") - if log_type.lower() == "fatal": + print(f"{current_time}: {status}: {msg}") + if status == "FATAL": os._exit(1) \ No newline at end of file