From 9ef0c687a73567768f1d4bf33339bf04bee7d76e Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Sun, 5 Jul 2009 21:37:57 -0500 Subject: [PATCH] Added direct message endpoints. --- api.py | 39 +++++++++++++++++++++++++++++++++++++-- models.py | 5 +++++ parsers.py | 24 ++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 5ff79e1..79091cb 100644 --- a/api.py +++ b/api.py @@ -2,13 +2,14 @@ import base64 from binder import bind_api from parsers import * -from models import User, Status +from models import User, Status, DirectMessage """Twitter API""" class API(object): def __init__(self, username=None, password=None, host='twitter.com', secure=False, - classes={'user': User, 'status': Status}): + classes={'user': User, 'status': Status, + 'direct_message': DirectMessage}): if username and password: self._b64up = base64.b64encode('%s:%s' % (username, password)) else: @@ -103,4 +104,38 @@ class API(object): require_auth = True ) + """Get direct messages""" + direct_messages = bind_api( + path = '/direct_messages.json', + parser = parse_directmessages, + allowed_param = ['since_id', 'max_id', 'count', 'page'], + require_auth = True + ) + + """Sent direct messages""" + sent_direct_messages = bind_api( + path = '/direct_messages/sent.json', + parser = parse_directmessages, + allowed_param = ['since_id', 'max_id', 'count', 'page'], + require_auth = True + ) + + """Send direct message""" + send_direct_message = bind_api( + path = '/direct_messages/new.json', + method = 'POST', + parser = parse_dm, + allowed_param = ['user', 'text'], + require_auth = True + ) + + """Destroy direct message""" + destroy_direct_message = bind_api( + path = '/direct_messages/destroy.json', + method = 'DELETE', + parser = parse_dm, + allowed_param = ['id'], + require_auth = True + ) + api = API('jitterapp', 'josh1987') diff --git a/models.py b/models.py index 4504e50..5061ab1 100644 --- a/models.py +++ b/models.py @@ -13,3 +13,8 @@ class User(object): return self._api.friends(id=self.id, **kargs) def followers(self, **kargs): return self._api.followers(id=self.id, **kargs) + +class DirectMessage(object): + + def destroy(self): + return self._api.destroy_direct_message(id=self.id) diff --git a/parsers.py b/parsers.py index f6037cb..8d79e99 100644 --- a/parsers.py +++ b/parsers.py @@ -60,3 +60,27 @@ def parse_statuses(data, api): for obj in json.loads(data): statuses.append(_parse_status(obj, api)) return statuses + +def _parse_dm(obj, api): + + dm = api.classes['direct_message']() + dm._api = api + for k,v in obj.items(): + if k == 'sender': + setattr(dm, k, _parse_user(v, api)) + elif k == 'created_at': + setattr(dm, k, _parse_datetime(v)) + else: + setattr(dm, k, v) + return dm + +def parse_dm(data, api): + + return _parse_dm(json.loads(data), api) + +def parse_directmessages(data, api): + + directmessages = [] + for obj in json.loads(data): + directmessages.append(_parse_dm(obj, api)) + return directmessages -- 2.25.1