From 0c3d4f7653cb2fd14d33a5c42b1e09372bd9a945 Mon Sep 17 00:00:00 2001 From: mia Date: Wed, 3 Jun 2026 12:27:24 +0200 Subject: [PATCH] Enhance HafasClient with detailed departure information, line is currently NOT working --- backend.py | 53 ++++++++++++++++++++++++++++++++++++++++++++-- station_monitor.py | 8 +++---- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/backend.py b/backend.py index d5eb4e5..03bc604 100644 --- a/backend.py +++ b/backend.py @@ -49,9 +49,49 @@ class HafasClient: res = self.stationRequest(stationString) return [(station["name"], station["extId"]) for station in res["svcResL"][0]["res"]["match"]["locL"]] + def _format_time(self, timeValue): + if not timeValue: + return None + return datetime.strptime(timeValue, "%H%M%S").strftime("%H:%M") + def getArrDep(self, stationId, arrdep="DEP", count=1): res = self.arrDepRequest(stationId, arrdep, count) - return res["svcResL"][0]["res"]["jnyL"] + departures = [] + time_key = "aTimeS" if arrdep == "ARR" else "dTimeS" + time_key_real = "aTimeR" if arrdep == "ARR" else "dTimeR" + + for departure in res["svcResL"][0]["res"]["jnyL"]: + prod_list = departure.get("prodL") or [] + prod_index = departure.get("prodX") + if isinstance(prod_index, int) and 0 <= prod_index < len(prod_list): + product = prod_list[prod_index] + else: + product = prod_list[0] if prod_list else {} + stop = departure.get("stbStop", {}) + + # Raw JSON access pattern: + # - Name: departure["dirTxt"] + # - Line: departure["prodL"][0]["nameS"] or departure["prodL"][0]["prodCtx"]["line"] + # - Departure/arrival time: departure["stbStop"]["dTimeS"] or ["aTimeS"] + departures.append({ + "name": departure.get("dirTxt"), + "line": ( + product.get("nameS") + or product.get("number") + or product.get("name") + or product.get("prodCtx", {}).get("line") + or product.get("prodCtx", {}).get("name") + or product.get("prodCtx", {}).get("nameS") + ), + "departure_time": self._format_time(stop.get(time_key)), + "departure_time_raw": stop.get(time_key), + "departure_time_real": self._format_time(stop.get(time_key_real)), + "departure_time_real_raw": stop.get(time_key_real), + "jid": departure.get("jid"), + "raw": departure, + }) + + return departures def getTrip(self, departure): res = self.tripRequest(departure) @@ -74,6 +114,14 @@ class HafasClient: trip = self.getTrip(departures[0]["jid"]) __import__('pprint').pprint(trip) + def moniterTest(self): + stationInput = input("Enter Station Name: ") + station = self.getStationNames(stationInput) + print(station) + # departures = self.getArrDep(station[0][1], arrdep="DEP", count=1) + departures = self.arrDepRequest(station[0][1], arrdep="DEP", count=1) + print(departures) + # streq = self.stationRequest(station)["svcResL"][0]["res"]["match"]["locL"] # @@ -91,4 +139,5 @@ class HafasClient: if __name__ == '__main__': client = HafasClient() - client.runTests() + # client.runTests() + client.moniterTest() diff --git a/station_monitor.py b/station_monitor.py index 4450f4c..acd719b 100644 --- a/station_monitor.py +++ b/station_monitor.py @@ -66,10 +66,10 @@ def run_station_monitor(hafas: backend.HafasClient | None = None): inputBuffer.insert_text(f"\n {station[0]}") resultBuffer.text = f"Ergebnisse für: {results[0][1]}\n\n" station = results[0][1] if results else "Keine Ergebnisse gefunden." - result = hafas.getArrDep(station, arrdep="ARR", count=3) - inputBuffer.insert_line_below() - for entry in result: - resultBuffer.insert_text(f"\n {result[0]}") + departures = hafas.getArrDep(station, arrdep="ARR", count=3) + resultBuffer.text = "\n".join( + [resultBuffer.text.rstrip()] + [f" {entry['departure_time']} {entry['line']} {entry['name']}" for entry in departures] + ) else: app_state = "MONITOR"