Implemented saved searches endpoints.
authorJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 03:05:19 +0000 (22:05 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 03:05:19 +0000 (22:05 -0500)
api.py
models.py
parsers.py

diff --git a/api.py b/api.py
index 04df9f3ae6600b9085d3b74ece2853f42b8dfe4e..b08cfbe20e0df5a18551fae5dd0bd6fb47cbe30f 100644 (file)
--- a/api.py
+++ b/api.py
@@ -2,7 +2,7 @@ import base64
 
 from binder import bind_api
 from parsers import *
-from models import User, Status, DirectMessage, Friendship
+from models import User, Status, DirectMessage, Friendship, SavedSearch
 from error import TweepError
 
 """Twitter API"""
@@ -10,7 +10,8 @@ class API(object):
 
   def __init__(self, username=None, password=None, host='twitter.com', secure=False,
                 classes={'user': User, 'status': Status,
-                'direct_message': DirectMessage, 'friendship': Friendship}):
+                'direct_message': DirectMessage, 'friendship': Friendship,
+                'saved_search': SavedSearch}):
     if username and password:
       self.set_credentials(username, password)
     else:
@@ -300,16 +301,16 @@ class API(object):
 
   """Check if block exists"""
   def exists_block(self, **kargs):
-      try:
-        bind_api(
-            path = '/blocks/exists.json',
-            parser = parse_none,
-            allowed_param = ['id', 'user_id', 'screen_name'],
-            require_auth = True
-        )(self, **kargs)
-      except TweepError:
-        return False
-      return True
+    try:
+      bind_api(
+          path = '/blocks/exists.json',
+          parser = parse_none,
+          allowed_param = ['id', 'user_id', 'screen_name'],
+          require_auth = True
+    )(self, **kargs)
+    except TweepError:
+      return False
+    return True
 
   """Get list of users that are blocked"""
   blocks = bind_api(
@@ -319,9 +320,44 @@ class API(object):
       require_auth = True
   )
 
+  """Get list of ids of users that are blocked"""
   blocks_ids = bind_api(
       path = '/blocks/blocking/ids.json',
       parser = parse_ids,
       require_auth = True
   )
 
+  """Get list of saved searches"""
+  saved_searches = bind_api(
+      path = '/saved_searches.json',
+      parser = parse_saved_searches,
+      require_auth = True
+  )
+
+  """Get a single saved search by id"""
+  def get_saved_search(self, id):
+    return bind_api(
+        path = '/saved_searches/show/%s.json' % id,
+        parser = parse_saved_search,
+        require_auth = True
+    )(self)
+
+  """Create new saved search"""
+  create_saved_search = bind_api(
+      path = '/saved_searches/create.json',
+      method = 'POST',
+      parser = parse_saved_search,
+      allowed_param = ['query'],
+      require_auth = True
+  )
+
+  """Destroy a saved search"""
+  def destroy_saved_search(self, id):
+    return bind_api(
+        path = '/saved_searches/destroy/%s.json' % id,
+        method = 'DELETE',
+        parser = parse_saved_search,
+        allowed_param = ['id'],
+        require_auth = True
+    )(self)
+
index fda94e73b4e23a3e2545287239f37a2cb0715797..4990f507ebd4485eb43646abcec040b0cfce995e 100644 (file)
--- a/models.py
+++ b/models.py
@@ -22,3 +22,7 @@ class DirectMessage(object):
 class Friendship(object):
 
   pass
+
+class SavedSearch(object):
+
+  pass
index 233621faec6e150eca0944b25e40373f0187f392..825d2082a7d36671d66c15b6c2f70121a58821cc 100644 (file)
@@ -101,6 +101,29 @@ def parse_friendship(data, api):
 
   return source, target
 
+def _parse_saved_search(obj, api):
+
+  ss = api.classes['saved_search']()
+  ss._api = api
+  for k,v in obj.items():
+    if k == 'created_at':
+      setattr(ss, k, _parse_datetime(v))
+    else:
+      setattr(ss, k, v)
+  return ss
+
+def parse_saved_search(data, api):
+
+  return _parse_saved_search(json.loads(data), api)
+
+def parse_saved_searches(data, api):
+
+  saved_searches = []
+  saved_search = api.classes['saved_search']()
+  for obj in json.loads(data):
+    saved_searches.append(_parse_saved_search(obj, api))
+  return saved_searches
+
 def parse_bool(data, api):
 
   return json.loads(data)