-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from slimkrazy/development
Release 1.4.0
- Loading branch information
Showing
3 changed files
with
62 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ jpulec | |
ruhman | ||
drootnar | ||
keradus | ||
daniil-shumko | ||
beamerblvd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
|
||
__all__ = ['GooglePlaces', 'GooglePlacesError', 'GooglePlacesAttributeError', | ||
'geocode_location'] | ||
__version__ = '1.3.1' | ||
__version__ = '1.4.0' | ||
__author__ = 'Samuel Adu' | ||
__email__ = '[email protected]' | ||
|
||
|
@@ -49,7 +49,10 @@ def __get__(self, instance, cls=None): | |
return result | ||
|
||
|
||
def _fetch_remote(service_url, params={}, use_http_post=False): | ||
def _fetch_remote(service_url, params=None, use_http_post=False): | ||
if not params: | ||
params = {} | ||
|
||
encoded_data = {} | ||
for k, v in params.items(): | ||
if isinstance(v, six.string_types): | ||
|
@@ -67,19 +70,25 @@ def _fetch_remote(service_url, params={}, use_http_post=False): | |
request = urllib.request.Request(service_url, data=encoded_data) | ||
return (request_url, urllib.request.urlopen(request)) | ||
|
||
def _fetch_remote_json(service_url, params={}, use_http_post=False): | ||
def _fetch_remote_json(service_url, params=None, use_http_post=False): | ||
"""Retrieves a JSON object from a URL.""" | ||
if not params: | ||
params = {} | ||
|
||
request_url, response = _fetch_remote(service_url, params, use_http_post) | ||
if six.PY3: | ||
str_response = response.read().decode('utf-8') | ||
return (request_url, json.loads(str_response, parse_float=Decimal)) | ||
return (request_url, json.load(response, parse_float=Decimal)) | ||
|
||
def _fetch_remote_file(service_url, params={}, use_http_post=False): | ||
def _fetch_remote_file(service_url, params=None, use_http_post=False): | ||
"""Retrieves a file from a URL. | ||
Returns a tuple (mimetype, filename, data) | ||
""" | ||
if not params: | ||
params = {} | ||
|
||
request_url, response = _fetch_remote(service_url, params, use_http_post) | ||
dummy, params = cgi.parse_header( | ||
response.headers.get('Content-Disposition', '')) | ||
|
@@ -220,11 +229,11 @@ def query(self, **kwargs): | |
|
||
def nearby_search(self, language=lang.ENGLISH, keyword=None, location=None, | ||
lat_lng=None, name=None, radius=3200, rankby=ranking.PROMINENCE, | ||
sensor=False, type=None, types=[]): | ||
sensor=False, type=None, types=[], pagetoken=None): | ||
"""Perform a nearby search using the Google Places API. | ||
One of either location or lat_lng are required, the rest of the keyword | ||
arguments are optional. | ||
One of either location, lat_lng or pagetoken are required, the rest of | ||
the keyword arguments are optional. | ||
keyword arguments: | ||
keyword -- A term to be matched against all available fields, including | ||
|
@@ -250,9 +259,13 @@ def nearby_search(self, language=lang.ENGLISH, keyword=None, location=None, | |
types -- An optional list of types, restricting the results to | ||
Places (default []). If there is only one item the request | ||
will be send as type param. | ||
pagetoken-- Optional parameter to force the search result to return the next | ||
20 results from a previously run search. Setting this parameter | ||
will execute a search with the same parameters used previously. | ||
(default None) | ||
""" | ||
if location is None and lat_lng is None: | ||
raise ValueError('One of location or lat_lng must be passed in.') | ||
if location is None and lat_lng is None and pagetoken is None: | ||
raise ValueError('One of location, lat_lng or pagetoken must be passed in.') | ||
if rankby == 'distance': | ||
# As per API docs rankby == distance: | ||
# One or more of keyword, name, or types is required. | ||
|
@@ -280,6 +293,8 @@ def nearby_search(self, language=lang.ENGLISH, keyword=None, location=None, | |
self._request_params['keyword'] = keyword | ||
if name is not None: | ||
self._request_params['name'] = name | ||
if pagetoken is not None: | ||
self._request_params['pagetoken'] = pagetoken | ||
if language is not None: | ||
self._request_params['language'] = language | ||
self._add_required_param_keys() | ||
|
@@ -288,18 +303,22 @@ def nearby_search(self, language=lang.ENGLISH, keyword=None, location=None, | |
_validate_response(url, places_response) | ||
return GooglePlacesSearchResult(self, places_response) | ||
|
||
def text_search(self, query, language=lang.ENGLISH, lat_lng=None, | ||
radius=3200, type=None, types=[], location=None): | ||
def text_search(self, query=None, language=lang.ENGLISH, lat_lng=None, | ||
radius=3200, type=None, types=[], location=None, pagetoken=None): | ||
"""Perform a text search using the Google Places API. | ||
Only the query kwarg is required, the rest of the keyword arguments | ||
are optional. | ||
Only the one of the query or pagetoken kwargs are required, the rest of the | ||
keyword arguments are optional. | ||
keyword arguments: | ||
lat_lng -- A dict containing the following keys: lat, lng | ||
(default None) | ||
location -- A human readable location, e.g 'London, England' | ||
(default None) | ||
pagetoken-- Optional parameter to force the search result to return the next | ||
20 results from a previously run search. Setting this parameter | ||
will execute a search with the same parameters used previously. | ||
(default None) | ||
radius -- The radius (in meters) around the location/lat_lng to | ||
restrict the search to. The maximum is 50000 meters. | ||
(default 3200) | ||
|
@@ -324,6 +343,8 @@ def text_search(self, query, language=lang.ENGLISH, lat_lng=None, | |
self._request_params['types'] = '|'.join(types) | ||
if language is not None: | ||
self._request_params['language'] = language | ||
if pagetoken is not None: | ||
self._request_params['pagetoken'] = pagetoken | ||
self._add_required_param_keys() | ||
url, places_response = _fetch_remote_json( | ||
GooglePlaces.TEXT_SEARCH_API_URL, self._request_params) | ||
|
@@ -757,6 +778,7 @@ def __init__(self, query_instance, response): | |
for place in response['results']: | ||
self._places.append(Place(query_instance, place)) | ||
self._html_attributions = response.get('html_attributions', []) | ||
self._next_page_token = response.get('next_page_token', []) | ||
|
||
@property | ||
def raw_response(self): | ||
|
@@ -779,11 +801,21 @@ def html_attributions(self): | |
""" | ||
return self._html_attributions | ||
|
||
@property | ||
def next_page_token(self): | ||
"""Returns the next page token(next_page_token).""" | ||
return self._next_page_token | ||
|
||
@property | ||
def has_attributions(self): | ||
"""Returns a flag denoting if the response had any html attributions.""" | ||
return len(self.html_attributions) > 0 | ||
|
||
@property | ||
def has_next_page_token(self): | ||
"""Returns a flag denoting if the response had a next page token.""" | ||
return len(self.next_page_token) > 0 | ||
|
||
def __repr__(self): | ||
""" Return a string representation stating the number of results.""" | ||
return '<{} with {} result(s)>'.format(self.__class__.__name__, len(self.places)) | ||
|