2017-09-19 20:50:19 +00:00
|
|
|
import cgi
|
|
|
|
import datetime
|
2017-09-22 15:25:26 +00:00
|
|
|
import pytz
|
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-09-19 20:50:19 +00:00
|
|
|
|
|
|
|
def application(env, re):
|
|
|
|
if env["REQUEST_METHOD"] == "POST":
|
|
|
|
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
|
|
|
|
elif env["REQUEST_METHOD"] == "GET":
|
|
|
|
args = cgi.parse_qs(env['QUERY_STRING'], True)
|
|
|
|
else:
|
|
|
|
re("405 Method Not Allowed", [])
|
|
|
|
return
|
|
|
|
|
2017-09-28 17:27:39 +00:00
|
|
|
conn = False
|
|
|
|
val = False
|
|
|
|
cfrm = None
|
|
|
|
cto = None
|
|
|
|
|
|
|
|
split = env["PATH_INFO"].split("/")
|
|
|
|
|
|
|
|
while "" in split:
|
|
|
|
split.remove("")
|
|
|
|
|
|
|
|
if len(split) > 2:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "Only one (validate) or two (conn) arguments may be passed as path."
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(split) > 0:
|
|
|
|
if len(split) == 1:
|
|
|
|
val = True
|
|
|
|
else:
|
|
|
|
conn = True
|
|
|
|
cto = split[1]
|
|
|
|
|
|
|
|
cfrm = split[0]
|
|
|
|
|
2017-09-19 20:50:19 +00:00
|
|
|
try:
|
2017-09-28 17:27:39 +00:00
|
|
|
rtype = "conn" if conn else "val" if val else args["type"][0]
|
2017-09-19 20:50:19 +00:00
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "A request type must be provided.".encode()
|
|
|
|
return
|
|
|
|
|
2017-09-23 14:57:24 +00:00
|
|
|
json = "json" in args
|
|
|
|
|
2017-09-19 20:50:19 +00:00
|
|
|
if rtype.lower() in ["conn", "connection"]:
|
|
|
|
try:
|
2017-09-28 17:27:39 +00:00
|
|
|
frm = cfrm or args["from"][0]
|
|
|
|
to = cto or args["to"][0]
|
2017-09-19 20:50:19 +00:00
|
|
|
|
|
|
|
if not frm or not to:
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "\"from\" and \"to\" values are required for this type of request.".encode()
|
|
|
|
return
|
|
|
|
|
2017-09-26 11:03:19 +00:00
|
|
|
count = args["count"][0] if "count" in args and args["count"] else 1
|
2017-09-22 15:25:26 +00:00
|
|
|
date = args["date"][0] if "date" in args and args["date"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%d.%m.%Y")
|
|
|
|
time = args["time"][0] if "time" in args and args["time"] else datetime.datetime.strftime(datetime.datetime.now(pytz.timezone("Europe/Vienna")),"%H:%M")
|
2017-09-19 21:01:25 +00:00
|
|
|
mode = True if "mode" in args and args["mode"] and args["mode"][0].lower() == "arr" else False
|
2017-09-22 15:25:26 +00:00
|
|
|
details = True if "details" in args else False
|
2017-09-19 20:50:19 +00:00
|
|
|
|
2017-09-21 19:55:32 +00:00
|
|
|
try:
|
|
|
|
count = int(count)
|
|
|
|
if count < 0 or count > 10:
|
|
|
|
raise ValueError()
|
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "The \"count\" value must be a value between 0 and 10."
|
|
|
|
return
|
|
|
|
|
2017-09-19 20:50:19 +00:00
|
|
|
try:
|
2017-09-19 20:58:40 +00:00
|
|
|
outtime = datetime.datetime.strptime("%s %s" % (date, time), "%d.%m.%Y %H:%M")
|
2017-09-19 20:50:19 +00:00
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "The \"date\" value must be in DD.MM.YYYY format, the \"time\" value must be in HH:MM format."
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2017-09-23 14:57:24 +00:00
|
|
|
output = workers.conn.worker(frm, to, count, outtime, mode, details, json)
|
2017-09-19 20:50:19 +00:00
|
|
|
except Exception as e:
|
|
|
|
re("500 Internal Server Error", [])
|
|
|
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
|
|
if "debug" in args:
|
|
|
|
yield str(e).encode()
|
|
|
|
return
|
|
|
|
|
2017-09-23 14:57:24 +00:00
|
|
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
2017-09-22 15:25:26 +00:00
|
|
|
yield output.encode()
|
|
|
|
return
|
2017-09-19 20:50:19 +00:00
|
|
|
|
|
|
|
elif rtype.lower() in ["val", "validate"]:
|
|
|
|
try:
|
2017-09-28 17:27:39 +00:00
|
|
|
name = cfrm or args["name"]
|
2017-09-19 20:50:19 +00:00
|
|
|
|
|
|
|
if not name:
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "A \"name\" value is required for this type of request.".encode()
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2017-09-23 14:57:24 +00:00
|
|
|
output = workers.val.worker(name, json)
|
2017-09-19 20:50:19 +00:00
|
|
|
|
2017-09-21 19:55:32 +00:00
|
|
|
except Exception as e:
|
2017-09-19 20:50:19 +00:00
|
|
|
re("500 Internal Server Error", [])
|
|
|
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
|
|
if "debug" in args:
|
|
|
|
yield str(e).encode()
|
|
|
|
return
|
|
|
|
|
2017-09-23 14:57:24 +00:00
|
|
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
2017-09-23 13:08:34 +00:00
|
|
|
yield output.encode()
|
|
|
|
return
|
2017-09-21 19:55:32 +00:00
|
|
|
|
|
|
|
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
|
|
|
|
try:
|
|
|
|
lat = float(args["lat"][0])
|
|
|
|
lon = float(args["lon"][0])
|
|
|
|
|
|
|
|
if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
|
|
|
|
raise ValueError()
|
|
|
|
|
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "\"lat\" and \"lon\" values are required for this type of request.".encode()
|
|
|
|
return
|
|
|
|
|
|
|
|
distance = args["distance"][0] if "distance" in args and args["distance"] else 1000
|
|
|
|
|
|
|
|
try:
|
|
|
|
distance = int(distance)
|
|
|
|
if distance < 0 or distance > 10000:
|
|
|
|
raise ValueError()
|
|
|
|
except:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>"
|
|
|
|
yield "\"distance\" must be a value between 0 and 10000."
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2017-09-23 14:57:24 +00:00
|
|
|
output = workers.closest.worker(lat, lon, distance, json)
|
2017-09-21 19:55:32 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
re("500 Internal Server Error", [])
|
|
|
|
yield "<h1>500 Internal Server Error</h1>".encode()
|
|
|
|
if "debug" in args:
|
|
|
|
yield e.encode()
|
|
|
|
return
|
|
|
|
|
2017-09-23 14:57:24 +00:00
|
|
|
re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
|
2017-09-21 19:55:32 +00:00
|
|
|
yield output.encode()
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
re("400 Bad Request", [])
|
|
|
|
yield "<h1>400 Bad Request</h1>".encode()
|
|
|
|
yield "The request type you submitted is invalid.".encode()
|
|
|
|
return
|