oebb_py/workers/radar.py

49 lines
1.6 KiB
Python
Raw Normal View History

2017-10-03 11:50:32 +00:00
import json
import urllib.parse
from classes import *
2017-10-03 11:50:32 +00:00
def getRadar():
return HTTPClient().get("http://zugradar.oebb.at/bin/query.exe/dny?look_minx=-180000000&look_maxx=180000000&look_miny=-90000000&look_maxy=90000000&tpl=trains2json2&look_json=yes&performLocating=1&look_nv=get_zntrainname|no|attr|81|get_rtonly|yes|zugposmode|2|interval|30000|intervalstep|2000|maxnumberoftrains|500000000000|").text
2017-10-03 11:50:32 +00:00
def getTrains(names=None):
trains = json.loads(getRadar())["t"]
for train in trains:
name = " ".join(train["n"].split())
2017-10-03 11:50:32 +00:00
# destination = workers.conn.getStation(train["l"])
destination = train["l"]
xcoord = train["x"]
ycoord = train["y"]
prodclass = train["c"]
tid = train["i"]
find = False if names else True
2017-10-03 11:50:32 +00:00
if not find:
for cname in names:
if cname.lower().replace(" ", "") == name.split("-")[0].lower().replace(" ", "") or name.lower().replace(" ", "").startswith(cname.lower().replace(" ", "")):
find = True
2017-10-03 11:50:32 +00:00
if find:
yield Train(name=name, dest=destination, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass, tid=tid)
2017-10-03 11:50:32 +00:00
def worker(trains=None, json=False):
outtext = """{
2017-10-03 11:50:32 +00:00
"trains": [
""" if json else """<?xml version="1.0" encoding="UTF-8"?>
<trains>
"""
for train in getTrains(trains):
outtext += ",\n" if (json and not outtext.strip()[-1] == "[") else ""
outtext += train.json(2) if json else train.xml(1)
outtext += "\n" if not json else ""
2017-10-03 11:50:32 +00:00
outtext += """
2017-10-03 11:50:32 +00:00
]
}""" if json else "</trains>"
return outtext