import six
-from tweepy.binder import bind_api
+from tweepy.binder import bind_api, pagination
from tweepy.error import TweepError
from tweepy.parsers import ModelParser, Parser
from tweepy.utils import list_to_csv
'include_entities']
)
+ @pagination(mode='next')
def search_30_day(self, environment_name, *args, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search
:allowed_param: 'query', 'tag', 'fromDate', 'toDate', 'maxResults',
require_auth=True
)(*args, **kwargs)
+ @pagination(mode='next')
def search_full_archive(self, environment_name, *args, **kwargs):
""" :reference: https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search
:allowed_param: 'query', 'tag', 'fromDate', 'toDate', 'maxResults',
raise TweepError(error_msg, resp, api_code=api_error_code)
# Parse the response payload
- self.return_cursors = self.return_cursors or 'cursor' in self.session.params
+ self.return_cursors = (self.return_cursors or
+ 'cursor' in self.session.params or 'next' in self.session.params)
result = self.parser.parse(self, resp.text, return_cursors=self.return_cursors)
# Store result into cache if one is available.
_call.pagination_mode = 'page'
return _call
+
+
+def pagination(mode):
+ def decorator(method):
+ method.pagination_mode = mode
+ return method
+ return decorator
self.iterator = DMCursorIterator(method, *args, **kwargs)
elif method.pagination_mode == 'id':
self.iterator = IdIterator(method, *args, **kwargs)
+ elif method.pagination_mode == "next":
+ self.iterator = NextIterator(method, *args, **kwargs)
elif method.pagination_mode == 'page':
self.iterator = PageIterator(method, *args, **kwargs)
else:
return self.method(page=self.current_page, *self.args, **self.kwargs)
+class NextIterator(BaseIterator):
+
+ def __init__(self, method, *args, **kwargs):
+ BaseIterator.__init__(self, method, *args, **kwargs)
+ self.next_token = self.kwargs.pop('next', None)
+ self.page_count = 0
+
+ def next(self):
+ if self.next_token == -1 or (self.limit and self.page_count == self.limit):
+ raise StopIteration
+ data = self.method(next=self.next_token, return_cursors=True, *self.args, **self.kwargs)
+ self.page_count += 1
+ if isinstance(data, tuple):
+ data, self.next_token = data
+ else:
+ self.next_token = -1
+ return data
+
+ def prev(self):
+ raise TweepError('This method does not allow backwards pagination')
+
+
class ItemIterator(BaseIterator):
def __init__(self, page_iterator):
raise TweepError('Failed to parse JSON payload: %s' % e)
if return_cursors and isinstance(json, dict):
- if 'next_cursor' in json:
+ if 'next' in json:
+ return json, json['next']
+ elif 'next_cursor' in json:
if 'previous_cursor' in json:
cursors = json['previous_cursor'], json['next_cursor']
return json, cursors