diff --git a/classes/train.py b/classes/train.py new file mode 100644 index 0000000..3315fe0 --- /dev/null +++ b/classes/train.py @@ -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 + "\n" + + out += (" " * indent + " %s\n" % self.name) if name else "" + out += (" " * indent + " %s\n" % self.tid) if tid else "" + + if dest: +# out += " " * indent + " \n" +# out += self.dest.xml(indent + 2, **stationkwargs) + "\n" +# out += " " * indent + " \n" + out += " " * indent + " %s\n" % self.dest + + if coords: + out += " " * indent + " \n" + out += " " * indent + " %f\n" % self.xcoord + out += " " * indent + " %f\n" % self.ycoord + out += " " * indent + " \n" + + out += (" " * indent + " %s\n" % self.prodclass) if prodclass else "" + + out += " " * indent + "" + + return out + diff --git a/workers/radar.py b/workers/radar.py new file mode 100644 index 0000000..1cc34cf --- /dev/null +++ b/workers/radar.py @@ -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 """ + +""" + + 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 "" + + return outtext