From 4d85f40c9207045209f62c3eb15827a94b6e3a32 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Wed, 29 Jul 2009 23:45:52 -0500 Subject: [PATCH] Implemented search endpoint. --- api.py | 14 ++++++++++++-- binder.py | 10 +++++++--- models.py | 4 ++++ parsers.py | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/api.py b/api.py index b271ddf..c36072f 100644 --- 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) + diff --git a/binder.py b/binder.py index 42b84ae..e4d82f1 100644 --- 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: diff --git a/models.py b/models.py index 017d5bb..83fb28f 100644 --- a/models.py +++ b/models.py @@ -30,3 +30,7 @@ class Friendship(object): class SavedSearch(object): pass + +class SearchResult(object): + + pass diff --git a/parsers.py b/parsers.py index 77fdce9..71ec6df 100644 --- a/parsers.py +++ b/parsers.py @@ -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) -- 2.25.1