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