oebb_py/workers/deparr.py

114 lines
3.6 KiB
Python

from bs4 import BeautifulSoup
import datetime
import pytz
import threading
import queue
import sys
import workers.val
from classes import *
def getStation(name):
return list(workers.val.validateName(name))[0]
def getService(sid, url, dtime, q=None, eq=None):
try:
zuppa = BeautifulSoup(HTTPClient().get(url).text, "html5lib")
name = zuppa.findAll("div", {"class": "block"})[0].text.strip().replace(
"(Zug-Nr. ", " - ").replace(")", "")
ddate = zuppa.findAll("div", {"class": "block"})[1].text.strip()
table = zuppa.find("table", {"class": "resultTable"})
rows = table.findAll("tr")[1:]
for row in rows:
if len(row.findAll("td")) > 6 and row.findAll("td")[4].text.strip() == dtime:
depst = getStation(row.findAll("td")[1].text.strip())
currdep = row.findAll("td")[5].text.strip()
currdep = dtime if currdep == "pünktlich" else currdep or None
deppf = row.findAll("td")[-1].text.strip() or None
dest = getStation(rows[-1].findAll("td")[1].text.strip())
atime = rows[-1].findAll("td")[2].text.strip()
curarr = rows[-1].findAll("td")[3].text.strip()
curarr = atime if curarr == "pünktlich" else curarr or None
arrpf = rows[-1].findAll("td")[-1].text.strip()
deptime = datetime.datetime.strptime(
"%s %s" % (ddate, dtime), "%d.%m.%Y %H:%M")
arrtime = datetime.datetime.strptime(
"%s %s" % (ddate, atime), "%d.%m.%Y %H:%M")
if arrtime < deptime:
arrtime += datetime.timedelta(days=1)
if q:
q.put((sid, Service(name, depst, deptime, dest,
arrtime, dest, deppf, currdep, arrpf, curarr)))
return q
except:
if eq:
eq.put(sys.exc_info())
raise
def daRequest(station, count=3, time=datetime.datetime.now(), mode=False, details=False):
outdate = datetime.datetime.strftime(time, "%d.%m.%Y")
outtime = datetime.datetime.strftime(time, "%H:%M")
url = "http://fahrplan.oebb.at/bin/stboard.exe/dn?input=%s&boardType=%s&time=%s&productsFilter=1111111111111111&dateBegin=%s&dateEnd=&selectDate=&maxJourneys=%i&start=yes&dirInput=&sqView=2" % (
station.extid if station.extid else station.name, "arr" if mode else "dep", outtime, outdate, count)
source = HTTPClient().get(url).text
if "traininfo.exe/dn/" not in source:
raise ValueError("No services found.")
juha = BeautifulSoup(source, "html5lib")
services = []
table = juha.find("table", {"class": "resultTable"})
for row in table.findAll("tr")[1:-1]:
if not len(row.findAll("td")) < 4:
services += [(row.findAll("a")[0].get("href"),
row.findAll("td")[0].text.strip())]
threads = []
eq = queue.Queue()
q = queue.PriorityQueue()
for i in range(len(services)):
t = threading.Thread(target=getService, args=(
i, services[i][0], services[i][1], q, eq), daemon=True)
t.start()
threads += [t]
for t in threads:
t.join()
if not eq.empty():
exc = eq.get()
raise exc[1].with_traceback(exc[2])
while not q.empty():
station.addService(q.get()[1])
return station
def worker(station, count=30, time=datetime.datetime.now(pytz.timezone("Europe/Vienna")), mode=False, details=False, json=False):
station = daRequest(getStation(station), count, time, mode, details)
if json:
output = station.json(services=True)
else:
output = """<?xml version="1.0" encoding="UTF-8"?>\n"""
output += station.xml(services=True)
return output