#!/usr/bin/env python # -*- coding: utf-8 -*- from web import db, SQLLiteral, config import urllib import simplejson as json import time try: import config except ImportError: print "Please create a config file first - see README.txt" exit() # wdb for writes wdb = db.database( dbn = config.dbtype, host = config.dbhost, db = config.dbname, user = config.dbuser, passwd = config.dbpass, use_unicode = True, charset = 'utf8') GOOGLE_MAPS_API_KEY = config.GOOGLE_MAPS_API_KEY DELAY = config.GOOGLE_API_DELAY status_codes = ( ("G_GEO_SUCCESS", 200), ("G_GEO_BAD_REQUEST", 400), ("G_GEO_SERVER_ERROR", 500), ("G_GEO_MISSING_QUERY", 601), ("G_GEO_MISSING_ADDRESS", 601), ("G_GEO_UNKNOWN_ADDRESS", 602), ("G_GEO_UNAVAILABLE_ADDRESS", 603), ("G_GEO_UNKNOWN_DIRECTIONS", 604), ("G_GEO_BAD_KEY", 610), ("G_GEO_TOO_MANY_QUERIES", 620)) def get_restaturants(): try: while True: yield wdb.query("SELECT * FROM raw WHERE GGeoStatusCode NOT IN (200, 602) LIMIT 1")[0] except IndexError: print "No more restaurants" def lookup(address): url = "".join([ "http://maps.google.com/maps/geo?q=", urllib.quote(address), "&output=json&oe=utf8&sensor=true&key=", GOOGLE_MAPS_API_KEY]) f = urllib.urlopen(url) return f.read() def main(): for rest in get_restaturants(): address = ", ".join([rest.address, rest.cp, rest.town, rest.province, 'spain']).encode("utf-8") result = lookup(address) geo = json.loads(result) #rest.GGeoJSONResponse = result #rest.GGeoStatusCode = geo['Status']['code'] #rest.GGeoCountPlacemarks = len(geo['Placemark']) try: gPlace = geo['Placemark'][0] numPlaces = len(geo['Placemark']) accuracy = gPlace['AddressDetails']['Accuracy'] lat = gPlace['Point']['coordinates'][1] lon = gPlace['Point']['coordinates'][0] address = gPlace['address'] except: gPlace = None numPlaces = 0 accuracy = 0 lat = 0 lon = 0 address = "" try: cp = gPlace['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber'] except: cp = '' try: road = gPlace['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['Thoroughfare']['ThoroughfareName'] except: road = '' try: adminArea = gPlace['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName'] except: adminArea = '' try: localityName = gPlace['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'] except: localityName = '' wdb.update('raw', where="id="+str(rest.id), GGeoJSONResponse = result, GGeoStatusCode = geo['Status']['code'], GGeoCountPlacemarks = numPlaces, GGeoAddressAccuracy = accuracy, GGeoAddress = address, GGeoLocalityName = localityName, GGeoPostalCodeNumber = cp, GGeoThoroughfareName = road, GGeoSubAdministrativeAreaName = adminArea, GGeoLatitude = lat, GGeoLongitude = lon ) time.sleep(DELAY) if __name__=="__main__": main()