From 366497d8fbaa225bb092f1b6c4c8a7038db28266 Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 5 Oct 2022 14:19:05 +0200 Subject: [PATCH] Added stop_by_id() method --- pyrmv/methods/__init__.py | 1 + pyrmv/methods/stop_by_id.py | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 pyrmv/methods/stop_by_id.py diff --git a/pyrmv/methods/__init__.py b/pyrmv/methods/__init__.py index 62fb3fd..12a6440 100644 --- a/pyrmv/methods/__init__.py +++ b/pyrmv/methods/__init__.py @@ -3,6 +3,7 @@ from .board_departure import board_departure from .him_search import him_search from .journey_detail import journey_detail from .stop_by_coords import stop_by_coords +from .stop_by_id import stop_by_id from .stop_by_name import stop_by_name from .trip_find import trip_find from .trip_recon import trip_recon \ No newline at end of file diff --git a/pyrmv/methods/stop_by_id.py b/pyrmv/methods/stop_by_id.py new file mode 100644 index 0000000..334c2ce --- /dev/null +++ b/pyrmv/methods/stop_by_id.py @@ -0,0 +1,48 @@ +from typing import List, Union +from pyrmv.classes.Stop import Stop +from pyrmv.enums.lang import Language +from pyrmv.raw.stop_by_name import stop_by_name as raw_stop_by_name +from pyrmv.utility.find_exception import find_exception + +try: + from typing import Literal +except ImportError: + from typing_extensions import Literal + +def stop_by_id( + + access_id: str, + query: str, + lang: Literal[Language.DE, Language.DA, Language.EN, Language.ES, Language.FR, Language.HU, Language.IT, Language.NL, Language.NO, Language.PL, Language.SV, Language.TR] = Language.EN, + ) -> Union[Stop, None]: + """Method can be used to get Stop object whilst only having id available. + + ### Args: + * access_id (`str`): Access ID for identifying the requesting client. Get your key on [RMV website](https://opendata.rmv.de/site/start.html). + * query (`str`): Search for that token. + * lang (`Literal[Language.DE, Language.DA, Language.EN, Language.ES, Language.FR, Language.HU, Language.IT, Language.NL, Language.NO, Language.PL, Language.SV, Language.TR]`, **optional**): The language of response. Defaults to `Language.EN`. + + ### Returns: + * Stop: Instance of Stop object or None if not found. + """ + + stops_raw = raw_stop_by_name( + accessId=access_id, + inputString=query, + lang=lang.code, + maxNo=1 + ) + + find_exception(stops_raw) + + if len(stops_raw["stopLocationOrCoordLocation"]) > 0: + stop = stops_raw["stopLocationOrCoordLocation"][0] + + if "StopLocation" in stop: + return Stop(stop["StopLocation"]) + elif "CoordLocation" in stop: + return Stop(stop["CoordLocation"]) + else: + return None + else: + return 0 \ No newline at end of file