From 950924c31a4e820a41ccaebb0954171e8babb2b3 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sun, 24 Sep 2017 22:59:51 +0200 Subject: [PATCH] Prepare worker.conn for JSON --- classes/__init__.py | 106 ++++++++++++++++++++++++++++++++++++++++++-- workers/conn.py | 95 +++------------------------------------ 2 files changed, 110 insertions(+), 91 deletions(-) diff --git a/classes/__init__.py b/classes/__init__.py index 9d3151b..2d1db34 100644 --- a/classes/__init__.py +++ b/classes/__init__.py @@ -1,3 +1,5 @@ +import datetime + class Station: def __init__(self, name, sttype, extid = None, xcoord = None, ycoord = None, prodclass = None): self.name = name @@ -71,13 +73,54 @@ class Service: self.arrpf = arrpf self.curarr = curarr - def duration(): + 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): + out = " " * indent + "\n" % ("" if iid is False else " id=\"%i\"" % iid) + + out += (" " * indent + " %s\n" % self.name) if name else "" + out += (" " * indent + " %s\n" % self.dest) if dest and self.dest else "" + + if depst or deptime or deppf or currdep: + out += " " * indent + " \n" + + out += (self.depst.xml(indent + 2) + "\n") if depst else "" + + if deptime: + out += " " * indent + " %s\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y") + out += " " * indent + " \n" % datetime.datetime.strftime(self.deptime, "%H:%M") + + out += (" " * indent + " %s\n" % self.currdep) if currdep and self.currdep else "" + out += (" " * indent + " %s\n" % self.deppf) if deppf and self.deppf else "" + + out += " " * indent + " \n" + + if arrst or arrtime or arrpf or curarr: + out += " " * indent + " \n" + + out += (self.arrst.xml(indent + 2) + "\n") if arrst else "" + + if arrtime: + out += " " * indent + " %s\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y") + out += " " * indent + " \n" % datetime.datetime.strftime(self.arrtime, "%H:%M") + + out += (" " * indent + " %s\n" % self.curarr) if curarr and self.curarr else "" + out += (" " * indent + " %s\n" % self.arrpf) if arrpf and self.arrpf else "" + + out += " " * indent + " \n" + + out += " " * indent + "" + + return out + class Connection: - def __init__(self, buyurl = None): + def __init__(self, details = False): self.services = [] - self.buyurl = buyurl + self.details = details def addService(self, service): self.services.append(service) @@ -123,3 +166,60 @@ class Connection: return self.services[-1].arrtime - self.services[0].deptime except: return None + + def durationString(self): + hrs, scs = divmod(self.duration().total_seconds(), 3600) + mns, rmd = divmod(scs, 60) + + return "%i:%s" % (hrs, str(int(mns)).zfill(2)) + + 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): + out = " " * indent + "\n" % ("" if cid is False else " id=\"%i\"" % cid) + + if frm: + out += " " * indent + " \n" + out += self.depst().xml(indent + 2) + "\n" + out += " " * indent + " \n" + + if to: + out += " " * indent + " \n" + out += self.arrst().xml(indent + 2) + "\n" + out += " " * indent + " \n" + + if deptime or arrtime or duration or changes: + out += " " * indent + "
\n" + + if deptime: + out += " " * indent + " \n" + out += " " * indent + " %s\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y") + out += " " * indent + " \n" % datetime.datetime.strftime(self.deptime(), "%H:%M") + out += " " * indent + " \n" + + if arrtime: + out += " " * indent + " \n" + out += " " * indent + " %s\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y") + out += " " * indent + " \n" % datetime.datetime.strftime(self.arrtime(), "%H:%M") + out += " " * indent + " \n" + + out += (" " * indent + " %s\n" % self.durationString()) if duration else "" + out += (" " * indent + " %i\n" % self.changes()) if changes else "" + + out += " " * indent + "
\n" + + if services: + out += " " * indent + " \n" + + for i in range(len(self.services)): + out += self.services[i].xml(indent + 2, i) + "\n" + + out += " " * indent + " \n" + + out += " " * indent + "" + + return out diff --git a/workers/conn.py b/workers/conn.py index 014328d..0f0d96e 100644 --- a/workers/conn.py +++ b/workers/conn.py @@ -34,13 +34,13 @@ def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False if not cont: break - buyurl = None +# buyurl = None +# +# for url in cont.findAll("a"): +# if url.get("href") and "https://tickets.oebb.at/de/ticket/ticket?" in url.get("href"): +# buyurl = url.get("href") - for url in cont.findAll("a"): - if url.get("href") and "https://tickets.oebb.at/de/ticket/ticket?" in url.get("href"): - buyurl = url.get("href") - - conn = Connection(buyurl) + conn = Connection(details) lines = cont.findAll("tr", { "class": "tpDetails" })[1:] cdate = None @@ -134,88 +134,7 @@ def worker(frm, to, count = 3, time = datetime.datetime.now(pytz.timezone("Europ """ for conn in conns: - hrs, scs = divmod(conn.duration().total_seconds(), 3600) - mns, rmd = divmod(scs, 60) - - chg = max(len([service for service in conn.services if service.name != "Walk"]) - 1, 0) if details else max(len([service for service in conn.services[0].name.split("/") if service != "Walk"]) - 1, 0) - - ddt = datetime.datetime.strftime(conn.deptime(), "%d.%m.%Y") - dtm = datetime.datetime.strftime(conn.deptime(), "%H:%M") - - adt = datetime.datetime.strftime(conn.arrtime(), "%d.%m.%Y") - atm = datetime.datetime.strftime(conn.arrtime(), "%H:%M") - - output += """ - - %s - %s - - - %s - %s - -
- - %s - - - - %s - - - %s - %i -
- -""" % (i, conn.depst().name, conn.depst().useId(), conn.arrst().name, conn.arrst().useId(), ddt, dtm, adt, atm, "%i:%s" % (hrs, str(int(mns)).zfill(2)), chg) - - j = 0 - - for service in conn.services: - output += """ - %s - - - %s - %s - - %s - -""" % (j, service.name, service.depst.name, service.depst.useId(), datetime.datetime.strftime(service.deptime,"%d.%m.%Y"), datetime.datetime.strftime(service.deptime, "%H:%M")) - - if service.currdep: - output += " %s\n" % service.currdep - - if service.deppf: - output += " %s\n" % service.deppf - - output += """ - - - %s - %s - - %s - -""" % (service.arrst.name, service.arrst.useId(), datetime.datetime.strftime(service.arrtime, "%d.%m.%Y"), datetime.datetime.strftime(service.arrtime, "%H:%M")) - - if service.curarr: - output += " %s\n" % service.curarr - - if service.arrpf: - output += " %s\n" % service.arrpf - - output += """ - -""" - - j += 1 - - output += """ -
-""" - - i += 1 + output += conn.xml(1) + "\n" output += """"""