Toast
Toast

Reputation: 657

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.

Upvotes: 4

Views: 9165

Answers (5)

User Not Exist
User Not Exist

Reputation: 109

Twitter API does not have a function for getting replies of a tweet but you can achieve it with several steps. Here is the most efficient way for Twitter API version 7.0.


Step 1 - Get all replies. (This function can only return up to 800 most recent replies)

List<Status> replyList = twitter.getMentionsTimeline(new Paging(1, 800));


Step 2 - If a user replies to his own tweet, these replies are returned to getUserTimeline() instead of getMentionsTimeline(). Thus, you need to move these replies to the right place.

List<Status> tweetList = new ArrayList<>();
for (Status tweet : twitter.getUserTimeline()) {
    if (tweet.getInReplyToStatusId() == -1) {
        tweetList.add(tweet);
    } else {
        replyList.add(tweet);
    }
}


Step 3 - Use getInReplyToStatusId() to identify the original tweet of each reply. Make your function recursive (as shown below) if you want to get the nested replies as well.

public void getReply(Status tweet) {
    for (Status reply : replyList) {
        if (tweet.getId() == reply.getInReplyToStatusId()) {
            System.out.println(reply.getText());
            getReply(reply);
        }
    }
}

Upvotes: 0

eos1d3
eos1d3

Reputation: 413

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;
}

Upvotes: 3

n1amr
n1amr

Reputation: 178

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;
}

Upvotes: 3

Hari
Hari

Reputation: 31

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.

Upvotes: 3

Bozho
Bozho

Reputation: 597046

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

Upvotes: 4

Related Questions