Some improvements

This commit is contained in:
Kumi 2019-03-27 10:51:52 +01:00
parent 9f7bf662c2
commit 1a612baef1

15
main.py
View file

@ -1,6 +1,7 @@
import cgi
import datetime
import pytz
import html
import workers.conn
import workers.val
@ -20,16 +21,18 @@ HTML = "text/html"
JSON = "application/json"
XML = "text/xml"
DOCSTR = ' Check out the <a href="https://kumig.it/kumitterer/oebb_py#request">documentation</a> for usage instructions.'
def doConn(req):
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 = getfrm.encode("latin-1").decode("utf-8")
to = getto.encode("latin-1").decode("utf-8")
frm = html.unescape(getfrm.encode("latin-1").decode("utf-8"))
to = html.unescape(getto.encode("latin-1").decode("utf-8"))
except UnicodeDecodeError:
frm = getfrm
to = getto
frm = html.unescape(getfrm)
to = html.unescape(getto)
if not frm or not to:
raise ValueError()
@ -218,9 +221,9 @@ def application(env, re):
res = doNot(req)
except IllegalMethodException as e:
res = Response(HTTP405, HTML, str(e))
res = Response(HTTP405, HTML, str(e) + DOCSTR)
except InvalidArgumentException as e:
res = Response(HTTP400, HTML, str(e))
res = Response(HTTP400, HTML, str(e) + DOCSTR)
re(res.status, [("Content-Type", res.ctype)])
yield res.content.encode()