Implemented search endpoint.
authorJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 04:45:52 +0000 (23:45 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 04:45:52 +0000 (23:45 -0500)
api.py
binder.py
models.py
parsers.py

diff --git a/api.py b/api.py
index b271ddf3f500839c3728a9710fcd7b8e7bb812d6..c36072fd892ae39fea1fcf5be6c57adb76190652 100644 (file)
--- a/api.py
+++ b/api.py
@@ -6,7 +6,7 @@ import base64
 
 from binder import bind_api
 from parsers import *
-from models import User, Status, DirectMessage, Friendship, SavedSearch
+from models import User, Status, DirectMessage, Friendship, SavedSearch, SearchResult
 from error import TweepError
 
 """Twitter API"""
@@ -15,7 +15,7 @@ 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,
-                'saved_search': SavedSearch}):
+                'saved_search': SavedSearch, 'search_result': SearchResult}):
     if username and password:
       self.set_credentials(username, password)
     else:
@@ -371,3 +371,13 @@ class API(object):
         parser = parse_return_true
     )(self)
 
+  """Search API"""
+
+  def search(self, *args, **kargs):
+    return bind_api(
+        host = 'search.' + self.host,
+        path = '/search.json',
+        parser = parse_search_results,
+        allowed_param = ['q', 'lang', 'rpp', 'page', 'since_id', 'geocode', 'show_user'],
+    )(self, *args, **kargs)
+
index 42b84ae4eeced22abfe2746f302b33e4d0d8fe2c..e4d82f1d59a4841c3d7949e9ceaeba8178ec470c 100644 (file)
--- a/binder.py
+++ b/binder.py
@@ -8,7 +8,7 @@ import urllib
 from parsers import parse_error
 from error import TweepError
 
-def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False):
+def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, host=None):
 
   def _call(api, *args, **kargs):
     # If require auth, throw exception if credentials not provided
@@ -22,10 +22,14 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False)
       parameters = None
 
     # Open connection
+    if host:
+      _host = host
+    else:
+      _host = api.host
     if api.secure:
-      conn = httplib.HTTPSConnection(api.host)
+      conn = httplib.HTTPSConnection(_host)
     else:
-      conn = httplib.HTTPConnection(api.host)
+      conn = httplib.HTTPConnection(_host)
 
     # Build url with parameters
     if parameters:
index 017d5bb92eebbbbe14739a2e1b01ebc60f13956b..83fb28f6f802b0d63e6ffbfe0f4c0293916644ff 100644 (file)
--- a/models.py
+++ b/models.py
@@ -30,3 +30,7 @@ class Friendship(object):
 class SavedSearch(object):
 
   pass
+
+class SearchResult(object):
+
+  pass
index 77fdce9f84a972d3ff8d06b4b150e2157f061d51..71ec6df73f1f036aee3c00bf2f3fe0975933f3fe 100644 (file)
@@ -17,6 +17,10 @@ def _parse_datetime(str):
 
   return datetime.strptime(str, '%a %b %d %H:%M:%S +0000 %Y')
 
+def _parse_search_datetime(str):
+
+  return datetime.strptime(str, '%a, %d %b %Y %H:%M:%S +0000')
+
 def _parse_user(obj, api):
 
   user = api.classes['user']()
@@ -128,6 +132,24 @@ def parse_saved_searches(data, api):
     saved_searches.append(_parse_saved_search(obj, api))
   return saved_searches
 
+def _parse_search_result(obj, api):
+
+  result = api.classes['search_result']()
+  for k,v in obj.items():
+    if k == 'created_at':
+      setattr(result, k, _parse_search_datetime(v))
+    else:
+      setattr(result, k, v)
+  return result
+
+def parse_search_results(data, api):
+
+  results = json.loads(data)['results']
+  result_objects = []
+  for obj in results:
+    result_objects.append(_parse_search_result(obj, api))
+  return result_objects
+
 def parse_json(data, api):
 
   return json.loads(data)