Added stop_by_id() method

This commit is contained in:
Profitroll 2022-10-05 14:19:05 +02:00
parent 651d709e66
commit 366497d8fb
2 changed files with 49 additions and 0 deletions

View File

@ -3,6 +3,7 @@ from .board_departure import board_departure
from .him_search import him_search from .him_search import him_search
from .journey_detail import journey_detail from .journey_detail import journey_detail
from .stop_by_coords import stop_by_coords 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 .stop_by_name import stop_by_name
from .trip_find import trip_find from .trip_find import trip_find
from .trip_recon import trip_recon from .trip_recon import trip_recon

View File

@ -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