From a071bac08b6d2e5cbcc054c8615ed2efdd4b1768 Mon Sep 17 00:00:00 2001 From: Joshua Roesslein Date: Sun, 9 Jun 2013 12:37:13 -0700 Subject: [PATCH] Implement ID based iterator. --- tweepy/binder.py | 3 +++ tweepy/cursor.py | 32 +++++++++++++++++++++++++++++++- tweepy/models.py | 14 ++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tweepy/binder.py b/tweepy/binder.py index c5e99b9..0797215 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -200,6 +200,9 @@ def bind_api(**config): # Set pagination mode if 'cursor' in APIMethod.allowed_param: _call.pagination_mode = 'cursor' + elif 'max_id' in APIMethod.allowed_param and \ + 'since_id' in APIMethod.allowed_param: + _call.pagination_mode = 'id' elif 'page' in APIMethod.allowed_param: _call.pagination_mode = 'page' diff --git a/tweepy/cursor.py b/tweepy/cursor.py index 4a0f0b4..5900ab9 100644 --- a/tweepy/cursor.py +++ b/tweepy/cursor.py @@ -11,8 +11,12 @@ class Cursor(object): if hasattr(method, 'pagination_mode'): if method.pagination_mode == 'cursor': self.iterator = CursorIterator(method, args, kargs) - else: + elif method.pagination_mode == 'id': + self.iterator = IdIterator(method, args, kargs) + elif method.pagination_mode == 'page': self.iterator = PageIterator(method, args, kargs) + else: + raise TweepError('Invalid pagination mode.') else: raise TweepError('This method does not perform pagination') @@ -74,6 +78,32 @@ class CursorIterator(BaseIterator): self.count -= 1 return data +class IdIterator(BaseIterator): + + def __init__(self, method, args, kargs): + BaseIterator.__init__(self, method, args, kargs) + self.max_id = kargs.get('max_id') + self.since_id = kargs.get('since_id') + + def next(self): + """Fetch a set of items with IDs less than current set.""" + # max_id is inclusive so decrement by one + # to avoid requesting duplicate items. + max_id = self.since_id - 1 if self.max_id else None + data = self.method(max_id = max_id, *self.args, **self.kargs) + self.max_id = data.max_id + self.since_id = data.since_id + print 'next!' + return data + + def prev(self): + """Fetch a set of items with IDs greater than current set.""" + since_id = self.max_id + data = self.method(since_id = since_id, *self.args, **self.kargs) + self.max_id = data.max_id + self.since_id = data.since_id + return data + class PageIterator(BaseIterator): def __init__(self, method, args, kargs): diff --git a/tweepy/models.py b/tweepy/models.py index 8257e51..7171c40 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -9,7 +9,21 @@ from tweepy.utils import parse_datetime, parse_html_value, parse_a_href, \ class ResultSet(list): """A list like object that holds results from a Twitter API query.""" + def __init__(self, max_id=None, since_id=None): + super(ResultSet, self).__init__() + self._max_id = max_id + self._since_id = since_id + @property + def max_id(self): + return self._max_id or max(self.ids()) + + @property + def since_id(self): + return self._since_id or min(self.ids()) + + def ids(self): + return [item.id for item in self if hasattr(item, 'id')] class Model(object): -- 2.25.1