【发布时间】:2020-08-18 14:41:33
【问题描述】:
我正在构建一个工具,用于获取用户时间线推文及其响应,我需要了解如何将这些对话存储在数据库中,然后在模板中呈现它们。
为了进行对话,我使用了一个 while 循环,它获取推文的“in_reply_to_status_id”,然后通过 id 检索此回复的状态对象,找到它的“in_reply_to_status_id”等,直到检索到整个对话。
这是我使用的代码:
conversation = []
while True:
response = api.get_status(id=tweet_id, tweet_mode='extended')._json
if response['in_reply_to_status_id'] == None:
break
else:
message_dict = {}
message_dict['author'] = response['user']['screen_name']
message_dict['text'] = response['full_text']
message_dict['created_at'] = response['created_at']
message_dict['author_profile_url'] = response['user']['profile_image_url']
conversation.append(message_dict)
tweet_id = response['in_reply_to_status_id']
if len(conversation) == 0:
return None
return reversed(conversation)
在我的 models.py 中,我有一个 Tweet 模型,我需要了解如何制作一个模型来存储上面脚本检索到的整个对话/对话/线程。此外,之后应该可以将对话呈现为简单的聊天对话。
我的第一个想法是在我的推文模型中添加“回复”字段并将对话存储为 JSON,但这对我来说似乎不是正确的解决方案。
【问题讨论】:
标签: django python-3.x twitter tweepy