回复特定推文Twitter API

Vic*_*tor 75 twitter

Twitter API中有没有办法获得对特定推文的回复?谢谢

Tim*_*tle 47

据我所知,没有办法直接这样做(至少现在不行).似乎应该添加一些东西.他们最近添加了一些"转发"功能,这似乎也是合乎逻辑的.

这是一种可行的方法,首先是样本推文数据(来自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>
     ...
Run Code Online (Sandbox Code Playgroud)

status/show你能找到用户的ID.然后statuses/mentions_timeline将返回用户的状态列表.只需解析返回,寻找in_reply_to_status_id与原始推文相匹配的内容id.

  • @Dunc看起来它刚被改为`status/mentions_timeline` (3认同)

小智 42

以下是获取推文回复的程序

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


小智 19

Twitter API v2 现在使用conversation_id字段支持这一点。您可以在文档中阅读更多内容。

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

https://api.twitter.com/2/tweets?ids=1225917697675886593&tweet.fields=conversation_id
Run Code Online (Sandbox Code Playgroud)

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

https://api.twitter.com/2/tweets/search/recent?query=conversation_id:1225912275971657728
Run Code Online (Sandbox Code Playgroud)

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

  • 这可行,但“搜索/最近”端点仅返回过去 7 天的结果。“搜索/全部”端点仅限于具有学术研究访问权限的应用程序。[搜索文档](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) (2认同)

min*_*naz 8

Twitter有一个名为related_results的无证api.它会为您提供指定推文ID的回复.不确定它的实验有多可靠,但这与在Twitter网站上调用的api调用相同.

使用风险由您自己承担.:)

https://api.twitter.com/1/related_results/show/172019363942117377.json?include_entities=1
Run Code Online (Sandbox Code Playgroud)

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

  • 此API不再有效 (30认同)

lin*_*iii 8

这是我的解决方案.它利用亚伯拉罕的Twitter Oauth PHP库:https://github.com/abraham/twitteroauth

它要求您了解Twitter用户的screen_name属性以及相关推文的id_str属性.这样,您就可以从任意用户的推文中获取任意对话Feed:

*更新:刷新代码以反映对象访问与阵列访问:

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;
     }

}
Run Code Online (Sandbox Code Playgroud)

  • 这很好,但可以吃掉有价值的速率限制(每个180个).用这种方法运行180条推文...见啊! (3认同)
  • 为什么这被否决了?它完全按照所述方式工作,并且可以精确地回答问题。此外,我的方法与@vsubbotin的不同之处在于,您可以使用任何Tweeter的ID代替自己的ID。 (2认同)

Den*_*mar 7

在这里,我分享简单的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.
Run Code Online (Sandbox Code Playgroud)