Commit a071bac0 authored by Joshua Roesslein's avatar Joshua Roesslein
Browse files

Implement ID based iterator.

parent 02a43b7f
......@@ -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'
......@@ -11,8 +11,12 @@ class Cursor(object):
if hasattr(method, 'pagination_mode'):
if method.pagination_mode == 'cursor':
self.iterator = CursorIterator(method, args, kargs)
elif method.pagination_mode == 'id':
self.iterator = IdIterator(method, args, kargs)
elif method.pagination_mode == 'page':
self.iterator = PageIterator(method, args, kargs)
raise TweepError('Invalid pagination mode.')
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):
......@@ -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
def max_id(self):
return self._max_id or max(self.ids())
def since_id(self):
return self._since_id or min(self.ids())
def ids(self):
return [ for item in self if hasattr(item, 'id')]
class Model(object):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment