Small refactor and isort+black formatting
This commit is contained in:
@@ -1,55 +1,58 @@
|
||||
from requests import get
|
||||
from typing import OrderedDict, Union
|
||||
from xmltodict import parse as xmlparse
|
||||
from datetime import datetime
|
||||
from typing import OrderedDict, Union
|
||||
|
||||
from pyrmv.utility.weekdays_bitmask import weekdays_bitmask
|
||||
from requests import get
|
||||
from xmltodict import parse as xmlparse
|
||||
|
||||
from pyrmv.utility import weekdays_bitmask
|
||||
|
||||
try:
|
||||
from typing import Literal
|
||||
except ImportError:
|
||||
from typing_extensions import Literal
|
||||
|
||||
|
||||
# 2.37. HIM Search (himsearch)
|
||||
def him_search(accessId: str,
|
||||
json: bool = True,
|
||||
dateB: Union[str, datetime, None] = None,
|
||||
dateE: Union[str, datetime, None] = None,
|
||||
timeB: Union[str, datetime, None] = None,
|
||||
timeE: Union[str, datetime, None] = None,
|
||||
weekdays: Union[str, OrderedDict[str, bool], None] = None,
|
||||
himIds: Union[str, list, None] = None,
|
||||
hierarchicalView: bool = False,
|
||||
operators: Union[str, list, None] = None,
|
||||
categories: Union[str, list, None] = None,
|
||||
channels: Union[str, list, None] = None,
|
||||
companies: Union[str, list, None] = None,
|
||||
lines: Union[str, list, None] = None,
|
||||
lineids: Union[str, list, None] = None,
|
||||
stations: Union[str, list, None] = None,
|
||||
fromstation: Union[str, None] = None,
|
||||
tostation: Union[str, None] = None,
|
||||
bothways: Union[bool, None] = None,
|
||||
trainnames: Union[str, list, None] = None,
|
||||
metas: Union[str, list, None] = None,
|
||||
himcategory: Union[str, None] = None,
|
||||
himtags: Union[str, list, None] = None,
|
||||
regions: Union[str, list, None] = None,
|
||||
himtext: Union[str, list, None] = None,
|
||||
himtexttags: Union[str, list, None] = None,
|
||||
additionalfields: Union[str, list, dict, None] = None,
|
||||
poly: bool = False,
|
||||
searchmode: Union[Literal["MATCH", "NOMATCH", "TFMATCH"], None] = None,
|
||||
affectedJourneyMode: Union[Literal["ALL", "OFF"], None] = None,
|
||||
affectedJourneyStopMode: Union[Literal["ALL", "IMP", "OFF"], None] = None,
|
||||
orderBy: Union[str, list, None] = None,
|
||||
minprio: Union[str, int, None] = None,
|
||||
maxprio: Union[str, int, None] = None
|
||||
) -> dict:
|
||||
def him_search(
|
||||
accessId: str,
|
||||
json: bool = True,
|
||||
dateB: Union[str, datetime, None] = None,
|
||||
dateE: Union[str, datetime, None] = None,
|
||||
timeB: Union[str, datetime, None] = None,
|
||||
timeE: Union[str, datetime, None] = None,
|
||||
weekdays: Union[str, OrderedDict[str, bool], None] = None,
|
||||
himIds: Union[str, list, None] = None,
|
||||
hierarchicalView: bool = False,
|
||||
operators: Union[str, list, None] = None,
|
||||
categories: Union[str, list, None] = None,
|
||||
channels: Union[str, list, None] = None,
|
||||
companies: Union[str, list, None] = None,
|
||||
lines: Union[str, list, None] = None,
|
||||
lineids: Union[str, list, None] = None,
|
||||
stations: Union[str, list, None] = None,
|
||||
fromstation: Union[str, None] = None,
|
||||
tostation: Union[str, None] = None,
|
||||
bothways: Union[bool, None] = None,
|
||||
trainnames: Union[str, list, None] = None,
|
||||
metas: Union[str, list, None] = None,
|
||||
himcategory: Union[str, None] = None,
|
||||
himtags: Union[str, list, None] = None,
|
||||
regions: Union[str, list, None] = None,
|
||||
himtext: Union[str, list, None] = None,
|
||||
himtexttags: Union[str, list, None] = None,
|
||||
additionalfields: Union[str, list, dict, None] = None,
|
||||
poly: bool = False,
|
||||
searchmode: Union[Literal["MATCH", "NOMATCH", "TFMATCH"], None] = None,
|
||||
affectedJourneyMode: Union[Literal["ALL", "OFF"], None] = None,
|
||||
affectedJourneyStopMode: Union[Literal["ALL", "IMP", "OFF"], None] = None,
|
||||
orderBy: Union[str, list, None] = None,
|
||||
minprio: Union[str, int, None] = None,
|
||||
maxprio: Union[str, int, None] = None,
|
||||
) -> dict:
|
||||
"""The himSearch will return a list of HIM messages if matched by the given criteria as well as affected
|
||||
products if any.
|
||||
|
||||
Read more about this in section 2.37. "HIM Search (himsearch)" of HAFAS ReST Documentation.
|
||||
Read more about this in section 2.37. "HIM Search (himsearch)" of HAFAS ReST Documentation.
|
||||
|
||||
### Args:
|
||||
* accessId (str): Access ID for identifying the requesting client. Get your key on [RMV website](https://opendata.rmv.de/site/start.html).
|
||||
@@ -89,28 +92,23 @@ def him_search(accessId: str,
|
||||
|
||||
### Returns:
|
||||
* dict: Output from RMV. Dict will contain "errorCode" and "errorText" if exception occurs.
|
||||
"""
|
||||
|
||||
if json:
|
||||
headers = {"Accept": "application/json"}
|
||||
else:
|
||||
headers = {"Accept": "application/xml"}
|
||||
"""
|
||||
|
||||
payload = {}
|
||||
headers = {"Accept": "application/json"} if json else {"Accept": "application/xml"}
|
||||
|
||||
for var, val in locals().items():
|
||||
if str(var) in ["dateB", "dateE"]:
|
||||
if str(var) in {"dateB", "dateE"}:
|
||||
if val != None:
|
||||
if isinstance(val, datetime):
|
||||
payload[str(var)] = val.strftime("%Y-%m-%d")
|
||||
else:
|
||||
payload[str(var)] = val
|
||||
elif str(var) in ["timeB", "timeE"]:
|
||||
elif str(var) in {"timeB", "timeE"}:
|
||||
if val != None:
|
||||
if isinstance(val, datetime):
|
||||
payload[str(var)] = val.strftime("%H:%M")
|
||||
else:
|
||||
payload[str(var)] = val
|
||||
payload[str(var)] = (
|
||||
val.strftime("%H:%M") if isinstance(val, datetime) else val
|
||||
)
|
||||
elif str(var) == "weekdays":
|
||||
if val != None:
|
||||
if isinstance(val, OrderedDict):
|
||||
@@ -132,7 +130,4 @@ def him_search(accessId: str,
|
||||
|
||||
output = get("https://www.rmv.de/hapi/himsearch", params=payload, headers=headers)
|
||||
|
||||
if json:
|
||||
return output.json()
|
||||
else:
|
||||
return xmlparse(output.content)
|
||||
return output.json() if json else xmlparse(output.content)
|
||||
|
Reference in New Issue
Block a user