[Commits] python.it commit r247 - in code/pythonisti/trunk/pythonisti: . geo

commit a svn.python.it commit a svn.python.it
Sab 30 Dic 2006 21:19:07 CET


Author: manlio
Date: Sat Dec 30 21:19:06 2006
New Revision: 247

Added:
   code/pythonisti/trunk/pythonisti/geo/google.py   (contents, props changed)
Modified:
   code/pythonisti/trunk/pythonisti/settings.py
Log:
Aggiunto il supporto per il geocoder di Google.


Added: code/pythonisti/trunk/pythonisti/geo/google.py
==============================================================================
--- (empty file)
+++ code/pythonisti/trunk/pythonisti/geo/google.py	Sat Dec 30 21:19:06 2006
@@ -0,0 +1,65 @@
+"""Google API support.
+
+$Id$
+
+Copyright (c) 2006 Perillo Manlio (manlio.perillo a gmail.com)
+All rights reserved.
+"""
+
+from urllib import urlencode
+from urllib2 import urlopen
+from simplejson import loads
+
+from django.conf import settings
+
+
+
+class AddressNotFound(Exception):
+    pass
+
+
+
+def get_page(url):
+    """Retrieve the content of the HTTP url, as an Unicode string.
+    
+    TODO move this function to pythonisti.util
+    """
+
+    fp = urlopen(url)
+    
+    content_type = fp.info()['content-type']
+    # TODO parse the charset
+    charset = 'utf-8'
+    print content_type
+
+    return fp.read().decode(charset)
+
+
+def get_geo_location(address):
+    """Return a list of placemarkers: Point, AddressDetails and
+    address.
+
+    address must be an Unicode string
+    """
+
+    url = 'http://maps.google.com/maps/geo?'
+    cmd = {
+        'q': address.encode('utf-8'),
+        'output': 'json',
+        'key': settings.GOOGLE_API_KEY
+        }
+
+    # Get the response from Google
+    page = get_page(url + urlencode(cmd))
+    response = loads(page.encode('utf-8'))
+
+    name = response['name']        
+    status = response['Status']
+
+    assert status['request'] == 'geocode'
+    assert name == address
+
+    if status['code'] != 200:
+        raise AddressNotFound()
+
+    return response['Placemark']

Modified: code/pythonisti/trunk/pythonisti/settings.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/settings.py	(original)
+++ code/pythonisti/trunk/pythonisti/settings.py	Sat Dec 30 21:19:06 2006
@@ -72,3 +72,9 @@
     'django.contrib.admin',
     'pythonisti.geo'
 )
+
+
+#
+# pythonisti specific configuration
+#
+GOOGLE_API_KEY = file('gmaps.key').read()


Maggiori informazioni sulla lista Commits