Twitter API中是否有办法获取对特定推文的回复?谢谢
Answers:
据我了解,没有办法直接做到这一点(至少现在不是这样)。好像应该添加一些东西。他们最近添加了一些“转发”功能,似乎也可以添加此功能。
这是一种可行的方法,首先对tweet数据进行采样(来自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
与原始tweet匹配的内容id
。
status/mentions
。
status/mentions_timeline
这是我的解决方案。它利用了亚伯拉罕的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;
}
}
Twitter有一个未公开的api,名为related_results。它将为您提供对指定tweet ID的答复。不确定实验的可靠性如何,但是这与Twitter网络上的api调用相同。
使用风险自负。:)
https://api.twitter.com/1/related_results/show/172019363942117377.json?include_entities=1
有关更多信息,请查看关于dev.twitter的讨论:https ://dev.twitter.com/discussions/293
在这里,我分享了简单的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.
并非以简单实用的方式。有一个功能请求:
http://code.google.com/p/twitter-api/issues/detail?id=142
有几个提供API的第三方网站,但它们通常会丢失状态。
作为国家的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;
几个月前,我在工作中遇到了相同的问题,就像我以前related_tweets
在REST V1中使用它们的终结点一样。
所以我不得不创建一个解决办法,我已经在这里记载:
http://adriancrepaz.com/twitter_conversations_api 镜子- Github上叉
此类应完全按照您的要求进行。它会抓取移动网站的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
)
)
....
)
)
您可以在python中使用twarc包来收集对tweet的所有答复。
twarc replies 824077910927691778 > replies.jsonl
另外,可以使用以下命令将所有回复链(对回复的回复)收集到一条推文中:
twarc replies 824077910927691778 --recursive
由于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 ==我们从状态/显示获得的名称)
(count == 1到最大200)
count:指定要尝试检索的Tweets数量,每个不同的请求最多200个。
从结果中进行解析,然后返回与原始tweet的ID相匹配的in_reply_to_status_id。
显然,这并不理想,但是会起作用。