【问题标题】:Is there any work around on fetching twitter conversations using latest Twitter REST API v1.1使用最新的 Twitter REST API v1.1 获取 Twitter 对话是否有任何解决方法
【发布时间】:2014-09-07 15:25:06
【问题描述】:

我正在做一个需要检索 twitter 用户对话的项目。例如我想得到BBC World Service这条推文的所有回复。使用REST API v1.1,我可以获得一个推特用户的时间线(推文、转推)。但我没有找到任何关于获取特定推文回复的文档/工作解决方法。是否有任何解决方法可以获取特定推文的回复?

【问题讨论】:

    标签: api rest twitter


    【解决方案1】:

    没有 API 调用来获取对特定推文的回复。但是,你可以作弊!

    使用Search API,您可以构建一个搜索查询:

    • 回复@bbcworldservice
    • 在推文发布后发生。
    • (可选)特定日期/时间之前。

    所以,在这种情况下,像

    https://api.twitter.com/1.1/search/tweets.json?
        q=%23bbcworldservice&
        since_id=489366839953489920&
        count=100
    

    您将获得推文列表(最多 100 条)。然后,您需要在它们中搜索 in_reply_to_status_id_str 并查看它是否与您正在寻找的状态相匹配。

    【讨论】:

    • 这真的很可靠,比我能想出的任何东西都要好。
    • 截至 2017 年,此方法的问题在于 twitter 中的对话可能包含未提及实际创建者(即根推文)的推文。在这种情况下,q=%23user 方法只会从提及@user 的对话中返回推文,这可能会在向用户显示时在对话中造成潜在的空白。但是,另一方面,这仍然是获得推文回复的唯一方法。
    • 此解决方案获取过去 7 天的消息。twittercommunity.com/t/…
    【解决方案2】:

    TwitterAPI v2 允许您在搜索中仅使用 conversation_id 检索整个对话线程。 (在 v1.1 中,您必须编写自定义代码来构建它)

    对给定推文的回复以及对这些回复的回复都包含在源自单个原始推文的对话中。无论结果有多少回复线程,它们都将与引发对话的原始推文共享一个共同的 conversation_id。使用 Twitter API v2,您可以检索和重建整个对话线程,以便您更好地了解正在说什么,以及对话和想法如何演变。

    例子:

    curl --request GET \
      --url 'https://api.twitter.com/2/tweets?ids=1225917697675886593&tweet.fields=author_id,conversation_id,created_at,in_reply_to_user_id,referenced_tweets&expansions=author_id,in_reply_to_user_id,referenced_tweets.id&user.fields=name,username' \
      --header 'Authorization: Bearer $BEARER_TOKEN' 
    

    反应会是这样的

    {
        "data": [
            {
                "id": "1225917697675886593",
                "text": "@TwitterEng",
                "created_at": "2020-02-07T23:02:10.000Z",
                "author_id": "2244994945",
                "in_reply_to_user_id": "6844292",
                "conversation_id": "1225912275971657728",
                "referenced_tweets": [
                    {
                        "type": "quoted",
                        "id": "1200517737669378053"
                    },
                    {
                        "type": "replied_to",
                        "id": "1225912275971657728"
                    }
                ]
            }
        ],
        "includes": {
            "users": [
                {
                    "username": "TwitterDev",
                    "name": "Twitter Dev",
                    "id": "2244994945"
                },
                {
                    "username": "TwitterEng",
                    "name": "Twitter Engineering",
                    "id": "6844292"
                }
            ],
            "tweets": [
                {
                    "id": "1200517737669378053",
                    "text": "| ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|\n             don't push            \n             to prod on            \n               Fridays                  \n|___________| \n(\\__/)  ||\n(•ㅅ•) ||\n/   づ",
                    "created_at": "2019-11-29T20:51:47.000Z",
                    "author_id": "2244994945",
                    "conversation_id": "1200517737669378053"
                },
                {
                    "id": "1225912275971657728",
                    "text": "Note to self: Don't deploy on Fridays",
                    "created_at": "2020-02-07T22:40:37.000Z",
                    "author_id": "6844292",
                    "conversation_id": "1225912275971657728"
                }
            ]
        }
    }
    

    欲了解更多信息,请查看 twitter API Conversation

    【讨论】:

    • 有谁知道为什么上面的请求只返回两条推文,因为这条推文有 数百 条回复? twitter.com/TwitterEng/status/1225912275971657728
    • @AlexSpurling 可能是因为 api 仅检索上周的推文。 The recent search endpoint allows you to programmatically access filtered public Tweets posted over the last week, and is available via the Standard and Academic Research product tracks.
    猜你喜欢
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多