From 2df4bd511da9031d66ac620a55e0553790037bd4 Mon Sep 17 00:00:00 2001 From: braian87b Date: Tue, 21 May 2019 12:49:12 -0300 Subject: [PATCH 1/2] Cache geocode intermediate queries to save money! When quering by location name it constantly query by geocode API, this adds a dict to keep in memory the values... I know that is very "basic" but it is saves a TON of money on your developer account!. --- googleplaces/__init__.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/googleplaces/__init__.py b/googleplaces/__init__.py index 9508444..cfdbaae 100644 --- a/googleplaces/__init__.py +++ b/googleplaces/__init__.py @@ -97,6 +97,8 @@ def _fetch_remote_file(service_url, params=None, use_http_post=False): return (response.headers.get('content-type'), fn, response.read(), response.geturl()) +locations_cache = {} + def geocode_location(location, sensor=False, api_key=None): """Converts a human-readable location to lat-lng. @@ -111,17 +113,23 @@ def geocode_location(location, sensor=False, api_key=None): raises: GooglePlacesError -- if the geocoder fails to find a location. """ - params = {'address': location, 'sensor': str(sensor).lower()} - if api_key is not None: - params['key'] = api_key - url, geo_response = _fetch_remote_json( - GooglePlaces.GEOCODE_API_URL, params) - _validate_response(url, geo_response) - if geo_response['status'] == GooglePlaces.RESPONSE_STATUS_ZERO_RESULTS: - error_detail = ('Lat/Lng for location \'%s\' can\'t be determined.' % - location) - raise GooglePlacesError(error_detail) - return geo_response['results'][0]['geometry']['location'] + if location in locations_cache: + print ("============= OBTENIDO DE LA CACHE!!!! =============") + return locations_cache[location] + else: + params = {'address': location, 'sensor': str(sensor).lower()} + if api_key is not None: + params['key'] = api_key + url, geo_response = _fetch_remote_json( + GooglePlaces.GEOCODE_API_URL, params) + _validate_response(url, geo_response) + if geo_response['status'] == GooglePlaces.RESPONSE_STATUS_ZERO_RESULTS: + error_detail = ('Lat/Lng for location \'%s\' can\'t be determined.' % + location) + raise GooglePlacesError(error_detail) + response = geo_response['results'][0]['geometry']['location'] + locations_cache.update({location: response}) + return response def _get_place_details(place_id, api_key, sensor=False, language=lang.ENGLISH): From 0d8a30f8c14f17d537fc788439f96213d05cc59b Mon Sep 17 00:00:00 2001 From: braian87b Date: Tue, 21 May 2019 12:51:42 -0300 Subject: [PATCH 2/2] Update __init__.py --- googleplaces/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/googleplaces/__init__.py b/googleplaces/__init__.py index cfdbaae..fdd1653 100644 --- a/googleplaces/__init__.py +++ b/googleplaces/__init__.py @@ -114,7 +114,6 @@ def geocode_location(location, sensor=False, api_key=None): GooglePlacesError -- if the geocoder fails to find a location. """ if location in locations_cache: - print ("============= OBTENIDO DE LA CACHE!!!! =============") return locations_cache[location] else: params = {'address': location, 'sensor': str(sensor).lower()}