From: Josh Roesslein Date: Tue, 3 Nov 2009 00:11:10 +0000 (-0600) Subject: Added Lists API methods. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9a35ff25dafb2548feb1c7aaa51461840a180459;p=tweepy.git Added Lists API methods. --- diff --git a/CHANGES b/CHANGES index c03917c..7705520 100644 --- a/CHANGES +++ b/CHANGES @@ -3,12 +3,14 @@ during upgrade will be listed here. 1.2 -> 1.3 [Future] ===================== ++ Lists API methods added + API.verify_credentials() now returns an User object if credentials are valid. Otherwise false will be returned. + API.new() removed + Removed model validation. Prone to breakage due to API changes. + Moved documentation out of api.py and into wiki. + Removed 'email' parameter from API.update_profile. No longer supported. ++ API.auth_handler -> API.auth 1.1 -> 1.2 [Current] ===================== diff --git a/tweepy/api.py b/tweepy/api.py index 7add5d9..b9337ac 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -17,7 +17,7 @@ class API(object): secure=False, api_root='', retry_count=0, retry_delay=0, retry_errors=None): # you may access these freely - self.auth_handler = auth_handler + self.auth = auth_handler self.host = host self.api_root = api_root self.cache = cache @@ -141,7 +141,7 @@ class API(object): """ Get the authenticated user """ def me(self): - return self.get_user(screen_name=self.auth_handler.get_username()) + return self.get_user(screen_name=self.auth.get_username()) """ statuses/friends """ friends = bind_api( @@ -450,26 +450,127 @@ class API(object): except TweepError: return False - """ Create list """ def create_list(self, *args, **kargs): return bind_api( - path = '/%s/lists.json' % self.auth_handler.get_username(), + path = '/%s/lists.json' % self.auth.get_username(), method = 'POST', parser = parse_list, allowed_param = ['name', 'mode'], require_auth = True )(self, *args, **kargs) - """ Update list """ + def destroy_list(self, slug): + return bind_api( + path = '/%s/lists/%s.json' % (self.auth.get_username(), slug), + method = 'DELETE', + parser = parse_list, + require_auth = True + )(self) + def update_list(self, slug, *args, **kargs): return bind_api( - path = '/%s/lists/%s.json' % (self.auth_handler.get_username, slug), + path = '/%s/lists/%s.json' % (self.auth.get_username(), slug), method = 'POST', parser = parse_list, allowed_param = ['name', 'mode'], require_auth = True )(self, *args, **kargs) + def lists(self, *args, **kargs): + return bind_api( + path = '/%s/lists.json' % self.auth.get_username(), + parser = parse_lists, + allowed_param = ['cursor'], + require_auth = True + )(self, *args, **kargs) + + def lists_memberships(self, *args, **kargs): + return bind_api( + path = '/%s/lists/memberships.json' % self.auth.get_username(), + parser = parse_lists, + allowed_param = ['cursor'], + require_auth = True + )(self, *args, **kargs) + + def list_timeline(self, owner, slug, *args, **kargs): + return bind_api( + path = '/%s/lists/%s/statuses.json' % (owner, slug), + parser = parse_statuses, + allowed_param = ['page'] + )(self, *args, **kargs) + + def get_list(self, owner, slug): + return bind_api( + path = '/%s/lists/%s.json' % (owner, slug), + parser = parse_list + )(self) + + def add_list_member(self, slug, *args, **kargs): + return bind_api( + path = '/%s/%s/members.json' % (self.auth.get_username(), slug), + method = 'POST', + parser = parse_user, + allowed_param = ['id'], + require_auth = True + )(self, *args, **kargs) + + def remove_list_member(self, slug, *args, **kargs): + return bind_api( + path = '/%s/%s/members.json' % (self.auth.get_username(), slug), + method = 'DELETE', + parser = parse_user, + allowed_param = ['id'], + require_auth = True + )(self, *args, **kargs) + + def list_members(self, owner, slug, *args, **kargs): + return bind_api( + path = '/%s/%s/members.json' % (owner, slug), + parser = parse_users, + allowed_param = ['cursor'] + )(self, *args, **kargs) + + def is_list_member(self, owner, slug, user_id): + try: + return bind_api( + path = '/%s/%s/members/%s.json' % (owner, slug, user_id), + parser = parse_user + )(self) + except TweepError: + return False + + def subscribe_list(self, owner, slug): + return bind_api( + path = '/%s/%s/subscribers.json' % (owner, slug), + method = 'POST', + parser = parse_list, + require_auth = True + )(self) + + def unsubscribe_list(self, owner, slug): + return bind_api( + path = '/%s/%s/subscribers.json' % (owner, slug), + method = 'DELETE', + parser = parse_list, + require_auth = True + )(self) + + def list_subscribers(self, owner, slug, *args, **kargs): + return bind_api( + path = '/%s/%s/subscribers.json' % (owner, slug), + parser = parse_users, + allowed_param = ['cursor'] + )(self, *args, **kargs) + + def is_subscribed_list(self, owner, slug, user_id): + try: + return bind_api( + path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id), + parser = parse_user, + )(self) + except TweepError: + return False + """ search """ def search(self, *args, **kargs): diff --git a/tweepy/binder.py b/tweepy/binder.py index 4da725c..052af22 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -26,7 +26,7 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, def _call(api, *args, **kargs): # If require auth, throw exception if credentials not provided - if require_auth and not api.auth_handler: + if require_auth and not api.auth: raise TweepError('Authentication required!') # check for post data @@ -103,8 +103,8 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, conn = httplib.HTTPConnection(_host) # Apply authentication - if api.auth_handler: - api.auth_handler.apply_auth( + if api.auth: + api.auth.apply_auth( scheme + _host + url, method, headers, parameters ) @@ -153,7 +153,7 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False, # Pass json object into parser try: - if next_cursor is not None and prev_cursor is not None: + if parameters and 'cursor' in parameters: out = parser(jobject, api), next_cursor, prev_cursor else: out = parser(jobject, api) diff --git a/tweepy/parsers.py b/tweepy/parsers.py index b856fe5..3117f9e 100644 --- a/tweepy/parsers.py +++ b/tweepy/parsers.py @@ -280,7 +280,7 @@ def parse_list(obj, api): def parse_lists(obj, api): lists = [] - for item in obj: + for item in obj['lists']: lists.append(parse_list(item, api)) return lists