Permalink
Browse files

Merge branch 'master' of git://github.com/cheecheeo/geopy

  • Loading branch information...
2 parents b9fb668 + 53e610d commit 39b5d2f636f34b183233a8b9c3bcd54ee7a3a6bd @japerk committed Oct 7, 2011
Showing with 45 additions and 9 deletions.
  1. +19 −4 geopy/geocoders/bing.py
  2. +26 −5 geopy/geocoders/google.py
View
@@ -36,18 +36,33 @@ def __init__(self, api_key, format_string='%s', output_format=None):
self.format_string = format_string
self.url = "http://dev.virtualearth.net/REST/v1/Locations?%s"
- def geocode(self, string, exactly_one=True):
+ def geocode(self, string, exactly_one=True, raw = False):
params = {'query': self.format_string % string,
'key': self.api_key
}
url = self.url % urlencode(params)
- return self.geocode_url(url, exactly_one)
+ return self.geocode_url(url, exactly_one, raw)
- def geocode_url(self, url, exactly_one=True):
+ def geocode_url(self, url, exactly_one=True, raw=False):
logger.debug("Fetching %s..." % url)
page = urlopen(url)
- return self.parse_json(page, exactly_one)
+ return self.parse_raw(page, exactly_one) if raw else self.parse_json(page, exactly_one)
+
+ def parse_raw(self, page, exactly_one=True):
+ if not isinstance(page, basestring):
+ page = decode_page(page)
+ doc = json.loads(page)
+ resources = doc['resourceSets'][0]['resources']
+
+ if exactly_one and len(resources) != 1:
+ raise ValueError("Didn't find exactly one resource! " \
+ "(Found %d.)" % len(resources))
+
+ if exactly_one:
+ return resources[0]
+ else:
+ return resources
def parse_json(self, page, exactly_one=True):
"""Parse a location name, latitude, and longitude from an JSON response."""
View
@@ -69,22 +69,43 @@ def url(self):
domain = self.domain.strip('/')
return "http://%s/maps/geo?%%s" % domain
- def geocode(self, string, exactly_one=True):
+ def geocode(self, string, exactly_one=True, raw=False):
params = {'q': self.format_string % string,
- 'output': self.output_format.lower(),
+ 'output': 'json' if raw else self.output_format.lower(),
}
if self.api_key:
params['key'] = self.api_key
url = self.url % urlencode(params)
- return self.geocode_url(url, exactly_one)
+ return self.geocode_url(url, exactly_one, raw)
- def geocode_url(self, url, exactly_one=True):
+ def parse_raw(self, page, exactly_one=True):
+ if not isinstance(page, basestring):
+ page = util.decode_page(page)
+ doc = json.loads(page)
+ places = doc.get('Placemark', [])
+
+ if len(places) == 0:
+ # Got empty result. Parse out the status code and raise an error if necessary.
+ status = doc.get("Status", [])
+ status_code = status["code"]
+ self.check_status_code(status_code)
+ return None
+ elif exactly_one and len(places) != 1:
+ raise ValueError("Didn't find exactly one placemark! " \
+ "(Found %d.)" % len(places))
+
+ if exactly_one:
+ return places[0]
+ else:
+ return places
+
+ def geocode_url(self, url, exactly_one=True, raw=False):
util.logger.debug("Fetching %s..." % url)
page = urlopen(url)
- dispatch = getattr(self, 'parse_' + self.output_format)
+ dispatch = self.parse_raw if raw else getattr(self, 'parse_' + self.output_format)
return dispatch(page, exactly_one)
def parse_xml(self, page, exactly_one=True):

0 comments on commit 39b5d2f

Please sign in to comment.