Add JSON export

This commit is contained in:
Klaus-Uwe Mitterer 2017-10-12 17:07:13 +02:00
parent 663665399d
commit 43110f8d17

86
main.py
View file

@ -62,6 +62,8 @@ def application(env, re):
cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None)) cur.execute(sql, (timestr, device, str(latitude), str(longitude), str(altitude) if altitude != None else None))
conn.commit() conn.commit()
cur.close()
conn.close()
re("200 OK", []) re("200 OK", [])
yield "OK".encode() yield "OK".encode()
@ -81,6 +83,9 @@ def application(env, re):
cur.execute("SELECT * FROM tracker WHERE device = %s ORDER BY ts DESC LIMIT 1;", device) cur.execute("SELECT * FROM tracker WHERE device = %s ORDER BY ts DESC LIMIT 1;", device)
row = cur.fetchone() row = cur.fetchone()
cur.close()
conn.close()
re("200 OK", [["Content-Type", "text/html"]]) re("200 OK", [["Content-Type", "text/html"]])
yield ("""<!DOCTYPE html> yield ("""<!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -108,3 +113,84 @@ def application(env, re):
</body> </body>
</html>""" % (row["ts"], row["lat"], row["lon"], row["lat"], row["lon"])).encode() </html>""" % (row["ts"], row["lat"], row["lon"], row["lat"], row["lon"])).encode()
return return
if env["PATH_INFO"] in ("/access", "/access.php", "/export", "/export.php"):
try:
device = args["device"][0]
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "device is required.".encode()
return
on = args["on"] if "on" in args else None
frm = on if on else args["from"] if "from" in args else None
to = on if on else args["to"] if "to" in args else None
frm = frm or datetime.datetime.now().strftime('%Y-%m-%d')
to = to or datetime.datetime.now().strftime('%Y-%m-%d')
for time in frm, to:
try:
datetime.strptime(time, "%Y-%m-%d")
except:
re("400 Bad Request", [])
yield "<h1>400 Bad Request</h1>".encode()
yield "Dates must be in YYYY-MM-DD format.".encode()
return
conn, cur = getDatabase()
sql = "SELECT ts, lat, lon FROM tracker WHERE device=%s AND DATE(ts)>=%s and DATE(ts)<=%s ORDER BY ts ASC;";
cur.execute(sql, (device, frm, to))
re("200 OK", [["Content-Type", "application/vnd.geo+json"], ['Content-Disposition', 'attachment; filename="export.geojson"']])
output = """{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature",
"properties": {
"Name": null,
"description": null,
"timestamp": null,
"begin": null,
"end": null,
"altitudeMode": null,
"tessellate": -1,
"extrude": 0,
"visibility": -1,
"drawOrder": null,
"icon": null,
"styleUrl": "#style",
"styleHash": "1a1ac94e",
"stroke": "#ffff00",
"stroke_opacity": "0.4980392156862745",
"stroke_width": "4",
"fill": "#00ff00",
"fill_opacity": "0.4980392156862745"
},
"geometry": {
"type": "LineString",
"coordinates": [
"""
for row in cur.fetchall():
output += " [ %s, %s ],\n" % (lat, lon)
output = "".join(output.rsplit(",", 1))
output += """ ]
}
}
]
}"""
cur.close()
conn.close()
yield output.encode()
return