Commit 9ef0c687 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Added direct message endpoints.

parent a07d56a7
......@@ -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')
......@@ -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)
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment