【问题标题】:Replies to a particular tweet, Twitter API回复特定推文,Twitter API
【发布时间】:2011-02-11 05:18:48
【问题描述】:

Twitter API 中是否有办法获取对特定推文的回复?谢谢

【问题讨论】:

标签: twitter


【解决方案1】:

据我了解,没有办法直接做到这一点(至少现在不是)。似乎应该添加一些东西。他们最近添加了一些“转发”功能,添加它似乎也是合乎逻辑的。

这是一种可能的方法,第一个示例推文数据(来自status/show):

<status>
  <created_at>Tue Apr 07 22:52:51 +0000 2009</created_at>
  <id>1472669360</id>
  <text>At least I can get your humor through tweets. RT @abdur: I don't mean this in a bad way, but genetically speaking your a cul-de-sac.</text>
  <source><a href="http://www.tweetdeck.com/">TweetDeck</a></source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>1401881</id>
     ...

status/show 可以找到用户的ID。然后statuses/mentions_timeline 将返回用户的状态列表。只需解析返回寻找与原始推文的id 匹配的in_reply_to_status_id

【讨论】:

  • 假设 user2 回复了 user1 的推文。要从 user1 的推文中弄清楚这一点,我需要搜索 user1 的提及。但是,如果我无法以 user1 身份进行身份验证怎么办?没有身份验证就不能公开访问吗?
  • @letronje 我不知道 - 你可以使用搜索 API 在推文中找到“@user1”,但我认为它不如使用 status/mentions 可靠。跨度>
  • 看起来雕像/提及已被弃用,所以我会解析搜索/推文?q=@screenName:dev.twitter.com/docs/api/1.1/get/search/tweets
  • @Dunc 好像刚改成status/mentions_timeline
  • @Tim 好点。但我的用例类似于 letronje 的(即推文可能来自任何人),所以我需要使用搜索。
【解决方案2】:

不是以简单实用的方式。有一个功能请求:

http://code.google.com/p/twitter-api/issues/detail?id=142

有几个第三方网站提供 API,但它们经常会错过状态。

【讨论】:

  • gnip.com 基本上是目前唯一获取 Twitter 数据的第三方场所。
【解决方案3】:

我通过以下方式实现了这一点:

1) statuses/update 返回最后一个状态的 id(如果 include_entities 为真) 2)然后您可以请求状态/提及并通过 in_reply_to_status_id 过滤结果。后者应该等于步骤 1 中的特定 id

