From cf766311f0ea4030c8981894182ca49b425682a2 Mon Sep 17 00:00:00 2001 From: drevicko Date: Fri, 8 Nov 2013 12:23:22 +1100 Subject: [PATCH 1/3] added option to cursor() to yeild "pages" - ie: iterators of items for each page returned by twitter modified: api.py --- twython/api.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/twython/api.py b/twython/api.py index 39b8f28..6e9acac 100644 --- a/twython/api.py +++ b/twython/api.py @@ -388,7 +388,7 @@ class Twython(EndpointsMixin, object): ) 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. :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) @@ -418,8 +418,11 @@ class Twython(EndpointsMixin, object): else: results = content - for result in results: - yield result + if returnPages: + yield results + else: + for result in results: + yield result if function.iter_mode == 'cursor' and content['next_cursor_str'] == '0': raise StopIteration @@ -438,8 +441,11 @@ class Twython(EndpointsMixin, object): except (TypeError, ValueError): # pragma: no cover raise TwythonError('Unable to generate next page of search results, `page` is not a number.') - for result in self.cursor(function, **params): - yield result + if returnPages: + yield self.cursor(function, **params) + else: + for result in self.cursor(function, **params): + yield result @staticmethod def unicode2utf8(text): From 252ded6e00cfb751a27a84bfd8b2c57d2575d69e Mon Sep 17 00:00:00 2001 From: drevicko Date: Fri, 8 Nov 2013 12:43:29 +1100 Subject: [PATCH 2/3] fixed recursive part for returning cursor() pages modified: api.py --- twython/api.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/twython/api.py b/twython/api.py index 6e9acac..9fc673b 100644 --- a/twython/api.py +++ b/twython/api.py @@ -388,7 +388,7 @@ class Twython(EndpointsMixin, object): ) return self.cursor(self.search, q=search_query, **params) - def cursor(self, function, **params, returnPages = False): + def cursor(self, function, returnPages = False, **params): """Returns a generator for results that match a specified query. :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) @@ -441,11 +441,8 @@ class Twython(EndpointsMixin, object): except (TypeError, ValueError): # pragma: no cover raise TwythonError('Unable to generate next page of search results, `page` is not a number.') - if returnPages: - yield self.cursor(function, **params) - else: - for result in self.cursor(function, **params): - yield result + for result in self.cursor(function, returnPages = returnPages, **params): + yield result @staticmethod def unicode2utf8(text): From e1ee67192e37df7ff44337c9aaf8981ae0d93549 Mon Sep 17 00:00:00 2001 From: Ian Date: Fri, 21 Feb 2014 10:29:10 +1100 Subject: [PATCH 3/3] fixed paramater naming as per PEP8 --- twython/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/twython/api.py b/twython/api.py index 7eee2f4..bb272d8 100644 --- a/twython/api.py +++ b/twython/api.py @@ -386,7 +386,7 @@ class Twython(EndpointsMixin, object): ) return self.cursor(self.search, q=search_query, **params) - def cursor(self, function, returnPages = False, **params): + def cursor(self, function, return_pages=False, **params): """Returns a generator for results that match a specified query. :param function: Instance of a Twython function (Twython.get_home_timeline, Twython.search) @@ -416,7 +416,7 @@ class Twython(EndpointsMixin, object): else: results = content - if returnPages: + if return_pages: yield results else: for result in results: @@ -439,7 +439,7 @@ class Twython(EndpointsMixin, object): except (TypeError, ValueError): # pragma: no cover raise TwythonError('Unable to generate next page of search results, `page` is not a number.') - for result in self.cursor(function, returnPages = returnPages, **params): + for result in self.cursor(function, return_pages=return_pages, **params): yield result @staticmethod