Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter4j: Get list of replies for a certain tweet

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.

like image 258
Toast Avatar asked Dec 11 '22 23:12

Toast


2 Answers

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

like image 63
Bozho Avatar answered Dec 28 '22 15:12

Bozho


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.

like image 25
Hari Avatar answered Dec 28 '22 16:12

Hari