2 # Copyright 2009-2010 Joshua Roesslein
3 # See LICENSE for details.
5 from __future__ import print_function
7 from tweepy.error import TweepError
8 from tweepy.parsers import ModelParser, RawParser
12 """Pagination helper class"""
14 def __init__(self, method, *args, **kargs):
15 if hasattr(method, 'pagination_mode'):
16 if method.pagination_mode == 'cursor':
17 self.iterator = CursorIterator(method, args, kargs)
18 elif method.pagination_mode == 'id':
19 self.iterator = IdIterator(method, args, kargs)
20 elif method.pagination_mode == 'page':
21 self.iterator = PageIterator(method, args, kargs)
23 raise TweepError('Invalid pagination mode.')
25 raise TweepError('This method does not perform pagination')
27 def pages(self, limit=0):
28 """Return iterator for pages"""
30 self.iterator.limit = limit
33 def items(self, limit=0):
34 """Return iterator for items in each page"""
35 i = ItemIterator(self.iterator)
40 class BaseIterator(object):
42 def __init__(self, method, args, kargs):
52 raise NotImplementedError
55 raise NotImplementedError
61 class CursorIterator(BaseIterator):
63 def __init__(self, method, args, kargs):
64 BaseIterator.__init__(self, method, args, kargs)
65 start_cursor = kargs.pop('cursor', None)
66 self.next_cursor = start_cursor or -1
67 self.prev_cursor = start_cursor or 0
71 if self.next_cursor == 0 or (self.limit and self.num_tweets == self.limit):
73 data, cursors = self.method(cursor=self.next_cursor,
76 self.prev_cursor, self.next_cursor = cursors
83 if self.prev_cursor == 0:
84 raise TweepError('Can not page back more, at first page')
85 data, self.next_cursor, self.prev_cursor = self.method(cursor=self.prev_cursor,
92 class IdIterator(BaseIterator):
94 def __init__(self, method, args, kargs):
95 BaseIterator.__init__(self, method, args, kargs)
96 self.max_id = kargs.pop('max_id', None)
99 self.model_results = []
103 """Fetch a set of items with IDs less than current set."""
104 if self.limit and self.limit == self.num_tweets:
107 if self.index >= len(self.results) - 1:
108 data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kargs)
110 if hasattr(self.method, '__self__'):
111 old_parser = self.method.__self__.parser
112 # Hack for models which expect ModelParser to be set
113 self.method.__self__.parser = ModelParser()
115 # This is a special invocation that returns the underlying
117 model = ModelParser().parse(self.method(create=True), data)
118 if hasattr(self.method, '__self__'):
119 self.method.__self__.parser = old_parser
120 result = self.method.__self__.parser.parse(self.method(create=True), data)
124 if len(self.results) != 0:
126 self.results.append(result)
127 self.model_results.append(model)
130 result = self.results[self.index]
131 model = self.model_results[self.index]
135 # TODO: Make this not dependant on the parser making max_id and
137 self.max_id = model.max_id
142 """Fetch a set of items with IDs greater than current set."""
143 if self.limit and self.limit == self.num_tweets:
148 # There's no way to fetch a set of tweets directly 'above' the
152 data = self.results[self.index]
153 self.max_id = self.model_results[self.index].max_id
158 class PageIterator(BaseIterator):
160 def __init__(self, method, args, kargs):
161 BaseIterator.__init__(self, method, args, kargs)
162 self.current_page = 0
166 if self.current_page > self.limit:
169 items = self.method(page=self.current_page, *self.args, **self.kargs)
172 self.current_page += 1
176 if self.current_page == 1:
177 raise TweepError('Can not page back more, at first page')
178 self.current_page -= 1
179 return self.method(page=self.current_page, *self.args, **self.kargs)
182 class ItemIterator(BaseIterator):
184 def __init__(self, page_iterator):
185 self.page_iterator = page_iterator
187 self.current_page = None
193 if self.num_tweets == self.limit:
195 if self.current_page is None or self.page_index == len(self.current_page) - 1:
196 # Reached end of current page, get the next page...
197 self.current_page = self.page_iterator.next()
201 return self.current_page[self.page_index]
204 if self.current_page is None:
205 raise TweepError('Can not go back more, at first page')
206 if self.page_index == 0:
207 # At the beginning of the current page, move to next...
208 self.current_page = self.page_iterator.prev()
209 self.page_index = len(self.current_page)
210 if self.page_index == 0:
211 raise TweepError('No more items')
214 return self.current_page[self.page_index]