Skip to content

Commit a071bac

Browse files
committed
Implement ID based iterator.
1 parent 02a43b7 commit a071bac

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

tweepy/binder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ def _call(api, *args, **kargs):
200200
# Set pagination mode
201201
if 'cursor' in APIMethod.allowed_param:
202202
_call.pagination_mode = 'cursor'
203+
elif 'max_id' in APIMethod.allowed_param and \
204+
'since_id' in APIMethod.allowed_param:
205+
_call.pagination_mode = 'id'
203206
elif 'page' in APIMethod.allowed_param:
204207
_call.pagination_mode = 'page'
205208

tweepy/cursor.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ def __init__(self, method, *args, **kargs):
1111
if hasattr(method, 'pagination_mode'):
1212
if method.pagination_mode == 'cursor':
1313
self.iterator = CursorIterator(method, args, kargs)
14-
else:
14+
elif method.pagination_mode == 'id':
15+
self.iterator = IdIterator(method, args, kargs)
16+
elif method.pagination_mode == 'page':
1517
self.iterator = PageIterator(method, args, kargs)
18+
else:
19+
raise TweepError('Invalid pagination mode.')
1620
else:
1721
raise TweepError('This method does not perform pagination')
1822

@@ -74,6 +78,32 @@ def prev(self):
7478
self.count -= 1
7579
return data
7680

81+
class IdIterator(BaseIterator):
82+
83+
def __init__(self, method, args, kargs):
84+
BaseIterator.__init__(self, method, args, kargs)
85+
self.max_id = kargs.get('max_id')
86+
self.since_id = kargs.get('since_id')
87+
88+
def next(self):
89+
"""Fetch a set of items with IDs less than current set."""
90+
# max_id is inclusive so decrement by one
91+
# to avoid requesting duplicate items.
92+
max_id = self.since_id - 1 if self.max_id else None
93+
data = self.method(max_id = max_id, *self.args, **self.kargs)
94+
self.max_id = data.max_id
95+
self.since_id = data.since_id
96+
print 'next!'
97+
return data
98+
99+
def prev(self):
100+
"""Fetch a set of items with IDs greater than current set."""
101+
since_id = self.max_id
102+
data = self.method(since_id = since_id, *self.args, **self.kargs)
103+
self.max_id = data.max_id
104+
self.since_id = data.since_id
105+
return data
106+
77107
class PageIterator(BaseIterator):
78108

79109
def __init__(self, method, args, kargs):

tweepy/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@
99

1010
class ResultSet(list):
1111
"""A list like object that holds results from a Twitter API query."""
12+
def __init__(self, max_id=None, since_id=None):
13+
super(ResultSet, self).__init__()
14+
self._max_id = max_id
15+
self._since_id = since_id
1216

17+
@property
18+
def max_id(self):
19+
return self._max_id or max(self.ids())
20+
21+
@property
22+
def since_id(self):
23+
return self._since_id or min(self.ids())
24+
25+
def ids(self):
26+
return [item.id for item in self if hasattr(item, 'id')]
1327

1428
class Model(object):
1529

0 commit comments

Comments
 (0)