Is it possible to get a list of tweets that reply to a tweet (or to its replies) using twitter4j? The twitter website and Android app have this feature.
Here's a code I'm using in welshare
The first part gets all the tweets that twitter is displaying below the tweet, when it is opened. The rest takes care of conversations, in case the tweet is a reply to some other tweet.
RelatedResults results = t.getRelatedResults(tweetId);
List<Status> conversations = results.getTweetsWithConversation();
/////////
Status originalStatus = t.showStatus(tweetId);
if (conversations.isEmpty()) {
conversations = results.getTweetsWithReply();
}
if (conversations.isEmpty()) {
conversations = new ArrayList<Status>();
Status status = originalStatus;
while (status.getInReplyToStatusId() > 0) {
status = t.showStatus(status.getInReplyToStatusId());
conversations.add(status);
}
}
// show the current message in the conversation, if there's such
if (!conversations.isEmpty()) {
conversations.add(originalStatus);
}
EDIT: This wont work anymore as Twitter API v 1 is now not in use
You can use InReplyToStatusId field value using Status.getInReplyToStatusId()
Use the code code below recursively to get all replies or conversations of a tweet using API v1.1:
Status replyStatus = twitter.showStatus(status.getInReplyToStatusId());
System.out.println(replyStatus.getText())
Using this I could pull Tweets with all of their replies.
I found the way to do this in https://github.com/klinker24/Talon-for-Twitter and modified it a little
public ArrayList<Status> getDiscussion(Status status, Twitter twitter) {
ArrayList<Status> replies = new ArrayList<>();
ArrayList<Status> all = null;
try {
long id = status.getId();
String screenname = status.getUser().getScreenName();
Query query = new Query("@" + screenname + " since_id:" + id);
System.out.println("query string: " + query.getQuery());
try {
query.setCount(100);
} catch (Throwable e) {
// enlarge buffer error?
query.setCount(30);
}
QueryResult result = twitter.search(query);
System.out.println("result: " + result.getTweets().size());
all = new ArrayList<Status>();
do {
System.out.println("do loop repetition");
List<Status> tweets = result.getTweets();
for (Status tweet : tweets)
if (tweet.getInReplyToStatusId() == id)
all.add(tweet);
if (all.size() > 0) {
for (int i = all.size() - 1; i >= 0; i--)
replies.add(all.get(i));
all.clear();
}
query = result.nextQuery();
if (query != null)
result = twitter.search(query);
} while (query != null);
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
return replies;
}
Don't use "@" before screen name. It searches only other people mentioning the account. To search replies to the tweet from the account, use "to:". See https://dev.twitter.com/rest/public/search for query operators.
public ArrayList<Status> getReplies(String screenName, long tweetID) {
ArrayList<Status> replies = new ArrayList<>();
try {
Query query = new Query("to:" + screenName + " since_id:" + tweetID);
QueryResult results;
do {
results = twitter.search(query);
System.out.println("Results: " + results.getTweets().size());
List<Status> tweets = results.getTweets();
for (Status tweet : tweets)
if (tweet.getInReplyToStatusId() == tweetID)
replies.add(tweet);
} while ((query = results.nextQuery()) != null);
} catch (Exception e) {
e.printStackTrace();
}
return replies;
}
来源:https://stackoverflow.com/questions/11969896/twitter4j-get-list-of-replies-for-a-certain-tweet