2017-09-19 20:50:19 +00:00
|
|
|
import cgi
|
|
|
|
import datetime
|
2017-09-22 15:25:26 +00:00
|
|
|
import pytz
|
2019-03-27 09:51:52 +00:00
|
|
|
import html
|
2022-04-19 11:32:38 +00:00
|
|
|
import traceback
|
2017-09-21 19:55:32 +00:00
|
|
|
|
2017-09-19 20:50:19 +00:00
|
|
|
import workers.conn
|
|
|
|
import workers.val
|
2017-09-21 19:55:32 +00:00
|
|
|
import workers.closest
|
2017-10-02 19:02:07 +00:00
|
|
|
import workers.radar
|
2017-11-04 18:37:45 +00:00
|
|
|
import workers.deparr
|
2017-09-19 20:50:19 +00:00
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
from classes.request import *
|
|
|
|
from classes.response import *
|
|
|
|
|
2017-10-27 09:28:14 +00:00
|
|
|
HTTP200 = "200 OK"
|
|
|
|
HTTP400 = "400 Bad Request"
|
2017-10-28 15:20:10 +00:00
|
|
|
HTTP405 = "405 Method Not Allowed"
|
2017-10-27 09:28:14 +00:00
|
|
|
HTTP500 = "500 Internal Server Error"
|
|
|
|
|
2019-04-16 18:04:19 +00:00
|
|
|
HTML = "text/html; charset=UTF-8"
|
|
|
|
JSON = "application/json; charset=UTF-8"
|
|
|
|
XML = "text/xml; charset=UTF-8"
|
2017-10-27 09:28:14 +00:00
|
|
|
|
2019-03-27 09:51:52 +00:00
|
|
|
DOCSTR = ' Check out the <a href="https://kumig.it/kumitterer/oebb_py#request">documentation</a> for usage instructions.'
|
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
def doConn(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
getfrm = req.cfrm if "cfrm" in dir(
|
|
|
|
req) and req.cfrm else req.args["from"][0] if "from" in req.args else None
|
|
|
|
getto = req.cto if "cto" in dir(
|
|
|
|
req) and req.cto else req.args["to"][0] if "to" in req.args else None
|
|
|
|
|
|
|
|
try:
|
|
|
|
frm = html.unescape(getfrm.encode("latin-1").decode("utf-8"))
|
|
|
|
to = html.unescape(getto.encode("latin-1").decode("utf-8"))
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
frm = html.unescape(getfrm)
|
|
|
|
to = html.unescape(getto)
|
|
|
|
|
|
|
|
if not frm or not to:
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "\"from\"and \"to\" values are required for this type of request."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
count = req.args["count"][0] if "count" in req.args and req.args["count"] else 6
|
|
|
|
date = req.args["date"][0] if "date" in req.args and req.args["date"] else datetime.datetime.strftime(
|
|
|
|
datetime.datetime.now(pytz.timezone("Europe/Vienna")), "%d.%m.%Y")
|
|
|
|
time = req.args["time"][0] if "time" in req.args and req.args["time"] else datetime.datetime.strftime(
|
|
|
|
datetime.datetime.now(pytz.timezone("Europe/Vienna")), "%H:%M")
|
|
|
|
mode = True if "mode" in req.args and req.args["mode"] and req.args["mode"][0].lower(
|
|
|
|
) == "arr" else False
|
|
|
|
details = True if "details" in req.args else False
|
|
|
|
|
|
|
|
try:
|
|
|
|
count = int(count)
|
|
|
|
if count < 0 or count > 10:
|
|
|
|
raise ValueError()
|
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "The \"count\" value must be a value between 0 and 10."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
try:
|
|
|
|
outtime = datetime.datetime.strptime(
|
|
|
|
"%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
via = list(req.args["via"]) if "via" in req.args else None
|
|
|
|
|
|
|
|
if via and len(via) > 3:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "It is not possible to route through more than three \"via\" stations."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = workers.conn.worker(
|
|
|
|
frm, to, count, outtime, mode, details, req.json, via)
|
|
|
|
except Exception as e:
|
|
|
|
content = "<h1>500 Internal Server Error</h1>\n"
|
|
|
|
if "debug" in req.args:
|
|
|
|
content += traceback.format_exc().replace("\n", "<br />")
|
|
|
|
return Response(HTTP500, HTML, content)
|
|
|
|
|
|
|
|
return Response(HTTP200, JSON if req.json else XML, content)
|
|
|
|
|
2017-10-27 09:28:14 +00:00
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
def doVal(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
name = (req.cfrm or req.args["name"][0]).encode(
|
|
|
|
"latin-1").decode("utf-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
name = req.cfrm or req.args["name"][0]
|
2017-10-27 15:46:00 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
if not name:
|
|
|
|
raise ValueError()
|
2017-10-27 15:46:00 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
except Exception:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "A \"name\" value is required for this type of request."
|
|
|
|
return Response(HTTP400, HTML, content)
|
2017-10-27 15:46:00 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
content = workers.val.worker(name, req.json)
|
2017-10-27 15:46:00 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
content = "<h1>500 Internal Server Error</h1>\n"
|
|
|
|
if "debug" in req.args:
|
|
|
|
content += traceback.format_exc().replace("\n", "<br />")
|
|
|
|
return Response(HTTP500, HTML, content)
|
|
|
|
|
|
|
|
return Response(HTTP200, JSON if req.json else XML, content)
|
2017-10-27 15:46:00 +00:00
|
|
|
|
2017-10-27 09:28:14 +00:00
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
def doNearby(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
lat = float(req.args["lat"][0].replace(",", "."))
|
|
|
|
lon = float(req.args["lon"][0].replace(",", "."))
|
|
|
|
|
|
|
|
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
|
|
|
|
raise ValueError()
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "\"lat\" and \"lon\" values are required for this type of request."
|
|
|
|
return Response(HTTP400, HTML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
distance = req.args["distance"][0] if "distance" in req.args and req.args["distance"] else 1000
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
distance = int(distance)
|
|
|
|
if distance < 0 or distance > 10000:
|
|
|
|
raise ValueError()
|
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "\"distance\" must be a value between 0 and 10000."
|
|
|
|
return Response(HTTP400, HTML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
content = workers.closest.worker(lat, lon, distance, req.json)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
content = "<h1>500 Internal Server Error</h1>"
|
|
|
|
if "debug" in req.args:
|
|
|
|
content += traceback.format_exc().replace("\n", "<br />")
|
|
|
|
return Response(HTTP500, HTML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
return Response(HTTP200, JSON if req.json else XML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
def doRadar(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
trains = req.args["train"] if "train" in req.args else None
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
content = workers.radar.worker(trains, req.json)
|
|
|
|
except Exception as e:
|
|
|
|
content = "<h1>500 Internal Server Error</h1>\n"
|
|
|
|
if "debug" in req.args:
|
|
|
|
content += traceback.format_exc().replace("\n", "<br />")
|
|
|
|
return Response(HTTP500, HTML, content)
|
|
|
|
|
|
|
|
return Response(HTTP200, JSON if req.json else XML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
|
|
|
|
2017-11-04 18:37:45 +00:00
|
|
|
def doDepArr(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
try:
|
|
|
|
name = req.args["name"][0]
|
|
|
|
try:
|
|
|
|
name = name.encode("latin-1").decode("utf-8")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "A \"name\" value is required for this type of request."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
count = req.args["count"][0] if "count" in req.args and req.args["count"] else 30
|
|
|
|
date = req.args["date"][0] if "date" in req.args and req.args["date"] else datetime.datetime.strftime(
|
|
|
|
datetime.datetime.now(pytz.timezone("Europe/Vienna")), "%d.%m.%Y")
|
|
|
|
time = req.args["time"][0] if "time" in req.args and req.args["time"] else datetime.datetime.strftime(
|
|
|
|
datetime.datetime.now(pytz.timezone("Europe/Vienna")), "%H:%M")
|
|
|
|
mode = True if req.rtype[:3] == "arr" else False
|
|
|
|
details = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
count = int(count)
|
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "The \"count\" value must be a numeric value."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
try:
|
|
|
|
outtime = datetime.datetime.strptime(
|
|
|
|
"%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
|
|
|
except:
|
|
|
|
content = "<h1>400 Bad Request</h1>\n"
|
|
|
|
content += "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format."
|
|
|
|
return Response(HTTP400, HTML, content)
|
|
|
|
|
|
|
|
try:
|
|
|
|
content = workers.deparr.worker(
|
|
|
|
name, count, outtime, mode, details, req.json)
|
|
|
|
except Exception as e:
|
|
|
|
content = "<h1>500 Internal Server Error</h1>\n"
|
|
|
|
if "debug" in req.args:
|
|
|
|
content += traceback.format_exc().replace("\n", "<br />")
|
|
|
|
return Response(HTTP500, HTML, content)
|
|
|
|
|
|
|
|
return Response(HTTP200, JSON if req.json else XML, content)
|
|
|
|
|
2017-11-04 18:37:45 +00:00
|
|
|
|
2017-10-28 15:20:10 +00:00
|
|
|
def doNot(req):
|
2022-04-19 13:01:03 +00:00
|
|
|
content = "<h1>400 Bad Request</h1>"
|
|
|
|
content += "The request type you submitted is invalid."
|
|
|
|
return Response(HTTP400, HTML, content)
|
2017-10-28 13:57:05 +00:00
|
|
|
|
2017-09-28 17:27:39 +00:00
|
|
|
|
2022-04-19 13:01:03 +00:00
|
|
|
def application(env, re):
|
|
|
|
try:
|
|
|
|
req = Request(env)
|
|
|
|
|
|
|
|
if req.rtype in ["conn", "connection", "connections"]:
|
|
|
|
res = doConn(req)
|
|
|
|
elif req.rtype in ["val", "validate"]:
|
|
|
|
res = doVal(req)
|
|
|
|
elif req.rtype in ["closest", "close", "near", "nearby"]:
|
|
|
|
res = doNearby(req)
|
|
|
|
elif req.rtype in ["radar", "live", "trains"]:
|
|
|
|
res = doRadar(req)
|
|
|
|
elif req.rtype in ["dep", "arr", "departure", "arrival", "departures", "arrivals"]:
|
|
|
|
res = doDepArr(req)
|
|
|
|
else:
|
|
|
|
res = doNot(req)
|
|
|
|
|
|
|
|
except IllegalMethodException as e:
|
|
|
|
res = Response(HTTP405, HTML, str(e) + DOCSTR)
|
|
|
|
except InvalidArgumentException as e:
|
|
|
|
res = Response(HTTP400, HTML, str(e) + DOCSTR)
|
|
|
|
|
|
|
|
re(res.status, [("Content-Type", res.ctype)])
|
|
|
|
yield res.content.encode()
|
|
|
|
return
|