Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Fixes for GetDirectMessages, GetSentDirectMessages, PostDirectMessage and DestroyDirectMessage #712

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3033,7 +3033,7 @@ def GetDirectMessages(self,
Returns:
A sequence of twitter.DirectMessage instances
"""
url = '%s/direct_messages.json' % self.base_url
url = '%s/direct_messages/events/list.json' % self.base_url
parameters = {
'full_text': bool(full_text),
'include_entities': bool(include_entities),
Expand All @@ -3053,7 +3053,7 @@ def GetDirectMessages(self,
if return_json:
return data
else:
return [DirectMessage.NewFromJsonDict(x) for x in data]
return [DirectMessage.NewFromJsonDict(x) for x in data['events']]

def GetSentDirectMessages(self,
since_id=None,
Expand Down Expand Up @@ -3180,15 +3180,7 @@ def PostDirectMessage(self,
if return_json:
return data
else:
dm = DirectMessage(
created_at=data['event']['created_timestamp'],
id=data['event']['id'],
recipient_id=data['event']['message_create']['target']['recipient_id'],
sender_id=data['event']['message_create']['sender_id'],
text=data['event']['message_create']['message_data']['text'],
)
dm._json = data
return dm
return [DirectMessage.NewFromJsonDict(x) for x in data]

def DestroyDirectMessage(self, message_id, include_entities=True, return_json=False):
"""Destroys the direct message specified in the required ID parameter.
Expand All @@ -3206,19 +3198,13 @@ def DestroyDirectMessage(self, message_id, include_entities=True, return_json=Fa
Returns:
A twitter.DirectMessage instance representing the message destroyed
"""
url = '%s/direct_messages/destroy.json' % self.base_url
url = '%s/direct_messages/events/destroy.json' % self.base_url
data = {
'id': enf_type('message_id', int, message_id),
'include_entities': enf_type('include_entities', bool, include_entities)
}

resp = self._RequestUrl(url, 'POST', data=data)
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

if return_json:
return data
else:
return DirectMessage.NewFromJsonDict(data)
resp = self._RequestUrl(url, 'DELETE', data=data)

def CreateFriendship(self, user_id=None, screen_name=None, follow=True, retweets=True, **kwargs):
"""Befriends the user specified by the user_id or screen_name.
Expand Down Expand Up @@ -5090,7 +5076,7 @@ def _ParseAndCheckTwitter(self, json_data):
raise TwitterError({'message': "Exceeded connection limit for user"})
if "Error 401 Unauthorized" in json_data:
raise TwitterError({'message': "Unauthorized"})
raise TwitterError({'message': 'Unknown error': '{0}'.format(json_data)})
raise TwitterError({'message': 'Unknown error: {0}'.format(json_data)})
self._CheckForTwitterError(data)
return data

Expand Down
23 changes: 12 additions & 11 deletions twitter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,26 @@ class DirectMessage(TwitterModel):

def __init__(self, **kwargs):
self.param_defaults = {
'created_at': None,
'created_timestamp': None,
'id': None,
'recipient_id': None,
'sender_id': None,
'text': None,
'message_create': None,
# 'recipient_id': None,
# 'sender_id': None,
# 'text': None,
}

for (param, default) in self.param_defaults.items():
setattr(self, param, kwargs.get(param, default))

def __repr__(self):
if self.text and len(self.text) > 140:
text = "{text}[...]".format(text=self.text[:140])
else:
text = self.text
return "DirectMessage(ID={dm_id}, Sender={sender}, Created={time}, Text='{text!r}')".format(
recipient_id = self.message_create['target']['recipient_id']
sender_id = self.message_create['sender_id']
text = self.message_create['message_data']['text']
return "DirectMessage(ID={dm_id}, Sender={sender}, Recipient={recipient}, Created={timestamp}, Text='{text!r}')".format(
dm_id=self.id,
sender=self.sender_id,
time=self.created_at,
recipient=recipient_id,
sender=sender_id,
timestamp=self.created_timestamp,
text=text)


Expand Down