transitous rewrite: fix validate_connection

This commit is contained in:
Kaaninchen
2026-08-17 18:29:06 +02:00
parent 60d4936dda
commit 9cd1f54258
4 changed files with 9 additions and 14 deletions
+4 -8
View File
@@ -110,6 +110,7 @@ def get_random_stop_id() -> str | None:
if stop_name == assigned_station: if stop_name == assigned_station:
stop_ids.clear() stop_ids.clear()
logger(f"Exact match found! Using {stop_name}")
stop_ids[stop_id] = stop_name stop_ids[stop_id] = stop_name
break break
@@ -118,11 +119,11 @@ def get_random_stop_id() -> str | None:
return None return None
stop_ids_list = list(stop_ids.keys()) stop_ids_list = list(stop_ids.keys())
stops_string = ", ".join(stop_ids.values())
if len(stop_ids_list) > 1: if len(stop_ids_list) > 1:
logger(f"No station associated as '{assigned_station}', choosing random from similar named station") logger(f"No station associated as '{assigned_station}', choosing random from similar named station")
logger(f"Run `python run main.py stations` to get exact station names") logger(f"Run `python run main.py stations` to get exact station names")
chosen_stop_id = random.choice(stop_ids_list) chosen_stop_id, chosen_station = random.choice(list(stop_ids.items()))
logger(f"Assigned Station: {chosen_station}")
return chosen_stop_id return chosen_stop_id
def get_random_connection(stop_id: str) -> str | None: def get_random_connection(stop_id: str) -> str | None:
@@ -200,6 +201,7 @@ def get_trip_details(random_connection: dict | None) -> dict | None:
logger(f"An error occured while trying to get the route details: {e}", "error") logger(f"An error occured while trying to get the route details: {e}", "error")
return None return None
legs = data["legs"][0] legs = data["legs"][0]
end_time = legs["endTime"] end_time = legs["endTime"]
from_station = random_connection["from_station"] from_station = random_connection["from_station"]
@@ -254,12 +256,6 @@ def get_trip_details(random_connection: dict | None) -> dict | None:
departure_time_iso = stop["departure"] departure_time_iso = stop["departure"]
trip_details["departure"] = parse_iso(departure_time_iso).strftime("%H:%M") trip_details["departure"] = parse_iso(departure_time_iso).strftime("%H:%M")
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) valid = validate_connection(start_time, end_time, departure_time_iso)
if not valid: if not valid:
return None return None
+1 -1
View File
@@ -114,7 +114,7 @@ OPERATORS = {
} }
OPERATOR_ALIASES = { OPERATOR_ALIASES = {
"Arverio BAyern GmbH": OPERATORS["Arverio Bayern"], "Arverio Bayern GmbH": OPERATORS["Arverio Bayern"],
"DB Regio AG Baden-Württemberg": OPERATORS["db_bawü"], "DB Regio AG Baden-Württemberg": OPERATORS["db_bawü"],
"DB Regio Stuttgart GmbH": OPERATORS["db_bawü"], "DB Regio Stuttgart GmbH": OPERATORS["db_bawü"],
"Arverio Baden-Württemberg": OPERATORS["db_bawü"], "Arverio Baden-Württemberg": OPERATORS["db_bawü"],
-1
View File
@@ -92,7 +92,6 @@ async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel)
vc.play(audio_source, after=after_playing) vc.play(audio_source, after=after_playing)
return True return True
async def _schedule_next_transfer(bot: discord.Bot, arrival_dt: datetime, voice_channel: discord.VoiceChannel, destination: str): async def _schedule_next_transfer(bot: discord.Bot, arrival_dt: datetime, voice_channel: discord.VoiceChannel, destination: str):
now = datetime.now(LOCAL_TZ) now = datetime.now(LOCAL_TZ)
+4 -4
View File
@@ -27,8 +27,9 @@ def choose_connection() -> dict | None:
return trip return trip
def validate_connection(start_time: str, end_time: str, station_departure: str) -> bool: def validate_connection(start_time: str, end_time: str, departure_time_iso: str) -> bool:
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
start_dt = datetime.fromisoformat(start_time.replace("Z", "+00:00"))
end_dt = datetime.fromisoformat(end_time.replace("Z", "+00:00")) end_dt = datetime.fromisoformat(end_time.replace("Z", "+00:00"))
if end_dt < now: if end_dt < now:
@@ -36,13 +37,12 @@ def validate_connection(start_time: str, end_time: str, station_departure: str)
return False return False
max_wait_time = config.connections.max_wait_time max_wait_time = config.connections.max_wait_time
start_dt = datetime.fromisoformat(start_time.replace("Z", "+00:00"))
if start_dt > now + timedelta(hours=max_wait_time): if start_dt > now + timedelta(hours=max_wait_time):
logger(f"Verbindung liegt zu weit in der Zukunft: {start_dt}", "error") logger(f"Verbindung liegt zu weit in der Zukunft: {start_dt}", "error")
return False return False
station_departure_dt = datetime.fromisoformat(station_departure.replace("Z", "+00:00")) departure_time_iso_dt = datetime.fromisoformat(departure_time_iso.replace("Z", "+00:00"))
trip_duration = (end_dt - station_departure_dt).total_seconds() trip_duration = (end_dt - departure_time_iso_dt).total_seconds()
trip_duration_minutes = str(timedelta(seconds=trip_duration)) trip_duration_minutes = str(timedelta(seconds=trip_duration))
min_duration = config.connections.min_duration min_duration = config.connections.min_duration