oebb_py/workers/val.py

57 lines
1.5 KiB
Python

import json
import urllib.parse
from classes import Hafas, Station
def getValidator(name):
hafas = Hafas()
data = hafas.request("svcReqL", [
{
"req": {
"input": {
"field": "S",
"loc": {
"name": f"{name}?",
"type": "ALL",
"dist": 1000
},
"maxLoc": 7
}
},
"meth": "LocMatch",
"id": "1|1|"
}
])
return json.loads(data)
def validateName(name):
stations = getValidator(name)
for station in stations["svcResL"][0]["res"]["match"]["locL"]:
name = station["name"]
sttype = station["type"]
extid = station.get("extId")
xcoord = station["crd"]["x"]
ycoord = station["crd"]["y"]
prodclass = station.get("pCls")
yield Station(name=name, sttype=sttype, extid=extid, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass)
def worker(name, json=False):
outtext = """{
"stations": [
""" if json else """<?xml version="1.0" encoding="UTF-8"?>
<stations>
"""
for station in validateName(name):
outtext += ",\n" if (json and not outtext.strip()[-1] == "[") else ""
outtext += station.json(2) if json else station.xml(1)
outtext += "\n" if not json else ""
outtext += """
]
}""" if json else "</stations>"
return outtext