Make JSON available for all requests

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-25 21:43:02 +02:00
parent 950924c31a
commit a921460a11
2 changed files with 114 additions and 21 deletions

View file

@ -76,10 +76,7 @@ class Service:
def duration(self):
return self.arrtime - self.deptime
def json(self):
return
def xml(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True):
def xml(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "<service%s>\n" % ("" if iid is False else " id=\"%i\"" % iid)
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
@ -88,7 +85,7 @@ class Service:
if depst or deptime or deppf or currdep:
out += " " * indent + " <departure>\n"
out += (self.depst.xml(indent + 2) + "\n") if depst else ""
out += (self.depst.xml(indent + 2, **stationkwargs) + "\n") if depst else ""
if deptime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
@ -102,7 +99,7 @@ class Service:
if arrst or arrtime or arrpf or curarr:
out += " " * indent + " <arrival>\n"
out += (self.arrst.xml(indent + 2) + "\n") if arrst else ""
out += (self.arrst.xml(indent + 2, **stationkwargs) + "\n") if arrst else ""
if arrtime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
@ -117,6 +114,53 @@ class Service:
return out
def json(self, indent = 0, iid = False, name = True, depst = True, deptime = True, arrst = True, arrtime = True, deppf = True, currdep = True, arrpf = True, curarr = True, duration = True, dest = True, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % iid) if iid else ""
out += (" " * indent + " \"name\": \"%s\",\n" % self.name) if name else ""
out += (" " * indent + " \"destination\": \"%s\",\n" % self.dest) if dest and self.dest else ""
if depst or deptime or deppf or currdep:
dep = " " * indent + " \"departure\": {\n"
if depst:
dep += " " * indent + " \"station\":\n"
dep += self.depst.json(indent + 3, **stationkwargs) + ",\n"
if deptime:
dep += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
dep += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
dep += (" " * indent + " \"current\": \"%s\",\n" % self.currdep) if currdep and self.currdep else ""
dep += (" " * indent + " \"platform\": \"%s\",\n" % self.deppf) if deppf and self.deppf else ""
dep = "".join(dep.rsplit(",", 1))
out += dep + " " * indent + " },\n"
if arrst or arrtime or arrpf or curarr:
arr = " " * indent + " \"arrival\": {\n"
if arrst:
arr += " " * indent + " \"station\":\n"
arr += self.arrst.json(indent + 3, **stationkwargs) + ",\n"
if arrtime:
arr += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
arr += " " * indent + " \"time\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
arr += (" " * indent + " \"current\": \"%s\",\n" % self.curarr) if curarr and self.curarr else ""
arr += (" " * indent + " \"platform\": \"%s\",\n" % self.arrpf) if arrpf and self.arrpf else ""
arr = "".join(arr.rsplit(",", 1))
out += arr + " " * indent + " },\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out
class Connection:
def __init__(self, details = False):
self.services = []
@ -176,20 +220,17 @@ class Connection:
def changes(self):
return max(len([service for service in self.services if service.name != "Walk"]) - 1, 0) if self.details else max(len([service for service in self.services[0].name.split("/") if service != "Walk"]) - 1, 0)
def json(self):
return
def xml(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True):
def xml(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "<connection%s>\n" % ("" if cid is False else " id=\"%i\"" % cid)
if frm:
out += " " * indent + " <from>\n"
out += self.depst().xml(indent + 2) + "\n"
out += self.depst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </from>\n"
if to:
out += " " * indent + " <to>\n"
out += self.arrst().xml(indent + 2) + "\n"
out += self.arrst().xml(indent + 2, **stationkwargs) + "\n"
out += " " * indent + " </to>\n"
if deptime or arrtime or duration or changes:
@ -216,10 +257,61 @@ class Connection:
out += " " * indent + " <services>\n"
for i in range(len(self.services)):
out += self.services[i].xml(indent + 2, i) + "\n"
out += self.services[i].xml(indent + 2, i, **servicekwargs) + "\n"
out += " " * indent + " </services>\n"
out += " " * indent + "</connection>"
return out
def json(self, indent = 0, cid = False, frm = True, to = True, deptime = True, arrtime = True, duration = True, changes = True, services = True, servicekwargs = {}, stationkwargs = {}):
out = " " * indent + "{\n"
out += (" " * indent + " \"@id\": %i,\n" % cid) if cid else ""
if frm:
out += " " * indent + " \"from\":\n"
out += self.depst().json(indent + 2, **stationkwargs) + ",\n"
if to:
out += " " * indent + " \"to\":\n"
out += self.arrst().json(indent + 2, **stationkwargs) + ",\n"
if deptime or arrtime or duration or changes:
det = ""
det += " " * indent + " \"details\": {\n"
if deptime:
det += " " * indent + " \"departure\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
det += " " * indent + " },\n"
if arrtime:
det += " " * indent + " \"arrival\": {\n"
det += " " * indent + " \"date\": \"%s\",\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
det += " " * indent + " \"time\": \"%s\"\n"% datetime.datetime.strftime(self.arrtime(), "%H:%M")
det += " " * indent + " },\n"
det += (" " * indent + " \"duration\": \"%s\",\n" % self.durationString()) if duration else ""
det += (" " * indent + " \"changes\": %i,\n" % self.changes()) if changes else ""
det = "".join(det.rsplit(",", 1))
out += det
out += " " * indent + " },\n"
if services:
out += " " * indent + " \"services\": [\n"
for i in range(len(self.services)):
out += self.services[i].json(indent + 2, i, **servicekwargs) + (",\n" if not i == len(self.services) - 1 else "\n")
out += " " * indent + " ],\n"
out += " " * indent + "}"
out = "".join(out.rsplit(",", 1))
return out

View file

@ -116,26 +116,27 @@ def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False
name = "/".join([img.get("title") for img in det.findAll("img", { "class": "product" })])
ticketurl = det.find("td", { "class": "fares" }).find("a").get("href")
# ticketurl = det.find("td", { "class": "fares" }).find("a").get("href")
svc = Service(name, depst, depts, arrst, arrts, currdep = curdep, curarr = curarr)
con = Connection(ticketurl)
con = Connection(details)
con.addService(svc)
yield con
def worker(frm, to, count = 3, time = datetime.datetime.now(pytz.timezone("Europe/Vienna")), mode = False, details = False, json = False):
conns = connRequest(getStation(frm), getStation(to), count, time, mode, details)
i = 0
conns = list(connRequest(getStation(frm), getStation(to), count, time, mode, details))
output = """<?xml version="1.0" encoding="UTF-8"?>
<connections>
"""
""" if not json else """{
\"connections\": [
"""
for conn in conns:
output += conn.xml(1) + "\n"
for i in range(len(conns)):
output += (conns[i].xml(1) + "\n") if not json else (conns[i].json(2) + ("\n" if i == len(conns) - 1 else ",\n"))
output += """</connections>"""
output += "</connections>" if not json else " ]\n}"
return output