回复特定推文Twitter API

时间:2010-04-22 18:49:00

标签: twitter

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

12 个答案:

答案 0 :(得分:39)

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

以下是一种可能的方法,首先是样本推文数据(来自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将返回用户的状态列表。只需解析返回内容即可找到与原始推文in_reply_to_status_id匹配的id

答案 1 :(得分:35)

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

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

答案 2 :(得分:8)

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

答案 3 :(得分: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;
     }

}

答案 4 :(得分:6)

我在这里分享简单的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.

答案 5 :(得分:3)

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

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

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

答案 6 :(得分:3)

我已通过以下方式实现此目的:

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

答案 7 :(得分:2)

状态为satheesh,效果很好。这是我使用的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;

答案 8 :(得分:2)

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 添加其他字段。

答案 9 :(得分:1)

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

所以我必须创建一个解决方法,我在这里已经记录了。 http://adriancrepaz.com/twitter_conversations_api

这门课应该完全符合你的要求。 它会抓取移动网站的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
                        )
                )
             ....             
        )
)

希望它有所帮助, 阿德里安。

答案 10 :(得分:0)

由于statuss / mentions_timeline将返回20条最近的提及,因此调用效率不高,并且它的局限性如每个窗口75个请求(15分钟),为此我们可以使用user_timeline

最佳方法:1.从状态/显示中获取screen_name或user_id参数。
              2.现在使用user_timeline
GET https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=screen_name&count=count

(screen_name ==我们从状态/显示中获得的名称)
(计数== 1至最大200)
count:指定要尝试和检索的Tweets数量,每个不同的请求最多200个。

从结果中进行解析,然后返回返回值,以查找与原始推文ID匹配的in_reply_to_status_id。

显然,这并不理想,但可以正常工作。

答案 11 :(得分:-1)

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

twarc replies 824077910927691778 > replies.jsonl

还可以使用以下命令将所有回复链(对回复的回复)收集到一条推文中:

twarc replies 824077910927691778 --recursive