Prepare worker.conn for JSON

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-24 22:59:51 +02:00
parent b8b233f630
commit 950924c31a
2 changed files with 110 additions and 91 deletions

View file

@ -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 + "<service%s>\n" % ("" if iid is False else " id=\"%i\"" % iid)
out += (" " * indent + " <name>%s</name>\n" % self.name) if name else ""
out += (" " * indent + " <destination>%s</destination>\n" % self.dest) if dest and self.dest else ""
if depst or deptime or deppf or currdep:
out += " " * indent + " <departure>\n"
out += (self.depst.xml(indent + 2) + "\n") if depst else ""
if deptime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.currdep) if currdep and self.currdep else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.deppf) if deppf and self.deppf else ""
out += " " * indent + " </departure>\n"
if arrst or arrtime or arrpf or curarr:
out += " " * indent + " <arrival>\n"
out += (self.arrst.xml(indent + 2) + "\n") if arrst else ""
if arrtime:
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime, "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime, "%H:%M")
out += (" " * indent + " <current>%s</current>\n" % self.curarr) if curarr and self.curarr else ""
out += (" " * indent + " <platform>%s</platform>\n" % self.arrpf) if arrpf and self.arrpf else ""
out += " " * indent + " </arrival>\n"
out += " " * indent + "</service>"
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 + "<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 += " " * indent + " </from>\n"
if to:
out += " " * indent + " <to>\n"
out += self.arrst().xml(indent + 2) + "\n"
out += " " * indent + " </to>\n"
if deptime or arrtime or duration or changes:
out += " " * indent + " <details>\n"
if deptime:
out += " " * indent + " <departure>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.deptime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.deptime(), "%H:%M")
out += " " * indent + " </departure>\n"
if arrtime:
out += " " * indent + " <arrival>\n"
out += " " * indent + " <date>%s</date>\n" % datetime.datetime.strftime(self.arrtime(), "%d.%m.%Y")
out += " " * indent + " <time>%s</time>\n" % datetime.datetime.strftime(self.arrtime(), "%H:%M")
out += " " * indent + " </arrival>\n"
out += (" " * indent + " <duration>%s</duration>\n" % self.durationString()) if duration else ""
out += (" " * indent + " <changes>%i</changes>\n" % self.changes()) if changes else ""
out += " " * indent + " </details>\n"
if services:
out += " " * indent + " <services>\n"
for i in range(len(self.services)):
out += self.services[i].xml(indent + 2, i) + "\n"
out += " " * indent + " </services>\n"
out += " " * indent + "</connection>"
return out

View file

@ -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 += """ <connection id='%i'>
<from>
<name>%s</name>
<id>%s</id>
</from>
<to>
<name>%s</name>
<id>%s</id>
</to>
<details>
<departure>
<date>%s</date>
<time>%s</time>
</departure>
<arrival>
<date>%s</date>
<time>%s</time>
</arrival>
<duration>%s</duration>
<changes>%i</changes>
</details>
<services>
""" % (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 += """ <service id='%i'>
<name>%s</name>
<departure>
<station>
<name>%s</name>
<id>%s</id>
</station>
<date>%s</date>
<time>%s</time>
""" % (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 += " <current>%s</current>\n" % service.currdep
if service.deppf:
output += " <platform>%s</platform>\n" % service.deppf
output += """ </departure>
<arrival>
<station>
<name>%s</name>
<id>%s</id>
</station>
<date>%s</date>
<time>%s</time>
""" % (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 += " <current>%s</current>\n" % service.curarr
if service.arrpf:
output += " <platform>%s</platform>\n" % service.arrpf
output += """ </arrival>
</service>
"""
j += 1
output += """ </services>
</connection>
"""
i += 1
output += conn.xml(1) + "\n"
output += """</connections>"""