forked from simonw/geocoders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyahoo.py
40 lines (33 loc) · 1.47 KB
/
yahoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from utils import make_nsfind, ET, partial2, RichResult
import urllib
# http://developer.yahoo.com/maps/rest/V1/geocode.html
def geocode(q, api_key):
find = make_nsfind({'ns': 'urn:yahoo:maps'})
args = {'location': q, 'appid': api_key}
url = 'http://local.yahooapis.com/MapsService/V1/geocode?%s' % urllib.urlencode(args)
et = ET.parse(urllib.urlopen(url))
result = find(et, '//ns:Result')
if not result:
return RichResult((None, (None, None)), data=None)
else:
namebits = {}
for field in ('Address', 'City', 'State', 'Zip', 'Country'):
bit = find(result, 'ns:%s' % field)
if bit is not None and bit.text:
namebits[field] = bit.text.decode('utf8')
if 'Address' in namebits:
name = '%(Address)s, %(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
elif 'Zip' in namebits:
name = '%(City)s, %(State)s %(Zip)s, %(Country)s' % namebits
elif 'City' in namebits:
name = '%(City)s, %(State)s, %(Country)s' % namebits
elif 'State' in namebits:
name = '%(State)s, %(Country)s' % namebits
elif 'Country' in namebits:
name = namebits['Country']
else:
return (None, (None, None))
lat = float(find(result, 'ns:Latitude').text)
lon = float(find(result, 'ns:Longitude').text)
return RichResult((name, (lat, lon)), data=result)
geocoder = partial2(geocode)