From 0c4348e94d7f99b562f65577b38e0c28dc01651d Mon Sep 17 00:00:00 2001 From: xjxckk <17519346+xjxckk@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:02:35 +0000 Subject: [PATCH 1/2] Add files via upload --- twitter/api.py | 16 +++++----------- twitter/models.py | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/twitter/api.py b/twitter/api.py index ecba5d92..f0881a4a 100755 --- a/twitter/api.py +++ b/twitter/api.py @@ -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), @@ -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, @@ -3206,19 +3206,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. @@ -5090,7 +5084,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 diff --git a/twitter/models.py b/twitter/models.py index 861f9832..18df4666 100644 --- a/twitter/models.py +++ b/twitter/models.py @@ -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) From f520bc51efee2e43f2fd230c674d8d6d7f4b8ce0 Mon Sep 17 00:00:00 2001 From: xjxckk <17519346+xjxckk@users.noreply.github.com> Date: Thu, 17 Feb 2022 12:05:06 +0000 Subject: [PATCH 2/2] Add files via upload --- twitter/api.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/twitter/api.py b/twitter/api.py index f0881a4a..90634c38 100755 --- a/twitter/api.py +++ b/twitter/api.py @@ -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.