From ed449472774c7037395c88ef49423ef0860f9fdc Mon Sep 17 00:00:00 2001 From: profitroll Date: Fri, 7 Oct 2022 13:25:33 +0200 Subject: [PATCH] him_search fully implemented --- pyrmv/classes/Client.py | 105 +++++++++++++++++++++++++++++++++++++--- pyrmv/raw/him_search.py | 9 ++++ test.py | 3 +- 3 files changed, 110 insertions(+), 7 deletions(-) diff --git a/pyrmv/classes/Client.py b/pyrmv/classes/Client.py index 92ad2ac..042de9a 100644 --- a/pyrmv/classes/Client.py +++ b/pyrmv/classes/Client.py @@ -9,6 +9,7 @@ from pyrmv.raw.stop_by_name import stop_by_name as raw_stop_by_name from pyrmv.raw.stop_by_coords import stop_by_coords as raw_stop_by_coords from pyrmv.raw.trip_find import trip_find as raw_trip_find from pyrmv.raw.trip_recon import trip_recon as raw_trip_recon +from pyrmv.raw.him_search import him_search as raw_him_search from pyrmv.utility.find_exception import find_exception from pyrmv.errors.not_ready import NotReadyYetError @@ -146,20 +147,112 @@ class Client(): channels: list = None, companies: list = None, lines: list = None, - lineids: list = None, - stations: list = None, + line_ids: list = None, + stations: Union[list, List[Stop]] = None, station_from: Union[str, Stop] = None, station_to: Union[str, Stop] = None, - bothways: bool = None, - trainnames: list = None, + both_ways: bool = None, + train_names: list = None, search_mode: Literal[SearchMode.MATCH, SearchMode.NOMATCH, SearchMode.TFMATCH] = None, affected_journey_mode: Literal[AffectedJourneyMode.ALL, AffectedJourneyMode.OFF] = None, affected_journey_stop_mode: Literal[AffectedJourneyStopMode.ALL, AffectedJourneyStopMode.IMP, AffectedJourneyStopMode.OFF] = None, priority_min: int = None, priority_max: int = None - ) -> None: #List[Message]: + ) -> List[Message]: + """The him_search method will deliver a list of HIM messages if matched by the given criteria as + well as affected products if any. - raise NotReadyYetError() + More detailed request is available as `raw.him_search()`, however returns `dict` instead of `List[Message]`. + + ### Args: + * date_begin (`Union[str, datetime]`, optional): Sets the event period start date. Defaults to `None`. + * date_end (`Union[str, datetime]`, optional): Sets the event period end date. Defaults to `None`. + * time_begin (`Union[str, datetime]`, optional): Sets the event period start time. Defaults to `None`. + * time_end (`Union[str, datetime]`, optional): Sets the event period end time. Defaults to `None`. + * weekdays (`Union[str, OrderedDict[str, bool]]`, optional): Bitmask or an OrderedDict for validity of HIM messages based on weekdays. OrderedDict must be formatted as follows: `OrderedDict(Monday=bool, Tuesday=bool, Wednesday=bool, Thursday=bool, Friday=bool, Saturday=bool, Sunday=bool)`. Each character of a bitmask represents a weekday starting on monday. Example: Only find HIM Messages valid from monday to friday: `1111100`. Defaults to `None`. + * ids (`list`, optional): List of HIM message IDs as a list or separated by comma. Defaults to `None`. + * operators (`list`, optional): List of operators as a list or separated by comma. Defaults to `None`. + * categories (`list`, optional): List of train categories as a list or separated by comma. Defaults to `None`. + * channels (`list`, optional): List of channels as a list or separated by comma. Defaults to `None`. + * companies (`list`, optional): List of companies as a list or separated by comma. Defaults to `None`. + * lines (`list`, optional): Only HIM messages for the given line are part of the result. To filter multiple lines, provide them as a list or separate the codes by comma. Defaults to `None`. + * line_ids (`list`, optional): Only HIM messages for the given line (identified by its line ID) are part of the result. To filter multiple lines, provide them as a list or separate the line IDs by comma. Defaults to `None`. + * stations (`Union[list, List[Stop]]`, optional): List of (external) station ids or a list of `Stop` objects to be filtered for as a list or separated by comma. Defaults to `None`. + * station_from (`Union[str, Stop]`, optional): Filter messages by line segment starting at this station given as (external) station id or as a `Stop` object. Defaults to `None`. + * station_to (`Union[str, Stop]`, optional): Filter messages by line segment travelling in direction of this station given as (external) station id or as a `Stop` object. Defaults to `None`. + * both_ways (`bool`, optional): If enabled, messages in both directions - from 'station_from' to 'station_to' as well as from 'station_to' to 'station_from' are returned. Defaults to `None`. + * train_names (`list`, optional): List of train name to be filtered for seperated by comma. Defaults to `None`. + * search_mode (`Literal[SearchMode.MATCH, SearchMode.NOMATCH, SearchMode.TFMATCH]`, optional): HIM search mode. `SearchMode.NOMATCH` iterate over all HIM messages available. `SearchMode.MATCH` iterate over all trips to find HIM messages. `SearchMode.TFMATCH` uses filters defined `metas` parameter. Defaults to `None`. + * affected_journey_mode (`Literal[AffectedJourneyMode.ALL, AffectedJourneyMode.OFF]`, optional): Define how to return affected journeys `AffectedJourneyMode.OFF`: do not return affected journeys. `AffectedJourneyMode.ALL`: return affected journeys. Defaults to `None`. + * affected_journey_stop_mode (`Literal[AffectedJourneyStopMode.ALL, AffectedJourneyStopMode.IMP, AffectedJourneyStopMode.OFF]`, optional): Define how to return stops of affected journeys. `AffectedJourneyStopMode.IMP`: return important stops of affected journeys. `AffectedJourneyStopMode.OFF`: do not return stops of affected journeys. `AffectedJourneyStopMode.ALL`: return all affected stops of affected journeys. Defaults to `None`. + * priority_min (`int`, optional): Filter for HIM messages having at least this priority. Defaults to `None`. + * priority_max (`int`, optional): Filter for HIM messages having this priority as maximum. Defaults to `None`. + + ### Returns: + * List[Message]: List of `Message` objects. Empty list if none found. + """ + + if isinstance(station_from, Stop): + station_from = station_from.ext_id + + if isinstance(station_to, Stop): + station_to = station_to.ext_id + + new_stations = [] + for stop in stations: + if isinstance(stop, Stop): + new_stations.append(stop.ext_id) + else: + new_stations.append(stop) + + if search_mode == None: + search_mode = None + else: + search_mode = search_mode.code + + if affected_journey_mode == None: + affected_journey_mode = None + else: + affected_journey_mode = affected_journey_mode.code + + if affected_journey_stop_mode == None: + affected_journey_stop_mode = None + else: + affected_journey_stop_mode = affected_journey_stop_mode.code + + messages = [] + messages_raw = raw_him_search( + accessId=self.access_id, + dateB=date_begin, + dateE=date_end, + timeB=time_begin, + timeE=time_end, + weekdays=weekdays, + himIds=ids, + operators=operators, + categories=categories, + channels=channels, + companies=companies, + lines=lines, + lineids=line_ids, + stations=new_stations, + fromstation=station_from, + tostation=station_to, + bothways=both_ways, + trainnames=train_names, + searchmode=search_mode, + affectedJourneyMode=affected_journey_mode, + affectedJourneyStopMode=affected_journey_stop_mode, + maxprio=priority_max, + minprio=priority_min + ) + + find_exception(messages_raw) + + for message in messages_raw["Message"]: + messages.append(Message(message)) + + return messages def journey_detail(self, id: str, diff --git a/pyrmv/raw/him_search.py b/pyrmv/raw/him_search.py index 5480315..db4976b 100644 --- a/pyrmv/raw/him_search.py +++ b/pyrmv/raw/him_search.py @@ -117,6 +117,15 @@ def him_search(accessId: str, payload[str(var)] = weekdays_bitmask(val) else: payload[str(var)] = val + elif str(var) == "searchmode": + if val != None: + payload["searchmode"] = val.upper() + elif str(var) == "affectedJourneyMode": + if val != None: + payload["affectedJourneyMode"] = val.upper() + elif str(var) == "affectedJourneyStopMode": + if val != None: + payload["affectedJourneyStopMode"] = val.upper() elif str(var) not in ["json", "headers", "payload"]: if val != None: payload[str(var)] = val diff --git a/test.py b/test.py index 6f57928..005e214 100644 --- a/test.py +++ b/test.py @@ -64,7 +64,7 @@ client = pyrmv.Client(key) test("raw_board_arrival", pyrmv.raw.board_arrival(key, id="A=1@O=Offenbach (Main)-Tempelsee Wilhelm-Schramm-Straße@X=8783648@Y=50083822@U=80@L=3008012@", maxJourneys=5), raw=True) test("raw_board_departure", pyrmv.raw.board_departure(key, id="A=1@O=Offenbach (Main)-Tempelsee Wilhelm-Schramm-Straße@X=8783648@Y=50083822@U=80@L=3008012@", maxJourneys=5), raw=True) -test("raw_him_search", pyrmv.raw.him_search(key, dateE=datetime.now()+timedelta(days=180), timeE=datetime.now()+timedelta(days=180), lines=["S1", "S9"], bothways=True, maxprio="1"), raw=True) +test("raw_him_search", pyrmv.raw.him_search(key, dateE=datetime.now()+timedelta(days=10), timeE=datetime.now()+timedelta(days=10), minprio=2, trainnames=["S9"]), raw=True) test("raw_journey_detail", pyrmv.raw.journey_detail(key, id="2|#VN#1#ST#1664906549#PI#0#ZI#12709#TA#0#DA#61022#1S#3008007#1T#1248#LS#3008043#LT#1323#PU#80#RT#1#CA#1aE#ZE#101#ZB#Bus 101 #PC#6#FR#3008007#FT#1248#TO#3008043#TT#1323#"), raw=True) test("raw_stop_by_name", pyrmv.raw.stop_by_name(key, "Groß Karben", maxNo=3), raw=True) test("raw_stop_by_coords", pyrmv.raw.stop_by_coords(key, 50.131140, 8.733362, radius=300, maxNo=3), raw=True) @@ -73,6 +73,7 @@ test("raw_trip_recon", pyrmv.raw.trip_recon(key, ctx="¶HKI¶G@F$A=2@O=50.084659 test("board_arrival", client.board_arrival("A=1@O=Frankfurt (Main) Taunusanlage@X=8668765@Y=50113478@U=80@L=3000011@", journeys_max=5)) test("board_departure", client.board_departure("A=1@O=Frankfurt (Main) Taunusanlage@X=8668765@Y=50113478@U=80@L=3000011@", journeys_max=5)) +test("him_search", client.him_search(date_end=datetime.now()+timedelta(days=10), priority_min=2, train_names=["S9"])) test("journey_detail", client.journey_detail("2|#VN#1#ST#1664906549#PI#0#ZI#12709#TA#0#DA#61022#1S#3008007#1T#1248#LS#3008043#LT#1323#PU#80#RT#1#CA#1aE#ZE#101#ZB#Bus 101 #PC#6#FR#3008007#FT#1248#TO#3008043#TT#1323#", real_time_mode=pyrmv.enums.RealTimeMode.FULL)) test("stop_by_coords", client.stop_by_coords(50.131140, 8.733362, radius=300, max_number=3)) test("stop_by_id", client.stop_by_id("A=1@O=Offenbach (Main)-Zentrum Marktplatz\/Frankf. Straße@X=8764456@Y=50105181@U=80@L=3002510@"))