added option to cursor() to yeild "page" - ie: iterators over each page returned by twitter #282

Merged
drevicko merged 5 commits from cursor-pages into master 2014-03-05 07:49:40 -08:00
Showing only changes of commit cf766311f0 - Show all commits

View file

@ -388,7 +388,7 @@ class Twython(EndpointsMixin, object):
) )
return self.cursor(self.search, q=search_query, **params) return self.cursor(self.search, q=search_query, **params)
def cursor(self, function, **params): def cursor(self, function, **params, returnPages = False):
"""Returns a generator for results that match a specified query. """Returns a generator for results that match a specified query.
:param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search)
@ -418,8 +418,11 @@ class Twython(EndpointsMixin, object):
else: else:
results = content results = content
for result in results: if returnPages:
yield result yield results
else:
for result in results:
yield result
if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0':
raise StopIteration raise StopIteration
@ -438,8 +441,11 @@ class Twython(EndpointsMixin, object):
except (TypeError, ValueError): # pragma: no cover except (TypeError, ValueError): # pragma: no cover
raise TwythonError('Unable to generate next page of search results, `page` is not a number.') raise TwythonError('Unable to generate next page of search results, `page` is not a number.')
for result in self.cursor(function, **params): if returnPages:
yield result yield self.cursor(function, **params)
else:
for result in self.cursor(function, **params):
yield result
@staticmethod @staticmethod
def unicode2utf8(text): def unicode2utf8(text):