【讨论】:

    【解决方案4】:

    Twitter 有一个名为related_results 的未记录API。它将为您提供指定推文 ID 的回复。不确定它的实验性有多可靠,但这是在 twitter 网络上调用的同一个 api 调用。

    使用风险自负。 :)

    https://api.twitter.com/1/related_results/show/172019363942117377.json?include_entities=1
    

    有关更多信息,请查看 dev.twitter 上的讨论: https://dev.twitter.com/discussions/293

    【讨论】:

    • 此 API 不再有效
    • 是的。正如 mathieu 所说,它不再活跃。它说 {u'message': u'Sorry, that page doesn't exist', u'code': 34}
    【解决方案5】:

    这是我的解决方案。它利用了 Abraham 的 Twitter Oauth PHP 库:https://github.com/abraham/twitteroauth

    它要求您知道 Twitter 用户的 screen_name 属性以及相关推文的 id_str 属性。这样,您可以从任意用户的推文中获取任意对话源:

    *更新: 刷新代码以反映对象访问与数组访问:

    function get_conversation($id_str, $screen_name, $return_type = 'json', $count = 100, $result_type = 'mixed', $include_entities = true) {
    
         $params = array(
              'q' => 'to:' . $screen_name, // no need to urlencode this!
              'count' => $count,
              'result_type' => $result_type,
              'include_entities' => $include_entities,
              'since_id' => $id_str
         );
    
         $feed = $connection->get('search/tweets', $params);
    
         $comments = array();
    
         for ($index = 0; $index < count($feed->statuses); $index++) {
              if ($feed->statuses[$index]->in_reply_to_status_id_str == $id_str) {
                   array_push($comments, $feed->statuses[$index]);
              }
         }
    
         switch ($return_type) {
         case 'array':
              return $comments;
              break;
         case 'json':
         default:
              return json_encode($comments);
              break;
         }
    
    }
    

    【讨论】:

    • 为什么这被否决了?它完全按照说明工作并准确地回答了问题。此外,我的方法与 @vsubbotin 的不同之处在于您可以使用任何 Tweeter 的 id 代替您自己的。
    • 这很好,但会占用宝贵的速率限制(每个 oauth 180)。使用这种方法运行了 180 条推文……见!
    【解决方案6】:

    几个月前我在工作中遇到了同样的问题,因为我之前在 REST V1 中使用了他们的 related_tweets 端点。

    所以我必须创建一个解决方法,我已在此处记录:
    http://adriancrepaz.com/twitter_conversations_api Mirror - Github fork

    这个类应该做你想做的事。 它会抓取移动网站的 HTML,并解析对话。用了一段时间,感觉很靠谱。

    获取对话...

    请求

    <?php
    
    require_once 'acTwitterConversation.php';
    
    $twitter = new acTwitterConversation;
    $conversation = $twitter->fetchConversion(324215761998594048);
    print_r($conversation);
    
    ?>
    

    回应

    Array
    (
        [error] => false
        [tweets] => Array
            (
                [0] => Array
                    (
                        [id] => 324214451756728320
                        [state] => before
                        [username] => facebook
                        [name] => Facebook
                        [content] => Facebook for iOS v6.0 ? Now with chat heads and stickers in private messages, and a more beautiful News Feed on iPad itunes.apple.com/us/app/faceboo?
                        [date] => 16 Apr
                        [images] => Array
                            (
                                [thumbnail] => https://pbs.twimg.com/profile_images/3513354941/24aaffa670e634a7da9a087bfa83abe6_normal.png
                                [large] => https://pbs.twimg.com/profile_images/3513354941/24aaffa670e634a7da9a087bfa83abe6.png
                            )
                    )
    
                [1] => Array
                    (
                        [id] => 324214861728989184
                        [state] => before
                        [username] => michaelschultz
                        [name] => Michael Schultz
                        [content] => @facebook good April Fools joke Facebook?.chat hasn?t changed. No new features.
                        [date] => 16 Apr
                        [images] => Array
                            (
                                [thumbnail] => https://pbs.twimg.com/profile_images/414193649073668096/dbIUerA8_normal.jpeg
                                [large] => https://pbs.twimg.com/profile_images/414193649073668096/dbIUerA8.jpeg
                            )
                    )
                 ....             
            )
    )
    

    【讨论】:

      【解决方案7】:

      这是获取推文回复的过程

      1. 当您获取推文时,存储 tweetId 即 id_str
      2. 使用 twitter 搜索 api 执行以下查询 [q="to:$tweeterusername", sinceId = $tweetId]
      3. 循环所有结果,匹配in_reply_to_status_id_str to $tweetid的结果是对帖子的回复。

      【讨论】:

        【解决方案8】:

        正如所说的那样,它工作得很好。这是我使用的 REST API 代码

        ini_set('display_errors', 1);
        require_once('TwitterAPIExchange.php');
        
        /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
        $settings = array(
            'oauth_access_token' => "xxxx",
            'oauth_access_token_secret' => "xxxx",
            'consumer_key' => "xxxx",
            'consumer_secret' => "xxxx"
        );
        
        
        
        // Your specific requirements
        $url = 'https://api.twitter.com/1.1/search/tweets.json';
        $requestMethod = 'GET';
        $getfield = '?q=to:screen_name&sinceId=twitter_id';
        
        // Perform the request
        $twitter = new TwitterAPIExchange($settings);
        $b =  $twitter->setGetfield($getfield)
                     ->buildOauth($url, $requestMethod)
                     ->performRequest();
        
        $arr = json_decode($b,TRUE);
        
        echo "Replies <pre>";
        print_r($arr);
        die;
        

        【讨论】:

          【解决方案9】:

          我在这里分享简单的 R 代码来获取特定推文的回复

          userName = "SrBachchan"
          
          ##fetch tweets from @userName timeline
          tweets = userTimeline(userName,n = 1)
          
          ## converting tweets list to DataFrame  
          tweets <- twListToDF(tweets)  
          
          ## building queryString to fetch retweets 
          queryString = paste0("to:",userName)
          
          ## retrieving tweet ID for which reply is to be fetched 
          Id = tweets[1,"id"]  
          
          ## fetching all the reply to userName
          rply = searchTwitter(queryString, sinceID = Id) 
          rply = twListToDF(rply)
          
          ## eliminate all the reply other then reply to required tweet Id  
          rply = rply[!rply$replyToSID > Id,]
          rply = rply[!rply$replyToSID < Id,]
          rply = rply[complete.cases(rply[,"replyToSID"]),]
          
          ## now rply DataFrame contains all the required replies.
          

          【讨论】:

            【解决方案10】:

            您可以在 python 中使用twarc 包来收集对推文的所有回复。

            twarc replies 824077910927691778 &gt; replies.jsonl

            此外,可以使用以下命令将所有回复链(对回复的回复)收集到推文:

            twarc replies 824077910927691778 --recursive

            【讨论】:

            • 有没有办法在 javascript 中做到这一点?
            • 我不确定,我没有检查,如果找到了会通知你的。
            【解决方案11】:

            由于 statuses/mentions_timeline 将返回 20 个最近提及的内容,因此调用效率不会那么高,并且它具有每个窗口 (15 分钟) 75 个请求等限制,为此我们可以使用 user_timeline

            最好的方法: 1. 从 status/show 中获取 screen_name 或 user_id 参数。
            2. 现在使用user_timeline
            获取https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=screen_name&count=count

            (screen_name== 我们从 status/show 得到的名字)
            (计数 == 1 到最大 200)
            count:指定要尝试和检索的推文数,每个不同请求最多 200 个。

            从结果中只需解析返回寻找与原始推文 id 匹配的 in_reply_to_status_id。

            显然,这并不理想,但它会起作用。

            【讨论】:

              【解决方案12】:

              Twitter API v2 现在使用conversation_id 字段支持此功能。你可以在docs阅读更多内容。

              首先,请求推文的conversation_id 字段。

              https://api.twitter.com/2/tweets?ids=1225917697675886593&tweet.fields=conversation_id
              

              其次,然后使用conversation_id 作为查询来搜索推文。

              https://api.twitter.com/2/tweets/search/recent?query=conversation_id:1225912275971657728
              

              这是一个最小的示例,因此您应该根据需要向 URL 添加其他字段。

              【讨论】:

                猜你喜欢
                • 2015-09-10
                • 1970-01-01
                • 2022-10-05
                • 2020-08-24
                • 2013-06-10
                • 2020-11-14
                • 2022-10-05
                • 2015-10-08
                • 1970-01-01
                相关资源
                最近更新 更多