Commits

Johnny Goodnow  committed 919c5f1

* Added google geocoding file to later integrate into project.

  • Participants
  • Parent commits 332eeae

Comments (0)

Files changed (1)

File geolocator/google_geocoding.py

+import urllib
+import urllib2
+import StringIO
+import gzip
+import json
+
+ZERO_RESULTS = 'ZERO_RESULTS'
+OVER_QUERY_LIMIT = 'OVER_QUERY_LIMIT'
+REQUEST_DENIED = 'REQUEST_DENIED'
+INVALID_REQUEST = 'INVALID_REQUEST'  
+
+def get_json(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 = urllib2.Request(request_str)
+	request.add_header('Accept-Encoding', 'gzip')
+	response = urllib2.urlopen(request)
+	compressed = response.info().getheader('Content-Encoding') == 'gzip'
+	print response.info().headers
+	json = 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()
+	status = jsonData['status']
+	if status == ZERO_RESULTS:
+		raise Warning('Zero Results')
+	elif status == OVER_QUERY_LIMIT:
+		raise Warning('Over Query Limit')
+	elif status == REQUEST_DENIED:
+		raise Warning('Request Denied')
+	elif status == INVALID_REQUEST:
+		raise Warning('Invalid Request')
+	if len(jsonData['results']) > 0:
+		result = jsonData['results'][0]
+		try:
+			lat = result['geometry']['location']['lat']
+			lng = result['geometry']['location']['lng']
+		except KeyError:
+			raise Warning('No Location Provided')
+			return (None, None)
+		else:
+			return (lat, lng)
+