mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-07-21 19:52:47 +00:00
Überprüfe ob Bahnhöfe existieren und lass den Bot auch ohne Blacklist Liste weiterlaufen
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"],
|
||||
|
||||
+19
-7
@@ -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)
|
||||
Reference in New Issue
Block a user