diff --git a/main.py b/main.py
index 79715ca..0f0b268 100644
--- a/main.py
+++ b/main.py
@@ -101,6 +101,57 @@ def doVal(args, cfrm):
return HTTP200, JSON if json else XML, content
+def doNearby(args):
+ try:
+ lat = float(args["lat"][0].replace(",", "."))
+ lon = float(args["lon"][0].replace(",", "."))
+
+ if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
+ raise ValueError()
+
+ except:
+ content = "
400 Bad Request
\n"
+ content += "\"lat\" and \"lon\" values are required for this type of request."
+ return HTTP400, HTML, content
+
+ 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:
+ content = "400 Bad Request
\n"
+ content += "\"distance\" must be a value between 0 and 10000."
+ return HTTP400, HTML, content
+
+ json = "json" in args
+
+ try:
+ content = workers.closest.worker(lat, lon, distance, json)
+
+ except Exception as e:
+ content = "500 Internal Server Error
"
+ if "debug" in args:
+ content += repr(e)
+ return HTTP500, HTML, content
+
+ return HTTP200, JSON if json else XML, content
+
+def doRadar(args):
+ trains = args["train"] if "train" in args else None
+ json = "json" in args
+
+ try:
+ content = workers.radar.worker(trains, json)
+ except Exception as e:
+ content = "500 Internal Server Error
\n"
+ if "debug" in args:
+ content += repr(e)
+ return HTTP500, HTML, content
+
+ return HTTP200, JSON if json else XML, content
+
def application(env, re):
if env["REQUEST_METHOD"] == "POST":
args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
@@ -144,8 +195,6 @@ def application(env, re):
yield "A request type must be provided.".encode()
return
- json = "json" in args
-
if rtype.lower() in ["conn", "connection"]:
status, ctype, content = doConn(args, cfrm, cto)
re(status, [("Content-Type", ctype)])
@@ -159,58 +208,15 @@ def application(env, re):
return
elif rtype.lower() in ["closest", "close", "near", "nearby"]:
- try:
- lat = float(args["lat"][0].replace(",", "."))
- lon = float(args["lon"][0].replace(",", "."))
-
- if (not lat and not lat == float(0)) or (not lon and not lon == float(0)):
- raise ValueError()
-
- except:
- re("400 Bad Request", [])
- yield "400 Bad Request
".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 "400 Bad Request
"
- yield "\"distance\" must be a value between 0 and 10000."
- return
-
- try:
- output = workers.closest.worker(lat, lon, distance, json)
-
- except Exception as e:
- re("500 Internal Server Error", [])
- yield "500 Internal Server Error
".encode()
- if "debug" in args:
- yield e.encode()
- return
-
- re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
- yield output.encode()
+ status, ctype, content = doNearby(args)
+ re(status, [("Content-Type", ctype)])
+ yield content.encode()
return
elif rtype.lower() in ["radar", "live"]:
- trains = args["train"] if "train" in args else None
- try:
- output = workers.radar.worker(trains, json)
- except Exception as e:
- re("500 Internal Server Error", [])
- yield "500 Internal Server Error
".encode()
- if "debug" in args:
- yield e.encode()
- return
-
- re("200 OK", [("Content-Type", "application/json" if json else "text/xml")])
- yield output.encode()
+ status, ctype, content = doRadar(args)
+ re(status, [("Content-Type", ctype)])
+ yield content.encode()
return
else: