#!/usr/bin/env python

import json
import os
import re
import sys
import urllib
import urllib2
from urllib2 import urlopen, HTTPError

# Look up each city in "cities.in" on the http://www.fallingrain.com/
# web site, and append their geographic data into "cities.out".

projdir = os.path.join(os.path.dirname(sys.argv[0]), '..')

cin = open(os.path.join(projdir, 'data', 'cities.in'), 'r')
cout = open(os.path.join(projdir, 'extensions', 'data', 'cities.out'), 'a')

for line in cin:
    line = line.strip()
    if not line or line.startswith('#'):
        continue
    if not line.startswith('Birmin'):
        continue

    name = line
    url = ('http://maps.googleapis.com/maps/api/geocode/json?'
           + urllib.urlencode({'address': name, 'sensor': 'false'}))
    data = json.loads(urllib2.urlopen(url).read())
    location = data['results'][0]['geometry']['location']

    latlng = '%s,%s' % (location['lat'], location['lng'])
    url2 = ('http://maps.googleapis.com/maps/api/elevation/json?'
            + urllib.urlencode({'locations': latlng, 'sensor': 'false'}))
    data2 = json.loads(urllib2.urlopen(url2).read())

    s = "    '%s': ('%s', '%s', %f),  # %s" % (
        name,
        location['lat'],
        location['lng'],
        data2['results'][0]['elevation'],
        data['results'][0]['address_components'][-1]['long_name']
        )
    print s
    #print >>cout, s
