Skip to content

Instantly share code, notes, and snippets.

@bealdav
Created July 10, 2014 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bealdav/944a9a938c4ea9255e20 to your computer and use it in GitHub Desktop.
Save bealdav/944a9a938c4ea9255e20 to your computer and use it in GitHub Desktop.
Query api.geonames.org site to get flat country informations
# -*- 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