From 3c4512993cc5c57fa444d17e5603b0177aeee3ef Mon Sep 17 00:00:00 2001 From: Harmon Date: Sun, 25 Aug 2019 04:32:04 -0500 Subject: [PATCH] Return cursors when calling method from DMCursorIterator This adds a return_cursors kwarg and attribute to APIMethod that can be used to explicitly specify to return cursors with the result. The existing check for the cursor session parameter has been refactored from JSONParser.parse to APIMethod.execute. This means that the parser's parse method is called with a return_cursors kwarg. All parsers in the library have been updated to reflect this change. --- tweepy/binder.py | 4 +++- tweepy/cursor.py | 2 +- tweepy/parsers.py | 26 +++++++++++++------------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/tweepy/binder.py b/tweepy/binder.py index ae4ece5..846cfbf 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -55,6 +55,7 @@ def bind_api(**config): api.wait_on_rate_limit) self.wait_on_rate_limit_notify = kwargs.pop('wait_on_rate_limit_notify', api.wait_on_rate_limit_notify) + self.return_cursors = kwargs.pop('return_cursors', False) self.parser = kwargs.pop('parser', api.parser) self.session.headers = kwargs.pop('headers', {}) self.build_parameters(args, kwargs) @@ -233,7 +234,8 @@ def bind_api(**config): raise TweepError(error_msg, resp, api_code=api_error_code) # Parse the response payload - result = self.parser.parse(self, resp.text) + self.return_cursors = self.return_cursors or 'cursor' in self.session.params + result = self.parser.parse(self, resp.text, return_cursors=self.return_cursors) # Store result into cache if one is available. if self.use_cache and self.api.cache and self.method == 'GET' and result: diff --git a/tweepy/cursor.py b/tweepy/cursor.py index 2140ac7..2a3d950 100644 --- a/tweepy/cursor.py +++ b/tweepy/cursor.py @@ -99,7 +99,7 @@ class DMCursorIterator(BaseIterator): def next(self): if self.next_cursor == -1 or (self.limit and self.page_count == self.limit): raise StopIteration - data = self.method(cursor=self.next_cursor, *self.args, **self.kwargs) + data = self.method(cursor=self.next_cursor, return_cursors=True, *self.args, **self.kwargs) self.page_count += 1 if isinstance(data, tuple): data, self.next_cursor = data diff --git a/tweepy/parsers.py b/tweepy/parsers.py index 70fd978..7d09f63 100644 --- a/tweepy/parsers.py +++ b/tweepy/parsers.py @@ -10,7 +10,7 @@ from tweepy.models import ModelFactory class Parser(object): - def parse(self, method, payload): + def parse(self, method, payload, *args, **kwargs): """ Parse the response payload and return the result. Returns a tuple that contains the result data and the cursors @@ -32,7 +32,7 @@ class RawParser(Parser): def __init__(self): pass - def parse(self, method, payload): + def parse(self, method, payload, *args, **kwargs): return payload def parse_error(self, payload): @@ -43,20 +43,20 @@ class JSONParser(Parser): payload_format = 'json' - def parse(self, method, payload): + def parse(self, method, payload, return_cursors=False): try: json = json_lib.loads(payload) except Exception as e: raise TweepError('Failed to parse JSON payload: %s' % e) - needs_cursors = 'cursor' in method.session.params - if needs_cursors and isinstance(json, dict) \ - and 'previous_cursor' in json \ - and 'next_cursor' in json: - cursors = json['previous_cursor'], json['next_cursor'] - return json, cursors - else: - return json + if return_cursors and isinstance(json, dict): + if 'next_cursor' in json: + if 'previous_cursor' in json: + cursors = json['previous_cursor'], json['next_cursor'] + return json, cursors + else: + return json, json['next_cursor'] + return json def parse_error(self, payload): error_object = json_lib.loads(payload) @@ -79,7 +79,7 @@ class ModelParser(JSONParser): JSONParser.__init__(self) self.model_factory = model_factory or ModelFactory - def parse(self, method, payload): + def parse(self, method, payload, return_cursors=False): try: if method.payload_type is None: return @@ -88,7 +88,7 @@ class ModelParser(JSONParser): raise TweepError('No model for this payload type: ' '%s' % method.payload_type) - json = JSONParser.parse(self, method, payload) + json = JSONParser.parse(self, method, payload, return_cursors=return_cursors) if isinstance(json, tuple): json, cursors = json else: -- 2.25.1