Python – Tweepy 中的 DirectMessage 对象
Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。
直接留言
Tweepy 模块中的DirectMessage
对象包含有关地点的信息。
以下是 DirectMessage 对象中的属性列表:
- type : The type of the direct message.
- id : The ID of the direct message.
- created_timestamp : The timestamp when the direct message was created.
- message_create : The details of the contents of the direct message like sender’s ID, text, media etc.
示例:使用get_direct_message()
方法获取直接消息。
# import the module
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# ID of the direct message
id = 1271013844639313927
# fetching the direct message
direct_message = api.get_direct_message(id)
# printing the information
print("The type is : " + direct_message.type)
print("The id is : " + direct_message.id)
print("The created_timestamp is : " + direct_message.created_timestamp)
# inside message_create
print("The recipient_id is : " + direct_message.message_create['target']['recipient_id'])
print("The sender_id is : " + direct_message.message_create['sender_id'])
print("The source_app_id is : " + direct_message.message_create['source_app_id'])
print("The text is : " + str(direct_message.message_create['message_data']['text']))
print("The entities are : " + str(direct_message.message_create['message_data']['entities']))
print("The media attachment is : " + str(direct_message.message_create['message_data']['attachment']))
输出 :
The type is : message_create
The id is : 1271013844639313927
The created_timestamp is : 1591868289514
The recipient_id is : 2344960135
The sender_id is : 4801790172
The source_app_id is : 3033300
The text is : https://t.co/wn8dK13pzf
The entities are : {‘hashtags’: [], ‘symbols’: [], ‘user_mentions’: [], ‘urls’: [{‘url’: ‘https://t.co/wn8dK13pzf’, ‘expanded_url’: ‘https://twitter.com/messages/media/1271013844639313927’, ‘display_url’: ‘pic.twitter.com/wn8dK13pzf’, ‘indices’: [1, 24]}]}
The media attachment is : {‘type’: ‘media’, ‘media’: {‘id’: 1271013835181154304, ‘id_str’: ‘1271013835181154304’, ‘indices’: [1, 24], ‘media_url’: ‘https://ton.twitter.com/1.1/ton/data/dm/1271013844639313927/1271013835181154304/gIf1lwJ9.png’, ‘media_url_https’: ‘https://ton.twitter.com/1.1/ton/data/dm/1271013844639313927/1271013835181154304/gIf1lwJ9.png’, ‘url’: ‘https://t.co/wn8dK13pzf’, ‘display_url’: ‘pic.twitter.com/wn8dK13pzf’, ‘expanded_url’: ‘https://twitter.com/messages/media/1271013844639313927’, ‘type’: ‘photo’, ‘sizes’: {‘small’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}, ‘medium’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}, ‘thumb’: {‘w’: 150, ‘h’: 150, ‘resize’: ‘crop’}, ‘large’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}}}}
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。