Twitter4j: Get list of replies for a certain tweet

﹥>﹥吖頭↗ 提交于 2019-11-28 22:07:43
Bozho

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

Hari

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!