From c8bcae3e738b73d8405a5c42efe1ee4333040308 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Wed, 29 Jul 2009 22:05:19 -0500 Subject: [PATCH] Implemented saved searches endpoints. --- api.py | 60 +++++++++++++++++++++++++++++++++++++++++++----------- models.py | 4 ++++ parsers.py | 23 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/api.py b/api.py index 04df9f3..b08cfbe 100644 --- 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) + diff --git a/models.py b/models.py index fda94e7..4990f50 100644 --- a/models.py +++ b/models.py @@ -22,3 +22,7 @@ class DirectMessage(object): class Friendship(object): pass + +class SavedSearch(object): + + pass diff --git a/parsers.py b/parsers.py index 233621f..825d208 100644 --- a/parsers.py +++ b/parsers.py @@ -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) -- 2.25.1