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"""
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:
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)
+
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
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:
class SavedSearch(object):
pass
+
+class SearchResult(object):
+
+ pass
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']()
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)