Created
July 10, 2014 08:13
-
-
Save bealdav/944a9a938c4ea9255e20 to your computer and use it in GitHub Desktop.
Query api.geonames.org site to get flat country informations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Script to update 'countries.py' if needed | |
""" | |
from lxml import objectify | |
import requests | |
URL = 'http://api.geonames.org/countryInfo?username=demo' | |
FILE_PREFIX = """# -*- coding: utf-8 -*- | |
# These datas comes from %s. | |
# It allows to have matching keys 'isoAlpha3' (2 chars) and 'isoNumeric' | |
# according to 2 chars country code | |
# ie 'JP': {'isoAlpha3': 'JPN', 'isoNumeric': 392} | |
""" % URL | |
geo = requests.get(URL) | |
geonames = objectify.fromstring(geo.text.encode('utf-8')) | |
countries = {} | |
countries_elms = geonames.getchildren() | |
assert len(countries_elms) > 1, "geoname web service response: \n%s" % geonames.status.get('message') | |
for country in countries_elms: | |
countries[country.countryCode] = { | |
'isoAlpha3': country.isoAlpha3, | |
'isoNumeric': country.isoNumeric, | |
} | |
with open('./countries.py', 'w') as f: | |
f.write('%sdatas = %s' % (FILE_PREFIX, str(countries))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment