mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-09-17 16:52:47 +00:00
transitous rewrite: fix validate_connection
This commit is contained in:
@@ -110,6 +110,7 @@ def get_random_stop_id() -> str | None:
|
||||
|
||||
if stop_name == assigned_station:
|
||||
stop_ids.clear()
|
||||
logger(f"Exact match found! Using {stop_name}")
|
||||
stop_ids[stop_id] = stop_name
|
||||
break
|
||||
|
||||
@@ -118,11 +119,11 @@ def get_random_stop_id() -> str | None:
|
||||
return None
|
||||
|
||||
stop_ids_list = list(stop_ids.keys())
|
||||
stops_string = ", ".join(stop_ids.values())
|
||||
if len(stop_ids_list) > 1:
|
||||
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")
|
||||
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
|
||||
|
||||
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")
|
||||
return None
|
||||
|
||||
|
||||
legs = data["legs"][0]
|
||||
end_time = legs["endTime"]
|
||||
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"]
|
||||
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)
|
||||
if not valid:
|
||||
return None
|
||||
|
||||
@@ -114,7 +114,7 @@ OPERATORS = {
|
||||
}
|
||||
|
||||
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 Stuttgart GmbH": OPERATORS["db_bawü"],
|
||||
"Arverio Baden-Württemberg": OPERATORS["db_bawü"],
|
||||
|
||||
@@ -92,7 +92,6 @@ async def voice_announcer(destination: str, voice_channel: discord.VoiceChannel)
|
||||
vc.play(audio_source, after=after_playing)
|
||||
return True
|
||||
|
||||
|
||||
async def _schedule_next_transfer(bot: discord.Bot, arrival_dt: datetime, voice_channel: discord.VoiceChannel, destination: str):
|
||||
now = datetime.now(LOCAL_TZ)
|
||||
|
||||
|
||||
+4
-4
@@ -27,8 +27,9 @@ def choose_connection() -> dict | None:
|
||||
|
||||
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)
|
||||
start_dt = datetime.fromisoformat(start_time.replace("Z", "+00:00"))
|
||||
|
||||
end_dt = datetime.fromisoformat(end_time.replace("Z", "+00:00"))
|
||||
if end_dt < now:
|
||||
@@ -36,13 +37,12 @@ def validate_connection(start_time: str, end_time: str, station_departure: str)
|
||||
return False
|
||||
|
||||
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):
|
||||
logger(f"Verbindung liegt zu weit in der Zukunft: {start_dt}", "error")
|
||||
return False
|
||||
|
||||
station_departure_dt = datetime.fromisoformat(station_departure.replace("Z", "+00:00"))
|
||||
trip_duration = (end_dt - station_departure_dt).total_seconds()
|
||||
departure_time_iso_dt = datetime.fromisoformat(departure_time_iso.replace("Z", "+00:00"))
|
||||
trip_duration = (end_dt - departure_time_iso_dt).total_seconds()
|
||||
trip_duration_minutes = str(timedelta(seconds=trip_duration))
|
||||
|
||||
min_duration = config.connections.min_duration
|
||||
|
||||
Reference in New Issue
Block a user