From: Josh Roesslein Date: Sat, 31 Oct 2009 02:21:35 +0000 (-0500) Subject: Begin work on lists API X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c42cb458cbfe8c29c6ad23c61eae57733146680a;p=tweepy.git Begin work on lists API --- diff --git a/tweepy/api.py b/tweepy/api.py index 24e237c..85b38c0 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -7,7 +7,6 @@ import mimetypes from tweepy.binder import bind_api from tweepy.error import TweepError -from tweepy.auth import BasicAuthHandler, OAuthHandler from tweepy.parsers import * @@ -28,18 +27,6 @@ class API(object): self.retry_delay = retry_delay self.retry_errors = retry_errors - # not a good idea to touch these - self._username = None - - @staticmethod - def new(auth='basic', *args, **kargs): - if auth == 'basic': - return API(BasicAuthHandler(*args, **kargs)) - elif auth == 'oauth': - return API(OAuthHandler(*args, **kargs)) - else: - raise TweepError('Invalid auth type') - """ statuses/public_timeline Returns the 20 most recent statuses from non-protected users who @@ -289,22 +276,7 @@ class API(object): See: API.get_user() """ def me(self): - # if username not fetched, go get it... - if self._username is None: - if self.auth_handler is None: - raise TweepError('Authentication required') - - try: - user = bind_api( - path = '/account/verify_credentials.json', - parser = parse_user - )(self) - except TweepError, e: - raise TweepError('Failed to fetch username: %s' % e) - - self._username = user.screen_name - - return self.get_user(screen_name=self._username) + return self.get_user(screen_name=self.auth_handler.get_username()) """ statuses/friends @@ -542,7 +514,7 @@ class API(object): try: return bind_api( path = '/account/verify_credentials.json', - parser = parse_return_true, + parser = parse_user, require_auth = True )(self) except TweepError: @@ -952,6 +924,38 @@ class API(object): except TweepError: return False + """ Create list + + Creates a new list for the authenticated user. + + Parameters: name (required), mode + Returns: List + """ + def create_list(self, *args, **kargs): + return bind_api( + path = '/%s/lists.json' % self.auth_handler.get_username(), + method = 'POST', + parser = parse_list, + allowed_param = ['name', 'mode'], + require_auth = True + )(self, *args, **kargs) + + """ Update list + + Updates the specified list. + + Parameters: name (required), mode + Returns: List + """ + def update_list(self, slug, *args, **kargs): + return bind_api( + path = '/%s/lists/%s.json' % (self.auth_handler.get_username, slug), + method = 'POST', + parser = parse_list, + allowed_param = ['name', 'mode'], + require_auth = True + )(self, *args, **kargs) + """ search Returns tweets that match a specified query. diff --git a/tweepy/auth.py b/tweepy/auth.py index c4488fe..0a3c299 100644 --- a/tweepy/auth.py +++ b/tweepy/auth.py @@ -7,23 +7,32 @@ import base64 from tweepy import oauth from tweepy.error import TweepError +from tweepy.api import API class AuthHandler(object): def apply_auth(self, url, method, headers, parameters): """Apply authentication headers to request""" - raise NotImplemented + raise NotImplementedError + + def get_username(self): + """Return the username of the authenticated user""" + raise NotImplementedError class BasicAuthHandler(AuthHandler): def __init__(self, username, password): + self.username = username self._b64up = base64.b64encode('%s:%s' % (username, password)) def apply_auth(self, url, method, headers, parameters): headers['Authorization'] = 'Basic %s' % self._b64up + def get_username(self): + return self.username + class OAuthHandler(AuthHandler): """OAuth authentication handler""" @@ -39,6 +48,7 @@ class OAuthHandler(AuthHandler): self.request_token = None self.access_token = None self.callback = callback + self.username = None def apply_auth(self, url, method, headers, parameters): request = oauth.OAuthRequest.from_consumer_and_token( @@ -105,3 +115,13 @@ class OAuthHandler(AuthHandler): except Exception, e: raise TweepError(e) + def get_username(self): + if self.username is None: + api = API(self) + user = api.verify_credentials() + if user: + self.username = user.screen_name + else: + raise TweepError("Unable to get username, invalid oauth token!") + return self.username + diff --git a/tweepy/models.py b/tweepy/models.py index b83dcce..127aac8 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -122,6 +122,10 @@ class Retweet(Model): pass +class List(Model): + + pass + # link up default model implementations. models = { 'status': Status, @@ -131,5 +135,6 @@ models = { 'saved_search': SavedSearch, 'search_result': SearchResult, 'retweet': Retweet, + 'list': List, }