【发布时间】:2011-02-11 05:18:48
【问题描述】:
Twitter API 中是否有办法获取对特定推文的回复?谢谢
【问题讨论】:
标签: twitter
Twitter API 中是否有办法获取对特定推文的回复?谢谢
【问题讨论】:
标签: twitter
据我了解,没有办法直接做到这一点(至少现在不是)。似乎应该添加一些东西。他们最近添加了一些“转发”功能,添加它似乎也是合乎逻辑的。
这是一种可能的方法,第一个示例推文数据(来自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 可靠。跨度>
status/mentions_timeline
不是以简单实用的方式。有一个功能请求:
http://code.google.com/p/twitter-api/issues/detail?id=142
有几个第三方网站提供 API,但它们经常会错过状态。
【讨论】:
我通过以下方式实现了这一点:
1) statuses/update 返回最后一个状态的 id(如果 include_entities 为真) 2)然后您可以请求状态/提及并通过 in_reply_to_status_id 过滤结果。后者应该等于步骤 1 中的特定 id
【讨论】:
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
【讨论】:
这是我的解决方案。它利用了 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;
}
}
【讨论】:
几个月前我在工作中遇到了同样的问题,因为我之前在 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
)
)
....
)
)
【讨论】:
这是获取推文回复的过程
[q="to:$tweeterusername", sinceId = $tweetId]
in_reply_to_status_id_str to $tweetid的结果是对帖子的回复。 【讨论】:
正如所说的那样,它工作得很好。这是我使用的 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;
【讨论】:
我在这里分享简单的 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.
【讨论】:
您可以在 python 中使用twarc 包来收集对推文的所有回复。
twarc replies 824077910927691778 > replies.jsonl
此外,可以使用以下命令将所有回复链(对回复的回复)收集到推文:
twarc replies 824077910927691778 --recursive
【讨论】:
由于 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。
显然,这并不理想,但它会起作用。
【讨论】:
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 添加其他字段。
【讨论】: