Add missing files for radar...

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-03 13:50:32 +02:00
parent bddef6c491
commit 337fe033c3
2 changed files with 115 additions and 0 deletions

69
classes/train.py Normal file
View File

@ -0,0 +1,69 @@
import workers.val
class Train:
def __init__(self, name, tid, dest, xcoord, ycoord, prodclass):
self.name = name
self.tid = tid
self.dest = dest
self.xcoord = float(xcoord)/1000000
self.ycoord = float(ycoord)/1000000
self.prodclass = prodclass
def lat(self):
return self.ycoord
def lon(self):
return self.xcoord
def destStation(self):
return list(workers.val.validateName(self.name))[0]
def json(self, indent = 0, name = True, tid = True, dest = True, coords = True, prodclass = False, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
out += (" " * indent + " \"id\": \"%s\",\n" % self.tid) if tid else ""
if dest:
# out += " " * indent + " \"destination\":\n"
# out += self.dest.json(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " \"destination\": \"%s\",\n" % self.dest
if coords:
out += " " * indent + " \"coords\": {\n"
out += " " * indent + " \"lon\": %f,\n" % self.xcoord
out += " " * indent + " \"lat\": %f\n" % self.ycoord
out += " " * indent + " },\n"
out += (" " * indent + " \"prodclass\": \"%s\",\n" % self.prodclass) if prodclass else ""
out = "".join(out.rsplit(",", 1))
out += " " * indent + "}"
return out
def xml(self, indent = 0, name = True, tid = True, dest = True, coords = True, prodclass = False, stationkwargs = {}):
out = " " * indent + "<train>\n"
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
out += (" " * indent + " <id>%s</id>\n" % self.tid) if tid else ""
if dest:
# out += " " * indent + " <destination>\n"
# out += self.dest.xml(indent + 2, **stationkwargs) + "\n"
# out += " " * indent + " </destination>\n"
out += " " * indent + " <destination>%s</destination>\n" % self.dest
if coords:
out += " " * indent + " <coords>\n"
out += " " * indent + " <lon>%f</lon>\n" % self.xcoord
out += " " * indent + " <lat>%f</lat>\n" % self.ycoord
out += " " * indent + " </coords>\n"
out += (" " * indent + " <prodclass>%s</prodclass>\n" % self.prodclass) if prodclass else ""
out += " " * indent + "</train>"
return out

46
workers/radar.py Normal file
View File

@ -0,0 +1,46 @@
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:
if cname.lower().replace(" ", "") == name.split("-")[0].lower().replace(" ", ""):
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