diff --git a/src/api/transitous.py b/src/api/transitous.py index 687a00d..14b14a7 100644 --- a/src/api/transitous.py +++ b/src/api/transitous.py @@ -130,10 +130,6 @@ def get_trip_details(random_connection: dict | None) -> dict | None: start_time = legs["startTime"] mode = legs["mode"] - is_valid = validate_connection(start_time, end_time) - if not is_valid: - return None - arrival = convert_iso_string(end_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 - + departure_time = start_time + for stop in legs["intermediateStops"]: 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: departure_time = stop["departure"] trip_details["departure"] = convert_iso_string(departure_time) 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: json.dump(data, f, indent=4, ensure_ascii=False) diff --git a/src/utils.py b/src/utils.py index 9fc05e3..b03de69 100644 --- a/src/utils.py +++ b/src/utils.py @@ -27,20 +27,27 @@ def choose_connection() -> dict | None: 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) - 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) - if end_dt < now: logger(f"Verbindung liegt bereits in der Vergangenheit: {start_dt}", "error") 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): 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() + 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 def convert_iso_string(isostring) -> str: