Add DMCursorIterator
authorHarmon <Harmon758@gmail.com>
Sun, 25 Aug 2019 09:22:05 +0000 (04:22 -0500)
committerHarmon <Harmon758@gmail.com>
Sun, 25 Aug 2019 09:22:05 +0000 (04:22 -0500)
tweepy/binder.py
tweepy/cursor.py

index 7d352a6ac0451bc02c4d5444b1c651a2bb8cff10..ae4ece57eee96c295ee72a3fd745a03d46559d01 100644 (file)
@@ -253,7 +253,10 @@ def bind_api(**config):
 
     # Set pagination mode
     if 'cursor' in APIMethod.allowed_param:
-        _call.pagination_mode = 'cursor'
+        if APIMethod.payload_type == 'direct_message':
+            _call.pagination_mode = 'dm_cursor'
+        else:
+            _call.pagination_mode = 'cursor'
     elif 'max_id' in APIMethod.allowed_param:
         if 'since_id' in APIMethod.allowed_param:
             _call.pagination_mode = 'id'
index 1803c37406969dc6e3741acd77aaa96b8eea4736..c9282085ada68e5e3809a3659d8a01b341f62f43 100644 (file)
@@ -13,6 +13,8 @@ class Cursor(object):
         if hasattr(method, 'pagination_mode'):
             if method.pagination_mode == 'cursor':
                 self.iterator = CursorIterator(method, *args, **kwargs)
+            elif method.pagination_mode == 'dm_cursor':
+                self.iterator = DMCursorIterator(method, *args, **kwargs)
             elif method.pagination_mode == 'id':
                 self.iterator = IdIterator(method, *args, **kwargs)
             elif method.pagination_mode == 'page':
@@ -87,6 +89,28 @@ class CursorIterator(BaseIterator):
         return data
 
 
+class DMCursorIterator(BaseIterator):
+
+    def __init__(self, method, *args, **kwargs):
+        BaseIterator.__init__(self, method, *args, **kwargs)
+        self.next_cursor = self.kwargs.pop('cursor', None)
+        self.page_count = 0
+
+    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)
+        self.page_count += 1
+        if isinstance(data, tuple):
+            data, self.next_cursor = data
+        else:
+            self.next_cursor = -1
+        return data
+
+    def prev(self):
+        raise TweepError('This method does not allow backwards pagination')
+
+
 class IdIterator(BaseIterator):
 
     def __init__(self, method, *args, **kwargs):