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:
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')
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