2 # Copyright 2009-2010 Joshua Roesslein
3 # See LICENSE for details.
5 from tweepy.error import TweepError
8 """Pagination helper class"""
10 def __init__(self, method, *args, **kargs):
11 if hasattr(method, 'pagination_mode'):
12 if method.pagination_mode == 'cursor':
13 self.iterator = CursorIterator(method, args, kargs)
14 elif method.pagination_mode == 'id':
15 self.iterator = IdIterator(method, args, kargs)
16 elif method.pagination_mode == 'page':
17 self.iterator = PageIterator(method, args, kargs)
19 raise TweepError('Invalid pagination mode.')
21 raise TweepError('This method does not perform pagination')
23 def pages(self, limit=0):
24 """Return iterator for pages"""
26 self.iterator.limit = limit
29 def items(self, limit=0):
30 """Return iterator for items in each page"""
31 i = ItemIterator(self.iterator)
35 class BaseIterator(object):
37 def __init__(self, method, args, kargs):
44 raise NotImplementedError
47 raise NotImplementedError
52 class CursorIterator(BaseIterator):
54 def __init__(self, method, args, kargs):
55 BaseIterator.__init__(self, method, args, kargs)
56 start_cursor = kargs.pop('cursor', None)
57 self.next_cursor = start_cursor or -1
58 self.prev_cursor = start_cursor or 0
62 if self.next_cursor == 0 or (self.limit and self.count == self.limit):
64 data, cursors = self.method(
65 cursor=self.next_cursor, *self.args, **self.kargs
67 self.prev_cursor, self.next_cursor = cursors
74 if self.prev_cursor == 0:
75 raise TweepError('Can not page back more, at first page')
76 data, self.next_cursor, self.prev_cursor = self.method(
77 cursor=self.prev_cursor, *self.args, **self.kargs
82 class IdIterator(BaseIterator):
84 def __init__(self, method, args, kargs):
85 BaseIterator.__init__(self, method, args, kargs)
86 self.max_id = kargs.get('max_id')
87 self.since_id = kargs.get('since_id')
91 """Fetch a set of items with IDs less than current set."""
92 if self.limit and self.limit == self.count:
95 # max_id is inclusive so decrement by one
96 # to avoid requesting duplicate items.
97 max_id = self.since_id - 1 if self.max_id else None
98 data = self.method(max_id = max_id, *self.args, **self.kargs)
101 self.max_id = data.max_id
102 self.since_id = data.since_id
107 """Fetch a set of items with IDs greater than current set."""
108 if self.limit and self.limit == self.count:
111 since_id = self.max_id
112 data = self.method(since_id = since_id, *self.args, **self.kargs)
115 self.max_id = data.max_id
116 self.since_id = data.since_id
120 class PageIterator(BaseIterator):
122 def __init__(self, method, args, kargs):
123 BaseIterator.__init__(self, method, args, kargs)
124 self.current_page = 0
127 self.current_page += 1
128 items = self.method(page=self.current_page, *self.args, **self.kargs)
129 if len(items) == 0 or (self.limit > 0 and self.current_page > self.limit):
134 if (self.current_page == 1):
135 raise TweepError('Can not page back more, at first page')
136 self.current_page -= 1
137 return self.method(page=self.current_page, *self.args, **self.kargs)
139 class ItemIterator(BaseIterator):
141 def __init__(self, page_iterator):
142 self.page_iterator = page_iterator
144 self.current_page = None
149 if self.limit > 0 and self.count == self.limit:
151 if self.current_page is None or self.page_index == len(self.current_page) - 1:
152 # Reached end of current page, get the next page...
153 self.current_page = self.page_iterator.next()
157 return self.current_page[self.page_index]
160 if self.current_page is None:
161 raise TweepError('Can not go back more, at first page')
162 if self.page_index == 0:
163 # At the beginning of the current page, move to next...
164 self.current_page = self.page_iterator.prev()
165 self.page_index = len(self.current_page)
166 if self.page_index == 0:
167 raise TweepError('No more items')
170 return self.current_page[self.page_index]