From 8a4423f546ef948727bef163186f9b4c9bf5f496 Mon Sep 17 00:00:00 2001 From: zaanposni Date: Sun, 16 Jun 2024 16:19:46 +0200 Subject: [PATCH 1/2] added cancellation information to departure and arrival. --- readme.md | 26 +++++++++++ vvspy/models/arrival.py | 98 +++++++++++++++++++++------------------ vvspy/models/departure.py | 16 +++++-- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/readme.md b/readme.md index ddf2b8c..f370bc7 100644 --- a/readme.md +++ b/readme.md @@ -31,6 +31,20 @@ for dep in deps: print(dep) # [11:47] [RB17]: Stuttgart Hauptbahnhof (oben) - Pforzheim Hauptbahnhof ``` +- Detect cancellations in upcoming departures/arrivals: + +```python +from vvspy import get_departures +from vvspy.enums import Station + +arrivals = get_departures(Station.VAIHINGEN, limit=5) + +for arrival in arrivals: + if arrival.cancelled: + print(f"Alarm! The train at {arrival.real_datetime} has been cancelled!") + # Check arrival.stop_infos and arrival.line_infos for more information +``` + - Get complete trip info between two stations (including interchanges): ```python @@ -65,6 +79,18 @@ for dep in deps: print(f"Departure of S4 at {dep.real_datetime}") ``` +- Filter for specific platforms: + +```python +from vvspy import get_departures +from vvspy.enums import Station + +deps = get_departures(Station.HAUPTBAHNHOF__TIEF) +for dep in deps: + if dep.platform == "101": + print(f"Departure of {dep.serving_line.number} to {dep.serving_line.direction} on {dep.platform_name} at {dep.real_datetime}") +``` + ### Get your station id See: [#64][station_id_issue_url] diff --git a/vvspy/models/arrival.py b/vvspy/models/arrival.py index 296cbf7..0810766 100644 --- a/vvspy/models/arrival.py +++ b/vvspy/models/arrival.py @@ -7,51 +7,57 @@ class Arrival: r""" - Arrival object from a arrival request of one station. + Arrival object from a arrival request of one station. - Attributes - ----------- + Attributes + ----------- + + raw :class:`dict` + Raw dict received by the API. + stop_id :class:`str` + Station_id of the arrival. + cancelled :class:`bool` + If the arrival is cancelled. + realtime_status :class:`str` + Realtime status of the arrival. + x :class:`str` + Coordinates of the station. + y :class:`str` + Coordinates of the station. + map_name :class:`str` + Map name the API works on. + area :class:`str` + The area of the station (unsure atm) + platform :class:`str` + Platform / track of the arrival. + platform_name :class:`str` + name of the platform. + stop_name :class:`str` + name of the station. + name_wo :class:`str` + name of the station. + countdown :class:`int` + minutes until arrival. + datetime :class:`datetime.datetime` + Planned arrival datetime. + real_datetime :class:`datetime.datetime` + Estimated arrival datetime (equal to ``self.datetime`` if no realtime data is available). + delay :class:`int` + Delay of arrival in minutes. + serving_line :class:`ServingLine` + line of the incoming arrival. + operator :class:`LineOperator` + Operator of the incoming arrival. + stop_infos Optional[:class:`dict`] + All related info to the station (e.g. maintenance work). + line_infos Optional[:class:`dict`] + All related info to the station (e.g. maintenance work). + """ - raw :class:`dict` - Raw dict received by the API. - stop_id :class:`str` - Station_id of the arrival. - x :class:`str` - Coordinates of the station. - y :class:`str` - Coordinates of the station. - map_name :class:`str` - Map name the API works on. - area :class:`str` - The area of the station (unsure atm) - platform :class:`str` - Platform / track of the arrival. - platform_name :class:`str` - name of the platform. - stop_name :class:`str` - name of the station. - name_wo :class:`str` - name of the station. - countdown :class:`int` - minutes until arrival. - datetime :class:`datetime.datetime` - Planned arrival datetime. - real_datetime :class:`datetime.datetime` - Estimated arrival datetime (equal to ``self.datetime`` if no realtime data is available). - delay :class:`int` - Delay of arrival in minutes. - serving_line :class:`ServingLine` - line of the incoming arrival. - operator :class:`LineOperator` - Operator of the incoming arrival. - stop_infos Optional[:class:`dict`] - All related info to the station (e.g. maintenance work). - line_infos Optional[:class:`dict`] - All related info to the station (e.g. maintenance work). - """ - def __init__(self, **kwargs): self.stop_id = kwargs.get("stopID") + self.realtime_status = kwargs.get("realtimeStatus") + self.cancelled = self.realtime_status == "ARRIVAL_CANCELLED" self.x = kwargs.get("x") self.y = kwargs.get("y") self.map_name = kwargs.get("mapName") @@ -70,7 +76,7 @@ def __init__(self, **kwargs): month=int(dt.get("month", datetime.now().month)), day=int(dt.get("day", datetime.now().day)), hour=int(dt.get("hour", datetime.now().hour)), - minute=int(dt.get("minute", datetime.now().minute)) + minute=int(dt.get("minute", datetime.now().minute)), ) except ValueError: pass @@ -84,7 +90,7 @@ def __init__(self, **kwargs): month=int(r_dt.get("month", datetime.now().month)), day=int(r_dt.get("day", datetime.now().day)), hour=int(r_dt.get("hour", datetime.now().hour)), - minute=int(r_dt.get("minute", datetime.now().minute)) + minute=int(r_dt.get("minute", datetime.now().minute)), ) except ValueError: pass @@ -101,7 +107,11 @@ def __init__(self, **kwargs): self.line_infos = kwargs.get("lineInfos") def __str__(self): - pre = "[Delayed] " if self.delay else "" + pre = "" + if self.delay: + pre = "[Delayed] " + if self.cancelled: + pre = "[Cancelled] " if self.real_datetime.date() == datetime.now().date(): return f"{pre}[{str(self.real_datetime.strftime('%H:%M'))}] {self.serving_line}" return f"{pre}[{str(self.real_datetime)}] {self.serving_line}" diff --git a/vvspy/models/departure.py b/vvspy/models/departure.py index 1f1167e..f204c9e 100644 --- a/vvspy/models/departure.py +++ b/vvspy/models/departure.py @@ -16,6 +16,10 @@ class Departure: Raw dict received by the API. stop_id :class:`str` Station_id of the departure. + cancelled :class:`bool` + If the departure is cancelled. + realtime_status :class:`str` + Realtime status of the departure. x :class:`str` Coordinates of the station. y :class:`str` @@ -54,6 +58,8 @@ def __init__(self, **kwargs): self.stop_id = kwargs.get("stopID") self.x = kwargs.get("x") self.y = kwargs.get("y") + self.realtime_status = kwargs.get("realtimeStatus") + self.cancelled = self.realtime_status == "DEPARTURE_CANCELLED" self.map_name = kwargs.get("mapName") self.area = kwargs.get("area") self.platform = kwargs.get("platform") @@ -70,7 +76,7 @@ def __init__(self, **kwargs): month=int(dt.get("month", datetime.now().month)), day=int(dt.get("day", datetime.now().day)), hour=int(dt.get("hour", datetime.now().hour)), - minute=int(dt.get("minute", datetime.now().minute)) + minute=int(dt.get("minute", datetime.now().minute)), ) except ValueError: pass @@ -84,7 +90,7 @@ def __init__(self, **kwargs): month=int(r_dt.get("month", datetime.now().month)), day=int(r_dt.get("day", datetime.now().day)), hour=int(r_dt.get("hour", datetime.now().hour)), - minute=int(r_dt.get("minute", datetime.now().minute)) + minute=int(r_dt.get("minute", datetime.now().minute)), ) except ValueError: pass @@ -101,7 +107,11 @@ def __init__(self, **kwargs): self.line_infos = kwargs.get("lineInfos") def __str__(self): - pre = "[Delayed] " if self.delay else "" + pre = "" + if self.delay: + pre = "[Delayed] " + if self.cancelled: + pre = "[Cancelled] " if self.real_datetime.date() == datetime.now().date(): return f"{pre}[{str(self.real_datetime.strftime('%H:%M'))}] {self.serving_line}" return f"{pre}[{str(self.real_datetime)}] {self.serving_line}" From ceffd864af10a53d6c30dcf3dc103afdbc48f43d Mon Sep 17 00:00:00 2001 From: zaanposni Date: Sun, 16 Jun 2024 16:20:04 +0200 Subject: [PATCH 2/2] bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 85081f9..f8800dd 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="vvspy", - version="2.1.0", + version="2.2.0", license="MIT", description="API Wrapper for VVS (Verkehrsverbund Stuttgart)", author="zaanposni",