From eae7ca8f8717d30582c93a7da1ac4a88e1ea1660 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:04:20 +0200 Subject: [PATCH 01/17] Update station_helper.py --- deutsche_bahn_api/station_helper.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/deutsche_bahn_api/station_helper.py b/deutsche_bahn_api/station_helper.py index d5965cd..5c3ffb0 100644 --- a/deutsche_bahn_api/station_helper.py +++ b/deutsche_bahn_api/station_helper.py @@ -53,3 +53,8 @@ def find_stations_by_name(self, query: str) -> list[Station]: results.append(station) return results + + def find_station_by_id(self, query: int) -> list[Station]: + for station in self.stations_list: + if query == station.EVA_NR: + return station From 426d369ac85f451ed7f9352084999500d77bbb91 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:06:55 +0200 Subject: [PATCH 02/17] Update train_changes.py --- deutsche_bahn_api/train_changes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/deutsche_bahn_api/train_changes.py b/deutsche_bahn_api/train_changes.py index 32c0573..8a28a60 100644 --- a/deutsche_bahn_api/train_changes.py +++ b/deutsche_bahn_api/train_changes.py @@ -9,3 +9,5 @@ class TrainChanges: stations: str platform: str messages: list[Message] + arrival_cancelled: bool + departure_cancelled: bool From 3345249d67edd13fc183788457f3e27317bbf15b Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:08:33 +0200 Subject: [PATCH 03/17] Update timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index a2b67bf..843c049 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -114,12 +114,16 @@ def get_timetable_changes(self, trains: list) -> list[Train]: train_changes.stations = changes.attrib["cpth"] if "cp" in changes.attrib: train_changes.platform = changes.attrib["cp"] + if "cs" in changes.attrib: + train_changes.departure_cancelled = changes.attrib["cs"] if changes.tag == "ar": if "ct" in changes.attrib: train_changes.arrival = changes.attrib["ct"] if "cpth" in changes.attrib: train_changes.passed_stations = changes.attrib["cpth"] + if "cs" in changes.attrib: + train_changes.arrival_cancelled = changes.attrib["cs"] for message in changes: new_message = Message() From b7d7edca8f110ccf9e664c5b6e1a7ef1bf000001 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:13:31 +0200 Subject: [PATCH 04/17] Update train_changes.py --- deutsche_bahn_api/train_changes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deutsche_bahn_api/train_changes.py b/deutsche_bahn_api/train_changes.py index 8a28a60..cc53a7f 100644 --- a/deutsche_bahn_api/train_changes.py +++ b/deutsche_bahn_api/train_changes.py @@ -9,5 +9,5 @@ class TrainChanges: stations: str platform: str messages: list[Message] - arrival_cancelled: bool - departure_cancelled: bool + arrival_cancelled: str + departure_cancelled: str From de7543f65c6848e341aa8d9757a1216235fa5128 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Tue, 22 Oct 2024 00:28:00 +0200 Subject: [PATCH 05/17] Update timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index 843c049..c75f461 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -44,9 +44,9 @@ def get_timetable_xml(self, hour: Optional[int] = None, date: Optional[datetime] .format(response.status_code, response.text)) return response.text - def get_timetable(self, hour: Optional[int] = None) -> list[Train]: + def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = None) -> list[Train]: train_list: list[Train] = [] - trains = elementTree.fromstringlist(self.get_timetable_xml(hour)) + trains = elementTree.fromstringlist(self.get_timetable_xml(hour, date)) for train in trains: trip_label_object: dict[str, str] | None = None arrival_object: dict[str, str] | None = None From 77c3aeb0ed0b1497a352c132aaa2616ba01b60a7 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:13:03 +0200 Subject: [PATCH 06/17] Update timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index c75f461..da4da80 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -59,9 +59,9 @@ def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = N if train_details.tag == "ar": arrival_object = train_details.attrib - if not departure_object: - """ Arrival without department """ - continue +# if not departure_object: +# """ Arrival without department """ +# continue train_object: Train = Train() train_object.stop_id = train.attrib["id"] From 729ac30494a00fbb15989c8611ec31511fbeb388 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:18:28 +0200 Subject: [PATCH 07/17] Update timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index da4da80..1b59dbb 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -67,15 +67,23 @@ def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = N train_object.stop_id = train.attrib["id"] train_object.train_type = trip_label_object["c"] train_object.train_number = trip_label_object["n"] - train_object.platform = departure_object['pp'] - train_object.stations = departure_object['ppth'] - train_object.departure = departure_object['pt'] + if departure_object: + train_object.platform = departure_object['pp'] + train_object.stations = departure_object['ppth'] + train_object.departure = departure_object['pt'] + if "l" in departure_object: + train_object.train_line = departure_object['l'] + else: + train_object.platform = arrival_object['pp'] + train_object.stations = arrival_object['ppth'] + train_object.departure = arrival_object['pt'] + if "l" in arrival_object: + train_object.train_line = arrival_object['l'] if "f" in trip_label_object: train_object.trip_type = trip_label_object["f"] - if "l" in departure_object: - train_object.train_line = departure_object['l'] + if arrival_object: train_object.passed_stations = arrival_object['ppth'] @@ -122,6 +130,8 @@ def get_timetable_changes(self, trains: list) -> list[Train]: train_changes.arrival = changes.attrib["ct"] if "cpth" in changes.attrib: train_changes.passed_stations = changes.attrib["cpth"] + if "cp" in changes.attrib: + train_changes.platform = changes.attrib["cp"] if "cs" in changes.attrib: train_changes.arrival_cancelled = changes.attrib["cs"] From f8e194e342241c84c153570ad430fce17a98cb6e Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Tue, 22 Oct 2024 01:44:38 +0200 Subject: [PATCH 08/17] Update timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index 1b59dbb..514abdb 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -75,8 +75,6 @@ def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = N train_object.train_line = departure_object['l'] else: train_object.platform = arrival_object['pp'] - train_object.stations = arrival_object['ppth'] - train_object.departure = arrival_object['pt'] if "l" in arrival_object: train_object.train_line = arrival_object['l'] From f3909a5a0fe1ff44793fb5a07865207f92b357d3 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:18:30 +0100 Subject: [PATCH 09/17] Aktualisieren von api_authentication.py --- deutsche_bahn_api/api_authentication.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deutsche_bahn_api/api_authentication.py b/deutsche_bahn_api/api_authentication.py index ff6ff51..31cce7d 100644 --- a/deutsche_bahn_api/api_authentication.py +++ b/deutsche_bahn_api/api_authentication.py @@ -12,7 +12,8 @@ def test_credentials(self) -> bool: headers={ "DB-Api-Key": self.client_secret, "DB-Client-Id": self.client_id, - } + }, + verify=False ) return response.status_code == 200 From 418d0b506f25e120ec53d57eb1b942f8bf1fe0c7 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:19:17 +0100 Subject: [PATCH 10/17] Aktualisieren von timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index a2b67bf..f99db7d 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -32,7 +32,8 @@ def get_timetable_xml(self, hour: Optional[int] = None, date: Optional[datetime] response = requests.get( f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1" f"/plan/{self.station.EVA_NR}/{date_string}/{hour}", - headers=self.api_authentication.get_headers() + headers=self.api_authentication.get_headers(), + verify=False ) if response.status_code == 410: return self.get_timetable_xml(int(hour), datetime.now() + timedelta(days=1)) From 59107a8419fe7353cf94d05676b28f23617a8f85 Mon Sep 17 00:00:00 2001 From: Jens <11321022+SnejPro@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:21:45 +0100 Subject: [PATCH 11/17] Aktualisieren von timetable_helper.py --- deutsche_bahn_api/timetable_helper.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index f99db7d..c52588b 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -89,7 +89,8 @@ def get_timetable(self, hour: Optional[int] = None) -> list[Train]: def get_timetable_changes(self, trains: list) -> list[Train]: response = requests.get( f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/{self.station.EVA_NR}", - headers=self.api_authentication.get_headers() + headers=self.api_authentication.get_headers(), + verify=False ) changed_trains = elementTree.fromstringlist(response.text) From 50b4a4a2bd50fbbf37ba6cc5d13fed088138fb4d Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:06:02 +0000 Subject: [PATCH 12/17] fix: remove verify=False Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- deutsche_bahn_api/api_authentication.py | 1 - deutsche_bahn_api/timetable_helper.py | 1 - 2 files changed, 2 deletions(-) diff --git a/deutsche_bahn_api/api_authentication.py b/deutsche_bahn_api/api_authentication.py index 31cce7d..61682c9 100644 --- a/deutsche_bahn_api/api_authentication.py +++ b/deutsche_bahn_api/api_authentication.py @@ -13,7 +13,6 @@ def test_credentials(self) -> bool: "DB-Api-Key": self.client_secret, "DB-Client-Id": self.client_id, }, - verify=False ) return response.status_code == 200 diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index b5238a8..3c558c5 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -33,7 +33,6 @@ def get_timetable_xml(self, hour: Optional[int] = None, date: Optional[datetime] f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1" f"/plan/{self.station.EVA_NR}/{date_string}/{hour}", headers=self.api_authentication.get_headers(), - verify=False ) if response.status_code == 410: return self.get_timetable_xml(int(hour), datetime.now() + timedelta(days=1)) From c140ad71c3ec7eacb0ab5d3812b34d47ac945b96 Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:10:22 +0000 Subject: [PATCH 13/17] add: StationHelper.find_stations_by_id and StationHelper.find_station_by_id Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- deutsche_bahn_api/station_helper.py | 17 ++++++++++++++++- deutsche_bahn_api/timetable_helper.py | 3 +-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/deutsche_bahn_api/station_helper.py b/deutsche_bahn_api/station_helper.py index 5c3ffb0..9b6e111 100644 --- a/deutsche_bahn_api/station_helper.py +++ b/deutsche_bahn_api/station_helper.py @@ -54,7 +54,22 @@ def find_stations_by_name(self, query: str) -> list[Station]: return results + def find_stations_by_id(self, query: int) -> list[Station]: + results: list[Station] = [] + + for station in self.stations_list: + if query == station.EVA_NR: + results.append(station) + + return results + def find_station_by_id(self, query: int) -> list[Station]: + results: list[Station] = [] for station in self.stations_list: if query == station.EVA_NR: - return station + results.append(station) + + if len(results)>1: + raise Exception("More than one station with id '%s' found" % query) + else: + return results[0] diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index 3c558c5..81b626b 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -94,8 +94,7 @@ def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = N def get_timetable_changes(self, trains: list) -> list[Train]: response = requests.get( f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/fchg/{self.station.EVA_NR}", - headers=self.api_authentication.get_headers(), - verify=False + headers=self.api_authentication.get_headers() ) changed_trains = elementTree.fromstringlist(response.text) From 8efce9e45d439edf8aa59343cdb505970c33e845 Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:27:08 +0000 Subject: [PATCH 14/17] fix: remove deprecation warning of package mpu replaced mpu with package haversine Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- deutsche_bahn_api/station_helper.py | 4 ++-- setup.py | 2 +- test.py | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 test.py diff --git a/deutsche_bahn_api/station_helper.py b/deutsche_bahn_api/station_helper.py index 9b6e111..a18a030 100644 --- a/deutsche_bahn_api/station_helper.py +++ b/deutsche_bahn_api/station_helper.py @@ -1,6 +1,6 @@ import json import pkgutil -import mpu +from haversine import haversine from deutsche_bahn_api.station import Station @@ -37,7 +37,7 @@ def find_stations_by_lat_long(self, target_lat: float, target_long: float, radiu for station in self.stations_list: lat_long: dict[str, float] = normalize_lat_or_long_from_station(station) - distance = mpu.haversine_distance( + distance = haversine( (lat_long['lat'], lat_long['long']), (target_lat, target_long)) if distance < radius: diff --git a/setup.py b/setup.py index ad339bc..22eeaae 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ long_description_content_type="text/markdown", url="https://github.com/Tutorialwork/deutsche_bahn_api", packages=find_packages(), - install_requires=["mpu", "requests"], + install_requires=["haversine", "requests"], package_data={"deutsche_bahn_api": ["static/*"]}, classifiers=[ "Programming Language :: Python :: 3", diff --git a/test.py b/test.py new file mode 100644 index 0000000..1376390 --- /dev/null +++ b/test.py @@ -0,0 +1,7 @@ +import deutsche_bahn_api.api_authentication as dba +import deutsche_bahn_api.timetable_helper as dbt +import deutsche_bahn_api.station_helper as dbc_s + +api = dba.ApiAuthentication("1ac3d18ab81777747efe5f41b6b78ca5", "076006a97b68f3fd1c914ede788c443f") +station_helper = dbc_s.StationHelper() +print(station_helper.find_stations_by_lat_long(48.209166953908536, 11.34949095056929, 2)) \ No newline at end of file From 77090f8a9e8033e23794970da5415b3d25e0d7ed Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:32:41 +0000 Subject: [PATCH 15/17] sync with upstream Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- deutsche_bahn_api/timetable_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index 81b626b..ac28996 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -46,7 +46,7 @@ def get_timetable_xml(self, hour: Optional[int] = None, date: Optional[datetime] def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = None) -> list[Train]: train_list: list[Train] = [] - trains = elementTree.fromstringlist(self.get_timetable_xml(hour, date)) + trains = elementTree.fromstringlist(self.get_timetable_xml(hour=hour, date=date)) for train in trains: trip_label_object: dict[str, str] | None = None arrival_object: dict[str, str] | None = None From 80eca1552d105addf8cbf419de7a5b1d6c962135 Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:36:03 +0000 Subject: [PATCH 16/17] fix: remove test file Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- test.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index 1376390..0000000 --- a/test.py +++ /dev/null @@ -1,7 +0,0 @@ -import deutsche_bahn_api.api_authentication as dba -import deutsche_bahn_api.timetable_helper as dbt -import deutsche_bahn_api.station_helper as dbc_s - -api = dba.ApiAuthentication("1ac3d18ab81777747efe5f41b6b78ca5", "076006a97b68f3fd1c914ede788c443f") -station_helper = dbc_s.StationHelper() -print(station_helper.find_stations_by_lat_long(48.209166953908536, 11.34949095056929, 2)) \ No newline at end of file From fdcd37d451bb1dab105e3b9a1dd2d52457f8c130 Mon Sep 17 00:00:00 2001 From: SnejPro <11321022+SnejPro@users.noreply.github.com> Date: Tue, 19 Aug 2025 06:40:31 +0000 Subject: [PATCH 17/17] add: comments Signed-off-by: SnejPro <11321022+SnejPro@users.noreply.github.com> --- deutsche_bahn_api/timetable_helper.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/deutsche_bahn_api/timetable_helper.py b/deutsche_bahn_api/timetable_helper.py index ac28996..243c301 100644 --- a/deutsche_bahn_api/timetable_helper.py +++ b/deutsche_bahn_api/timetable_helper.py @@ -59,20 +59,18 @@ def get_timetable(self, hour: Optional[int] = None, date: Optional[datetime] = N if train_details.tag == "ar": arrival_object = train_details.attrib -# if not departure_object: -# """ Arrival without department """ -# continue - train_object: Train = Train() train_object.stop_id = train.attrib["id"] train_object.train_type = trip_label_object["c"] train_object.train_number = trip_label_object["n"] + # If Stop has departure_object, get some informations from it if departure_object: train_object.platform = departure_object['pp'] train_object.stations = departure_object['ppth'] train_object.departure = departure_object['pt'] if "l" in departure_object: train_object.train_line = departure_object['l'] + # If not, get them from arrival_object else: train_object.platform = arrival_object['pp'] if "l" in arrival_object: