在 Twitter API 中,是否有一种方法可以获取特定推文的回复?谢谢。
在 Twitter API 中,是否有一种方法可以获取特定推文的回复?谢谢。
以下是获取推文回复的步骤:
[q="to:$tweeterusername", sinceId = $tweetId]
in_reply_to_status_id_str to $tweetid
匹配的结果是该帖子的回复。据我所了解,目前没有直接实现这个的方法。看起来应该添加这个功能。他们最近添加了一些“转推”功能,似乎也应该添加这个功能。
以下是一种可能的做法,首先使用示例推文数据(来自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
。
status/mentions
可靠。 - Tim Lytlestatus/mentions_timeline
。 - Tim LytleTwitter API v2现在支持使用conversation_id
字段,您可以在文档中了解更多信息。
首先,请求该推文的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中。
Twitter有一个名为related_results的未记录API。它将为指定的推文ID提供回复。不确定其可靠性如何,因为它是实验性的,但这与在Twitter Web上调用的API调用相同。
使用需自担风险。:)
https://api.twitter.com/1/related_results/show/172019363942117377.json?include_entities=1
欲了解更多信息,请查看dev.twitter上的讨论: https://dev.twitter.com/discussions/293
这是我的解决方案。它使用了Abraham的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;
}
}
在这里,我分享一个简单的 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.
twarc replies 824077910927691778 > replies.jsonl
此外,还可以使用以下命令收集一条推文的所有回复链(即回复的回复):
twarc replies 824077910927691778 --recursive
1)statuses/update 返回最后一个状态的id(如果 include_entities 为 true) 2)然后您可以请求 statuses/mentions 并通过 in_reply_to_status_id 进行结果过滤。 后者应该等于步骤1中的特定id
related_tweets
端点。<?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
)
)
....
)
)