Commits

Cédric Bonhomme committed ff77492

New project. A kind a Google Latitude (check-in) service for a more *personal* use.

  • Participants

Comments (0)

Files changed (3)

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import cgi
+import sqlite3
+import urllib
+from datetime import datetime
+from operator import itemgetter
+
+
+import utils
+from pygeocoder import Geocoder
+
+
+print "Content-Type: text/html\n\n"
+html = "<html><head>"
+print """<style type=text/css media=screen>
+body{font:normal medium 'Gill Sans','Gill Sans MT',Verdana,sans-serif;margin:1.75em auto;width:40.8em;line-height:1.75}
+blockquote{font-size:small;line-height:2.153846;margin:2.153846em 0;padding:0;font-style:oblique;border-left:1px dotted;margin-left:2.153846em;padding-left:2.153846em}
+blockquote p{margin:2.153846em 0}
+p+br{display:none}
+h1{font-size:large}
+h2,h3{font-size:medium}
+#footer{clear:both;text-align:center;font-size:small}
+img{border:0}
+.horizontal,.simple li{margin:0;padding:0;list-style:none;display:inline}
+.simple li:before{content:"+ "}
+.simple > li:first-child:before{content:""}
+.author{text-decoration:none;display:block;float:right;margin-left:2em;font-size:small}
+ul{list-style:none;margin-left:0;padding-left:0}
+li{clear:both}
+.content{margin:1.75em 1.75em}
+</style></head><body>"""
+print  "<title>Where is Cedric?</title>"
+form = cgi.FieldStorage()
+latitude = form.getvalue("latitude", "(none)")
+longitude = form.getvalue("longitude", "(none)")
+utime = form.getvalue("utime", "(none)")
+
+add_value(longitude, latitude, utime)
+
+
+positions = []
+try:
+    conn = sqlite3.connect("./longitude.db", isolation_level = None)
+    c = conn.cursor()
+    positions = c.execute("SELECT * FROM longitude").fetchall()
+except:
+    pass
+
+html += "<h1>Last location</h1>"
+
+
+history = sorted(positions,key=itemgetter(2), reverse=True)
+
+google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + history[0][1] + "," + history[0][0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
+urllib.urlretrieve(google_map_url, "./google_map.png")
+html += '<div align="center"><img src="' + google_map_url + '" align="center" /></div>'
+html += 'Time: ' + datetime.fromtimestamp(float(history[0][2])).isoformat()
+
+
+
+gcoder = Geocoder()
+html += "<h1>History</h1>"
+html += '<table border="1" width="100%">'
+for elem in history[:10]:
+    if elem[0] != '(none)':
+        try:
+            addresses = gcoder.reverse_geocode(float(elem[1]), float(elem[0]))
+            country = addresses[0]['address_components'][4]['long_name']
+            area = addresses[0]['address_components'][3]['long_name']
+            city = addresses[0]['address_components'][1]['long_name']
+        except Exception, e:
+            print e
+            city, country = "", ""
+        google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + elem[1] + "," + elem[0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
+        html += "<tr><td>" + city + ', ' + country + '<br />(<a href=' + google_map_url  +  '>map</a>)</td>'
+        html += "<td align='center'>" + str(elem[1]) + "</td><td align='center'>" +  str(elem[0]) + "</td><td align='center'>" +  datetime.fromtimestamp(float(elem[2])).isoformat()   + "</td></tr>"
+html += "</table>"
+html += "<hr /><p align='center'>Data from Android.</p>"
+html += "</body></html>"
+
+
+with open("./index.html", "w") as f:
+    f.write(html)
+ 

File longitude.cgi

+#!/usr/bin/python
+import cgi
+import sqlite3
+import urllib
+from datetime import datetime
+from operator import itemgetter
+from pygeocoder import Geocoder
+
+
+
+create_base()
+
+print "Content-Type: text/html\n\n"
+html = "<html><head>"
+html += """<style type=text/css media=screen>
+body{font:normal medium 'Gill Sans','Gill Sans MT',Verdana,sans-serif;margin:1.75em auto;width:40.8em;line-height:1.75}
+blockquote{font-size:small;line-height:2.153846;margin:2.153846em 0;padding:0;font-style:oblique;border-left:1px dotted;margin-left:2.153846em;padding-left:2.153846em}
+blockquote p{margin:2.153846em 0}
+p+br{display:none}
+h1{font-size:large}
+h2,h3{font-size:medium}
+#footer{clear:both;text-align:center;font-size:small}
+img{border:0}
+.horizontal,.simple li{margin:0;padding:0;list-style:none;display:inline}
+.simple li:before{content:"+ "}
+.simple > li:first-child:before{content:""}
+.author{text-decoration:none;display:block;float:right;margin-left:2em;font-size:small}
+ul{list-style:none;margin-left:0;padding-left:0}
+li{clear:both}
+.content{margin:1.75em 1.75em}
+</style></head><body>"""
+html+=  "<title>Where is Cedric?</title>"
+form = cgi.FieldStorage()
+latitude = form.getvalue("latitude", "(none)")
+longitude = form.getvalue("longitude", "(none)")
+utime = form.getvalue("utime", "(none)")
+
+add_value(longitude, latitude, utime)
+
+
+positions = []
+try:
+    conn = sqlite3.connect("./longitude.db", isolation_level = None)
+    c = conn.cursor()
+    positions = c.execute("SELECT * FROM longitude").fetchall()
+except:
+    pass
+
+html += "<h1>Last location</h1>"
+
+
+history = sorted(positions,key=itemgetter(2), reverse=True)
+
+google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + history[0][1] + "," + history[0][0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
+urllib.urlretrieve(google_map_url, "./google_map.png")
+html += '<div align="center"><img src="' + google_map_url + '" align="center" /></div>'
+html += 'Time: ' + datetime.fromtimestamp(float(history[0][2])).isoformat()
+
+
+
+gcoder = Geocoder()
+html += "<h1>History</h1>"
+html += '<table border="1" width="100%">'
+for elem in history[:10]:
+    if elem[0] != '(none)':
+        try:
+            addresses = gcoder.reverse_geocode(float(elem[1]), float(elem[0]))
+            country = addresses[0]['address_components'][4]['long_name']
+            area = addresses[0]['address_components'][3]['long_name']
+            city = addresses[0]['address_components'][1]['long_name']
+        except Exception, e:
+            print e
+            city, country = "", ""
+        google_map_url = "http://maps.google.com/maps/api/staticmap?center=" + elem[1] + "," + elem[0] + "&zoom=14&size=512x512&maptype=roadmap&sensor=false"
+        html += "<tr><td>" + city + ', ' + country + '<br />(<a href=' + google_map_url  +  '>map</a>)</td>'
+        html += "<td align='center'>" + str(elem[1]) + "</td><td align='center'>" +  str(elem[0]) + "</td><td align='center'>" +  datetime.fromtimestamp(float(elem[2])).isoformat()   + "</td></tr>"
+html += "</table>"
+html += "<hr /><p align='center'>Data from Android.</p>"
+html += "</body></html>"
+
+
+with open("./index.html", "w") as f:
+    f.write(html)
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+
+def create_base():
+    """
+    Create the base if not exists.
+    """
+    sqlite3.register_adapter(str, lambda s : s.decode('utf-8'))
+    conn = sqlite3.connect("./longitude.db", isolation_level = None)
+    c = conn.cursor()
+    c.execute('''create table if not exists longitude (longitude text, latitude text, utime text PRIMARY KEY)''')
+    conn.commit()
+    c.close()
+
+
+def add_value(longitude, latitude, utime):
+    c = None
+    try:
+        conn = sqlite3.connect("./longitude.db", isolation_level = None)
+        c = conn.cursor()
+        c.execute('insert into longitude values (?,?,?)', (longitude, latitude, utime))
+    except sqlite3.IntegrityError:
+        # feed already in the base
+        pass
+    finally:
+        conn.commit()
+        c.close()