diff --git a/geolocation/api.py b/geolocation/api.py index d57ca36..540a47d 100644 --- a/geolocation/api.py +++ b/geolocation/api.py @@ -8,7 +8,7 @@ class BaseApi(object): - log = logging.Logger('google_api') + log = logging.getLogger('google_api') def __init__(self, api_key): self.api_key = "&key=%s" % api_key diff --git a/geolocation/google_maps.py b/geolocation/google_maps.py index 0f8d66f..897f187 100644 --- a/geolocation/google_maps.py +++ b/geolocation/google_maps.py @@ -9,13 +9,13 @@ class GoogleMaps(object): """To find address use: GoogleMaps.search(location=full_address).""" - geocode = GeocodeParser() + geocode_parser = GeocodeParser() data = set() - log = logging.Logger('google_maps') + log = logging.getLogger('google_maps') def __init__(self, api_key): - self._geocode_api = GeocodeApi(api_key) + self.geocode_api = GeocodeApi(api_key) self.clear() def __repr__(self): @@ -32,33 +32,31 @@ def validate(location): return True - def _to_python(self, json_results): + def to_python(self, json_results): """Method should converts json_results to python object.""" for item in json_results: - self.geocode.json_data = item + self.geocode_parser.json_data = item location = LocationModel() - location.city = self.geocode.get_city() - location.route = self.geocode.get_route() - location.street_number = self.geocode.get_street_number() - location.postal_code = self.geocode.get_postal_code() + location.city = self.geocode_parser.get_city() + location.route = self.geocode_parser.get_route() + location.street_number = self.geocode_parser.get_street_number() + location.postal_code = self.geocode_parser.get_postal_code() - location.country = self.geocode.get_country() - location.country_shortcut = self.geocode.get_country_shortcut() + location.country = self.geocode_parser.get_country() + location.country_shortcut = self.geocode_parser.get_country_shortcut() - location.administrative_area = self.geocode.get_administrative_area() + location.administrative_area = self.geocode_parser.get_administrative_area() - location.lat = self.geocode.get_lat() - location.lng = self.geocode.get_lng() + location.lat = self.geocode_parser.get_lat() + location.lng = self.geocode_parser.get_lng() - location.formatted_address = self.geocode.get_formatted_address() + location.formatted_address = self.geocode_parser.get_formatted_address() if self.validate(location): self.data.add(location) - return self.all() - def all(self): """Method returns location list.""" return list(self.data) @@ -70,10 +68,10 @@ def first(self): return None def search(self, location=None, lat=None, lng=None): - json_results = self._geocode_api.query(location=location, lat=lat, lng=lng) + json_results = self.geocode_api.query(location=location, lat=lat, lng=lng) if json_results: - self._to_python(json_results) + self.to_python(json_results) return self diff --git a/geolocation/models.py b/geolocation/models.py index 5fd90ba..f9d6da9 100644 --- a/geolocation/models.py +++ b/geolocation/models.py @@ -5,15 +5,15 @@ class LocationModel(object): _administrative_area = list() def __init__(self, **kwargs): - self.city = kwargs.get('city', None) - self.route = kwargs.get('route', None) - self.street_number = kwargs.get('street_number', None) - self.country = kwargs.get('country', None) - self.country_shortcut = kwargs.get('country_shortcut', None) - self.postal_code = kwargs.get('postal_code', None) - self.lat = kwargs.get('lat', None) - self.lng = kwargs.get('lng', None) - self.formatted_address = kwargs.get('formatted_address', None) + self.city = kwargs.get('city') + self.route = kwargs.get('route') + self.street_number = kwargs.get('street_number') + self.country = kwargs.get('country') + self.country_shortcut = kwargs.get('country_shortcut') + self.postal_code = kwargs.get('postal_code') + self.lat = kwargs.get('lat') + self.lng = kwargs.get('lng') + self.formatted_address = kwargs.get('formatted_address') def __repr__(self): return '' % self.city @@ -25,7 +25,7 @@ def administrative_area(self): @administrative_area.setter def administrative_area(self, value_list): for value in value_list: - area = AdministrativeAreaLevelModel(value['area_type'], value['name']) + area = AdministrativeAreaLevelModel(value.get('area_type'), value.get('name')) self._administrative_area.append(area) diff --git a/geolocation/parsers.py b/geolocation/parsers.py index e82f7a7..433368e 100644 --- a/geolocation/parsers.py +++ b/geolocation/parsers.py @@ -16,23 +16,22 @@ def set_json_data(self, value): """Method sets value to json_data""" self.json_data = value - def _search_address_components(self, type_, shortcut=False): + def search_address_components(self, type_, shortcut=False): """Method searches address components in google maps json data.""" - if not self.json_data: - return None + data = None - for address_component in self.json_data['address_components']: - types = address_component['types'] + for address_component in self.json_data.get('address_components'): + types = address_component.get('types') if type_ in types: if shortcut: - return address_component['short_name'].encode('utf-8') + data = address_component.get('short_name').encode('utf-8') else: - return address_component['long_name'].encode('utf-8') + data = address_component.get('long_name').encode('utf-8') - return None + return data - def _search_geometry_location(self, type_): + def search_geometry_location(self, type_): """Method searches geometry location in google maps json data""" if not self.json_data: return None @@ -50,35 +49,35 @@ def get_formatted_address(self): def get_street_number(self): """Method should returns street number of current location.""" - return self._search_address_components('street_number') + return self.search_address_components('street_number') def get_route(self): """Method should returns route long name of current location.""" - return self._search_address_components('route') + return self.search_address_components('route') def get_postal_code(self): """Method should returns postal code of current location.""" - return self._search_address_components('postal_code') + return self.search_address_components('postal_code') def get_city(self): """Method should returns city long name of current location.""" - return self._search_address_components('locality') + return self.search_address_components('locality') def get_country(self): """Method should returns country long name from current location.""" - return self._search_address_components('country') + return self.search_address_components('country') def get_country_shortcut(self): """Method should returns country short name from current location.""" - return self._search_address_components('country', True) + return self.search_address_components('country', True) def get_lat(self): """Method should returns lat property of current location.""" - return self._search_geometry_location('lat') + return self.search_geometry_location('lat') def get_lng(self): """Method should returns lng property of current location.""" - return self._search_geometry_location('lng') + return self.search_geometry_location('lng') def get_administrative_area(self): """Method should returns all administrative areas of current location.""" @@ -89,7 +88,7 @@ def get_administrative_area(self): ] for area_type in administrative_areas: - name = self._search_address_components(area_type) + name = self.search_address_components(area_type) if name: data.append(dict(name=name, area_type=area_type)) diff --git a/setup.py b/setup.py index e60f529..110737b 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ setup( name='geolocation-python', - version='0.1.2d', + version='0.1.3', packages=['geolocation'], url='', download_url='https://github.com/slawek87/geolocation-python/',