oebb_py/workers/radar.py

47 lines
1.5 KiB
Python
Raw Normal View History

2017-10-03 11:50:32 +00:00
import requests
import json
import urllib.parse
from classes import *
def getRadar():
return requests.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
def getTrains(names = None):
trains = json.loads(getRadar())["t"]
for train in trains:
name = " ".join(train["n"].split())
# 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
if not find:
for cname in names:
2017-10-11 12:31:10 +00:00
if cname.lower().replace(" ", "") == name.split("-")[0].lower().replace(" ", "") or name.lower().replace(" ", "").startswith(cname.lower().replace(" ", "")):
2017-10-03 11:50:32 +00:00
find = True
if find:
yield Train(name = name, dest = destination, xcoord = xcoord, ycoord = ycoord, prodclass = prodclass, tid = tid)
def worker(trains = None, json = False):
outtext = """{
"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 ""
outtext += """
]
}""" if json else "</trains>"
return outtext