Commits

Johnny Goodnow committed 6e73a8b

* Added geocoding when caching queries.

  • Participants
  • Parent commits 4085e78

Comments (0)

Files changed (2)

File geolocator/google_geocoding.py

 REQUEST_DENIED = 'REQUEST_DENIED'
 INVALID_REQUEST = 'INVALID_REQUEST'  
 
-def get_json(address):
+def get_location(address):
 	address = urllib.quote_plus(address)
 	sensor = 'false'
 	request_str = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=%s' % (address, sensor)
 	request.add_header('Accept-Encoding', 'gzip')
 	response = urllib2.urlopen(request)
 	compressed = response.info().getheader('Content-Encoding') == 'gzip'
-	print response.info().headers
-	json = response.read()
+	jsonString = response.read()
 	if compressed:
-		print "Google Compressed It!"
 		compressed_stream = StringIO.StringIO(json)
 		gzip_stream = gzip.GzipFile(fileobj = compressed_stream)
-		json = gzip_stream.read()
-	print json
-	jsonFile = open('json.txt', 'w')
-	try:
-		jsonFile.write(json)
-	finally:
-		jsonFile.close()
-
-def do_work_with_json():
-	jsonFile = open('json.txt', 'Ur')
-	try:
-		jsonData = json.load(jsonFile)
-	finally:
-		jsonFile.close()
+		jsonString = gzip_stream.read()
+	jsonData = json.loads(jsonString)
 	status = jsonData['status']
 	if status == ZERO_RESULTS:
 		raise Warning('Zero Results')
 	if len(jsonData['results']) > 0:
 		result = jsonData['results'][0]
 		try:
-			#address = result['formatted_address']
+			addr = result['formatted_address']
 			lat = result['geometry']['location']['lat']
 			lng = result['geometry']['location']['lng']
 		except KeyError:
 			raise Warning('No Location Provided')
-			return (None, None)
+			return (None, None, None)
 		else:
-			return (lat, lng)
-
+			return (addr, lat, lng)

File geolocator/views.py

 from django.contrib import auth
 from django.http import HttpResponseRedirect
 
+import hokiefinder.geolocator.google_geocoding
 from hokiefinder.geolocator import models
 from hokiefinder.geolocator import forms
 from hokiefinder.geolocator.ldap_reader import Ldap_Reader
 				uupid = personInfo['uupid'][0]
 				name = personInfo['displayName'][0]
 				addr = personInfo['localPostalAddress'][0]
+				addr, lat, lng = google_geocoding(addr)
+				if addr == None:
+					continue
 				try:
 					phone = personInfo['localPhone'][0]
 				except KeyError:
 					major = personInfo['major'][0]
 				except KeyError:
 					major = None
-				lng = 0 #Need to set to geolocated values
-				lat = 0 #Need to set to geolocated values
 				newPerson = models.Person(pid = uupid, display_name = name, address = addr, phone_number = phone, \
 					major = major, longitude = lng, latitude = lat)
 				newPerson.save()