2017-09-21 12:48:05 +00:00
|
|
|
import json
|
|
|
|
import urllib.parse
|
2022-04-20 10:02:59 +00:00
|
|
|
from classes import Hafas, Station
|
2017-09-21 12:48:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
|
2017-09-21 12:48:05 +00:00
|
|
|
def getValidator(name):
|
2022-04-20 10:02:59 +00:00
|
|
|
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)
|
2022-04-19 13:01:03 +00:00
|
|
|
|
2017-09-21 12:48:05 +00:00
|
|
|
|
|
|
|
def validateName(name):
|
2022-04-20 10:02:59 +00:00
|
|
|
stations = getValidator(name)
|
|
|
|
for station in stations["svcResL"][0]["res"]["match"]["locL"]:
|
|
|
|
name = station["name"]
|
2022-04-19 13:01:03 +00:00
|
|
|
sttype = station["type"]
|
2022-04-20 10:02:59 +00:00
|
|
|
extid = station.get("extId")
|
|
|
|
xcoord = station["crd"]["x"]
|
|
|
|
ycoord = station["crd"]["y"]
|
|
|
|
prodclass = station.get("pCls")
|
2022-04-19 13:01:03 +00:00
|
|
|
|
|
|
|
yield Station(name=name, sttype=sttype, extid=extid, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass)
|
|
|
|
|
|
|
|
|
|
|
|
def worker(name, json=False):
|
|
|
|
outtext = """{
|
2017-09-24 19:04:59 +00:00
|
|
|
"stations": [
|
|
|
|
""" if json else """<?xml version="1.0" encoding="UTF-8"?>
|
2017-09-23 14:57:24 +00:00
|
|
|
<stations>
|
2017-09-21 19:55:32 +00:00
|
|
|
"""
|
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
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 ""
|
2017-09-21 19:55:32 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
outtext += """
|
2017-09-24 19:04:59 +00:00
|
|
|
]
|
|
|
|
}""" if json else "</stations>"
|
2017-09-21 19:55:32 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
return outtext
|