Added Lists API methods.
authorJosh Roesslein <jroesslein@gmail.com>
Tue, 3 Nov 2009 00:11:10 +0000 (18:11 -0600)
committerJosh Roesslein <jroesslein@gmail.com>
Tue, 3 Nov 2009 00:47:49 +0000 (18:47 -0600)
CHANGES
tweepy/api.py
tweepy/binder.py
tweepy/parsers.py

diff --git a/CHANGES b/CHANGES
index c03917cd2d18f4730c76bec287e0dd970ca4b6f4..77055208d1d2179a6cfd0e2c3947eac2c536694e 100644 (file)
--- 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]
 =====================
index 7add5d915ddd0ffb859e093e34d5e511d07c3df2..b9337ac54910b69d5fa77dbd7ccca22c9911e4c2 100644 (file)
@@ -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):
index 4da725c8c7a67d86c4b6bc3fc615201ab8faeb53..052af22634f5ca9f015b9f2da6cc5bb25da7e705 100644 (file)
@@ -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)
index b856fe54e326a1a2596835509dfcd0c738febb9a..3117f9e4802f4d3d50ee69cd71e67bb44c47dd52 100644 (file)
@@ -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