Skip to content

Instantly share code, notes, and snippets.

@alexrutherford
Created February 8, 2016 16:13
Show Gist options
  • Save alexrutherford/caf183fea21ff774480d to your computer and use it in GitHub Desktop.
Save alexrutherford/caf183fea21ff774480d to your computer and use it in GitHub Desktop.
import requests
from secrets import key
def getCountry(s):
'''
Takes address string as input, queries Google geocoding API and returns country as ISO code
'''
tempUrl='http://maps.googleapis.com/maps/api/geocode/json?address=%s' % (s)
res=requests.get(tempUrl)
if len(res.json()['results'])==0:
return None
for comp in res.json()['results'][0]['address_components']:
#print comp.keys()
#print '\t',comp['types']
if u'country' in comp['types']:
#print '\t',
return comp['short_name']
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment