Skip to content

Commit

Permalink
Merge pull request #91 from slimkrazy/development
Browse files Browse the repository at this point in the history
Release 1.4.0
  • Loading branch information
slimkrazy authored Jan 14, 2017
2 parents 9fea69a + 0771b87 commit af9f9be
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jpulec
ruhman
drootnar
keradus
daniil-shumko
beamerblvd
16 changes: 15 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ Code is easier to understand than words, so let us dive right in ::
photo.data


# Are there any additional pages of results?
if query_result.has_next_page_token:
query_result_next_page = google_places.nearby_search(
pagetoken=query_result.next_page_token)


# Adding and deleting a place
try:
added_place = google_places.add_place(name='Mom and Pop local store',
Expand Down Expand Up @@ -136,6 +142,10 @@ Reference
name -- A term to be matched against the names of the Places.
Results will be restricted to those containing the passed name value. (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)

Expand Down Expand Up @@ -165,6 +175,10 @@ Reference
language -- The language code, indicating in which language the results
should be returned, if possble. (default en)

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)

Expand Down Expand Up @@ -354,7 +368,7 @@ Reference

googleplaces.Place
reference
(DEPRECATED) Returns a unique identifier for the Place that can be used to
(DEPRECATED) Returns a unique identifier for the Place that can be used to
fetch full details about it. It is recommended that stored references for
Places be regularly updated. A Place may have many valid reference tokens.

Expand Down
58 changes: 45 additions & 13 deletions googleplaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

__all__ = ['GooglePlaces', 'GooglePlacesError', 'GooglePlacesAttributeError',
'geocode_location']
__version__ = '1.3.1'
__version__ = '1.4.0'
__author__ = 'Samuel Adu'
__email__ = '[email protected]'

Expand All @@ -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):
Expand All @@ -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', ''))
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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))
Expand Down

0 comments on commit af9f9be

Please sign in to comment.