From: Josh Roesslein Date: Tue, 29 Sep 2009 02:28:54 +0000 (-0500) Subject: Added Cursor object to help with pagination. "page" based pagination working. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f4f9b05a4fd09086922c4be4cbc2f33d9c7331d3;p=tweepy.git Added Cursor object to help with pagination. "page" based pagination working. --- diff --git a/CHANGES b/CHANGES index 0af376b..c3576cb 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,9 @@ during upgrade will be listed here. + API + Added cursor parameter to API.friends and API.followers methods. Note: page parameter is being deprecated by twitter on 10/26 ++ Cursor + Added the Cursor object to help with pagination within the API. + Please see the pagination tutorial for more details. 1.0.1 -> 1.1 ======================= diff --git a/tweepy/__init__.py b/tweepy/__init__.py index 69193d2..dfd35b8 100644 --- a/tweepy/__init__.py +++ b/tweepy/__init__.py @@ -14,6 +14,7 @@ from . cache import Cache, MemoryCache, FileCache, MemCache from . auth import BasicAuthHandler, OAuthHandler from . streaming import Stream, StreamListener from . logging import TweepyLogger, DummyLogger, ConsoleLogger, FileLogger +from . cursor import Cursor # Global, unauthenticated instance of API api = API() diff --git a/tweepy/binder.py b/tweepy/binder.py index ac04ec3..53a3e48 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -150,5 +150,8 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, return out + # Expose extra data in callable object + _call.allowed_param = allowed_param + return _call diff --git a/tweepy/cursor.py b/tweepy/cursor.py new file mode 100644 index 0000000..d59bab2 --- /dev/null +++ b/tweepy/cursor.py @@ -0,0 +1,75 @@ +# Tweepy +# Copyright 2009 Joshua Roesslein +# See LICENSE + +class Cursor(object): + """Pagination helper class""" + + def __init__(self, method, *args, **kargs): + if 'cursor' in method.allowed_param: + self.iterator = CursorIterator(method, args, kargs) + elif 'page' in method.allowed_param: + self.iterator = PageIterator(method, args, kargs) + else: + raise TweepError('This method does not perform pagination') + + def pages(self, limit=0): + """Return iterator for pages""" + if limit > 0: + self.iterator.limit = limit + return self.iterator + + def items(self, limit=0): + """Return iterator for items in each page""" + items_yielded = 0 + for page in self.iterator: + for item in page: + if limit > 0 and items_yielded == limit: + raise StopIteration + items_yielded += 1 + yield item + +class BaseIterator(object): + + def __init__(self, method, args, kargs): + self.method = method + self.args = args + self.kargs = kargs + self.limit = 0 + + def next(self): + raise NotImplementedError + + def prev(self): + raise NotImplementedError + + def __iter__(self): + return self + +class CursorIterator(BaseIterator): + + def next(self): + return + + def prev(self): + return + +class PageIterator(BaseIterator): + + def __init__(self, method, args, kargs): + BaseIterator.__init__(self, method, args, kargs) + self.current_page = 0 + + def next(self): + self.current_page += 1 + items = self.method(page=self.current_page, *self.args, **self.kargs) + if len(items) == 0 or (self.limit > 0 and self.current_page > self.limit): + raise StopIteration + return items + + def prev(self): + if (self.current_page == 0): + raise TweepError('Can not page back more, at first page') + self.current_page -= 1 + return self.method(page=self.current_page, *self.args, **self.kargs) + diff --git a/tweepyshell.py b/tweepyshell.py index 1d2eff3..57de31f 100755 --- a/tweepyshell.py +++ b/tweepyshell.py @@ -18,9 +18,6 @@ if len(sys.argv) != 3: exit(1) api = tweepy.API.new(auth='basic', username=sys.argv[1], password=sys.argv[2]) -if api.verify_credentials() is False: - print 'Invalid username and/or password!' - exit(1) code.interact('', local={'tweepy': tweepy, 'api': api})