def _parse_json(self, resources, exactly_one=True): """ Parse display name, latitude, and longitude from an JSON response. """ if resources.get('info').get('statuscode') == 403: raise exc.GeocoderAuthenticationFailure() resources = resources.get('results')[0].get('locations', []) if not len(resources): return None def parse_resource(resource): city = resource['adminArea5'] county = resource['adminArea4'] state = resource['adminArea3'] country = resource['adminArea1'] latLng = resource['latLng'] latitude, longitude = latLng.get('lat'), latLng.get('lng') location = join_filter(", ", [city, county, state, country]) if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return (location, (latitude, longitude)) if exactly_one: return parse_resource(resources[0]) else: return [parse_resource(resource) for resource in resources]
def _parse_json(self, resources, exactly_one=True): """ Parse type, words, latitude, and longitude and language from a JSON response. """ if resources.get('error') == "X1": raise exc.GeocoderAuthenticationFailure() if resources.get('error') == "11": raise exc.GeocoderQueryError( "Address (Word(s)) not recognised by What3Words.") def parse_resource(resource): """ Parse record. """ if resource['type'] == '3 words': words = resource['words'] words = join_filter(".", [words[0], words[1], words[2]]) position = resource['position'] latitude, longitude = position[0], position[1] if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return Location(words, (latitude, longitude), resource) elif resource['type'] == 'OneWord': # TODO this branch probably needs to be removed, as OneWord # addresses have been canceled. words = resource['words'] words = join_filter(".", [words[0], words[1], words[2]]) oneword = resource['oneword'] info = resource['info'] address = join_filter(", ", [ oneword, words, info['name'], info['address1'], info['address2'], info['address3'], info['city'], info['county'], info['postcode'], info['country_id'] ]) position = resource['position'] latitude, longitude = position[0], position[1] if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return Location(address, (latitude, longitude), resource) else: raise exc.GeocoderParseError('Error parsing result.') location = parse_resource(resources) if exactly_one: return location else: return [location]
def _parse_json(self, resources, exactly_one=True): """ Parse type, words, latitude, and longitude and language from a JSON response. """ error = resources.get('error') if error is not None: # https://developer.what3words.com/public-api/docs#error-handling exc_msg = "Error returned by What3Words: %s" % resources["error"][ "message"] exc_code = error.get('code') if exc_code in ['MissingKey', 'InvalidKey']: raise exc.GeocoderAuthenticationFailure(exc_msg) raise exc.GeocoderQueryError(exc_msg) def parse_resource(resource): """ Parse record. """ if 'coordinates' in resource: words = resource['words'] position = resource['coordinates'] latitude, longitude = position['lat'], position['lng'] if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return Location(words, (latitude, longitude), resource) else: raise exc.GeocoderParseError('Error parsing result.') location = parse_resource(resources) if exactly_one: return location else: return [location]
def _parse_json(self, resources, exactly_one=True): """ Parse type, words, latitude, and longitude and language from a JSON response. """ code = resources['status'].get('code') if code: # https://docs.what3words.com/api/v2/#errors exc_msg = "Error returned by What3Words: %s" % resources['status'][ 'message'] if code == 401: raise exc.GeocoderAuthenticationFailure(exc_msg) raise exc.GeocoderQueryError(exc_msg) def parse_resource(resource): """ Parse record. """ if 'geometry' in resource: words = resource['words'] position = resource['geometry'] latitude, longitude = position['lat'], position['lng'] if latitude and longitude: latitude = float(latitude) longitude = float(longitude) return Location(words, (latitude, longitude), resource) else: raise exc.GeocoderParseError('Error parsing result.') location = parse_resource(resources) if exactly_one: return location else: return [location]