Fix error caused by unexpected links in HAFAS' connection output, improve date handling, add exception raiser to threads

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-26 21:21:41 +02:00
parent d4dd2ba0e4
commit 92e69a14f4

View file

@ -4,6 +4,7 @@ import datetime
import pytz import pytz
import threading import threading
import queue import queue
import sys
import workers.val import workers.val
from classes import * from classes import *
@ -11,7 +12,8 @@ from classes import *
def getStation(name): def getStation(name):
return list(workers.val.validateName(name))[0] return list(workers.val.validateName(name))[0]
def getService(sid, lines, q): def getService(sid, lines, q, eq = None):
try:
dep = lines[0] dep = lines[0]
arr = lines[1] arr = lines[1]
det = lines[2] det = lines[2]
@ -37,14 +39,20 @@ def getService(sid, lines, q):
depdts = arrdts - datetime.timedelta(days=1) depdts = arrdts - datetime.timedelta(days=1)
depdate = datetime.datetime.strftime(depdts, "%d.%m.%Y") depdate = datetime.datetime.strftime(depdts, "%d.%m.%Y")
if not (walk and depdate): dest = None
if not (walk or depdate):
purl = dep.find("td", { "class": "product" }).find("a").get("href") purl = dep.find("td", { "class": "product" }).find("a").get("href")
psource = requests.get(purl).text psource = requests.get(purl).text
zuppa = BeautifulSoup(psource, "html5lib") zuppa = BeautifulSoup(psource, "html5lib")
depdate = zuppa.findAll("div", { "class": "block" })[1].text.strip() depdate = zuppa.findAll("div", { "class": "block" })[1].text.strip()
arrdate = arrdate or depdate arrdate = depdate
dest = None if walk else list(workers.val.validateName(zuppa.findAll("div", { "class": "block" })[2].text.split(":")[1].strip()))[0] dest = list(workers.val.validateName(zuppa.findAll("div", { "class": "block" })[2].text.split(":")[1].strip()))[0]
elif not depdate:
depdate = "01.01.2000"
arrdate = depdate
depts = datetime.datetime.strptime("%s %s" % (depdate, deptime), "%d.%m.%Y %H:%M") depts = datetime.datetime.strptime("%s %s" % (depdate, deptime), "%d.%m.%Y %H:%M")
arrts = datetime.datetime.strptime("%s %s" % (arrdate, arrtime), "%d.%m.%Y %H:%M") arrts = datetime.datetime.strptime("%s %s" % (arrdate, arrtime), "%d.%m.%Y %H:%M")
@ -55,7 +63,12 @@ def getService(sid, lines, q):
svc = Service(name, depst, depts, arrst, arrts, dest, depplat, depprog, arrplat, arrprog) svc = Service(name, depst, depts, arrst, arrts, dest, depplat, depprog, arrplat, arrprog)
q.put((sid, svc)) q.put((sid, svc))
def getDetails(cid, url, q, via = []): except Exception as e:
if eq:
eq.put(sys.exc_info())
def getDetails(cid, url, q, via = [], eq = None):
try:
ssource = requests.get(url).text ssource = requests.get(url).text
suppe = BeautifulSoup(ssource, "html5lib") suppe = BeautifulSoup(ssource, "html5lib")
@ -79,7 +92,7 @@ def getDetails(cid, url, q, via = []):
iq = queue.PriorityQueue() iq = queue.PriorityQueue()
for line in range(0, len(lines), 3): for line in range(0, len(lines), 3):
t = threading.Thread(target=getService, args=(line, lines[line:line + 3], iq)) t = threading.Thread(target=getService, args=(line, lines[line:line + 3], iq, eq))
t.start() t.start()
threads += [t] threads += [t]
@ -91,6 +104,10 @@ def getDetails(cid, url, q, via = []):
q.put((cid, conn)) q.put((cid, conn))
except:
if eq:
eq.put(sys.exc_info())
def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False, details = False, via = []): def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False, details = False, via = []):
outdate = datetime.datetime.strftime(time, "%d.%m.%Y") outdate = datetime.datetime.strftime(time, "%d.%m.%Y")
outtime = datetime.datetime.strftime(time, "%H:%M") outtime = datetime.datetime.strftime(time, "%H:%M")
@ -111,20 +128,25 @@ def connRequest(frm, to, count = 3, time = datetime.datetime.now(), mode = False
conns = [] conns = []
for a in juha.findAll("a"): for a in juha.findAll("a"):
if a.get("href") and "HWAI=CONNECTION$" in a.get("href"): if a.get("href") and "GO_conViewMode" in a.get("href"):
conns += [a.get("href")] conns += [a.get("href")]
threads = [] threads = []
eq = queue.Queue()
q = queue.PriorityQueue() q = queue.PriorityQueue()
for i in range(len(conns[:-1])): for i in range(len(conns)):
t = threading.Thread(target=getDetails, args=(i, conns[i], q, via)) t = threading.Thread(target=getDetails, args=(i, conns[i], q, via, eq))
t.start() t.start()
threads += [t] threads += [t]
for t in threads: for t in threads:
t.join() t.join()
if not eq.empty():
exc = eq.get()
raise exc[1].with_traceback(exc[2])
while not q.empty(): while not q.empty():
yield q.get()[1] yield q.get()[1]