3
from geopy import Point, Location
4
from urllib import urlencode
5
from urllib2 import urlopen, HTTPError
6
from geopy.geocoders.base import Geocoder
11
BASE_URL = "http://api.local.yahoo.com/MapsService/V1/geocode?%s"
13
def __init__(self, app_id, format_string='%s', output_format='xml'):
15
self.format_string = format_string
16
self.output_format = output_format.lower()
18
def geocode(self, string):
19
params = {'location': self.format_string % string,
20
'output': self.output_format,
23
url = self.BASE_URL % urlencode(params)
24
return self.geocode_url(url)
26
def geocode_url(self, url):
27
print "Fetching %s..." % url
30
parse = getattr(self, 'parse_' + self.output_format)
33
def parse_xml(self, page):
34
if not isinstance(page, basestring):
35
page = util.decode_page(page)
37
doc = xml.dom.minidom.parseString(page)
38
results = doc.getElementsByTagName('Result')
40
def parse_result(result):
42
address = util.get_first_text(result, 'Address', strip)
43
city = util.get_first_text(result, 'City', strip)
44
state = util.get_first_text(result, 'State', strip)
45
zip = util.get_first_text(result, 'Zip', strip)
46
country = util.get_first_text(result, 'Country', strip)
47
city_state = util.join_filter(", ", [city, state])
48
place = util.join_filter(" ", [city_state, zip])
49
location = util.join_filter(", ", [address, place, country])
50
latitude = util.get_first_text(result, 'Latitude') or None
51
longitude = util.get_first_text(result, 'Longitude') or None
52
if latitude and longitude:
53
point = Point(latitude, longitude)
56
return Location(location, point, {
64
return [parse_result(result) for result in results]