forked from simonw/geocoders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
placemaker.py
27 lines (24 loc) · 906 Bytes
/
placemaker.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
from utils import make_nsfind, ET, partial2, RichResult
import urllib
# http://developer.yahoo.com/geo/placemaker/guide/api_docs.html
def geocode(q, api_key):
find = make_nsfind({
'ns': 'http://wherein.yahooapis.com/v1/schema'
})
args = {
'documentContent': q,
'documentType': 'text/plain',
'appid': api_key,
}
et = ET.parse(urllib.urlopen(
'http://wherein.yahooapis.com/v1/document', urllib.urlencode(args))
)
place = find(et, 'ns:document/ns:placeDetails/ns:place')
if place is None:
return RichResult((None, (None, None)), data=None)
else:
name = find(place, 'ns:name').text.decode('utf8')
lat = float(find(place, 'ns:centroid/ns:latitude').text)
lon = float(find(place, 'ns:centroid/ns:longitude').text)
return RichResult((name, (lat, lon)), data=place)
geocoder = partial2(geocode)