transitous rewrite: minimal route length

This commit is contained in:
Kaaninchen
2026-08-16 14:50:11 +02:00
parent a4d9cb36f0
commit 10e1ea8ade
2 changed files with 19 additions and 11 deletions
+6 -5
View File
@@ -130,10 +130,6 @@ def get_trip_details(random_connection: dict | None) -> dict | None:
start_time = legs["startTime"] start_time = legs["startTime"]
mode = legs["mode"] mode = legs["mode"]
is_valid = validate_connection(start_time, end_time)
if not is_valid:
return None
arrival = convert_iso_string(end_time) arrival = convert_iso_string(end_time)
departure = convert_iso_string(start_time) departure = convert_iso_string(start_time)
@@ -159,15 +155,20 @@ def get_trip_details(random_connection: dict | None) -> dict | None:
} }
trip_details["stops"][train_from] = departure trip_details["stops"][train_from] = departure
departure_time = start_time
for stop in legs["intermediateStops"]: for stop in legs["intermediateStops"]:
stop_arrival = convert_iso_string(stop["arrival"]) stop_arrival = convert_iso_string(stop["arrival"])
trip_details["stops"][stop["name"]] = stop_arrival trip_details["stops"][stop["name"]] = stop_arrival # not sure if that actually works but im too tired to question it
if stop.get("name") == from_station: if stop.get("name") == from_station:
departure_time = stop["departure"] departure_time = stop["departure"]
trip_details["departure"] = convert_iso_string(departure_time) trip_details["departure"] = convert_iso_string(departure_time)
trip_details["stops"][goes_to] = arrival trip_details["stops"][goes_to] = arrival
valid = validate_connection(start_time, end_time, departure_time)
if not valid:
return None
with open('data.json', 'w') as f: with open('data.json', 'w') as f:
json.dump(data, f, indent=4, ensure_ascii=False) json.dump(data, f, indent=4, ensure_ascii=False)
+11 -4
View File
@@ -27,20 +27,27 @@ def choose_connection() -> dict | None:
return trip return trip
def validate_connection(start_time: str, end_time: str) -> bool: def validate_connection(start_time: str, end_time: str, station_departure: 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"))
max_wait_time = config.get("max_wait_time", 6)
end_dt = datetime.fromisoformat(end_time.replace("Z", "+00:00"))
if end_dt < now: if end_dt < now:
logger(f"Verbindung liegt bereits in der Vergangenheit: {start_dt}", "error") logger(f"Verbindung liegt bereits in der Vergangenheit: {start_dt}", "error")
return False return False
max_wait_time = config.get("max_wait_time", 6)
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"))
trip_duration = (end_dt - station_departure_dt).total_seconds()
min_duration = config.get("min_duration", 600)
if trip_duration < min_duration:
logger(f"Verbindung ist zu kurz: Nur {trip_duration} Sekunden lang ({min_duration} gewollt)")
return False
return True return True
def convert_iso_string(isostring) -> str: def convert_iso_string(isostring) -> str: