Twitter4j: Get list of replies for a certain tweet

前端 未结 5 1471
天命终不由人
天命终不由人 2020-12-15 01:59

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.

相关标签:
5条回答
  • 2020-12-15 02:21

    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

    0 讨论(0)
  • 2020-12-15 02:33

    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;
    }
    
    0 讨论(0)
  • 2020-12-15 02:39

    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;
    }
    
    0 讨论(0)
  • 2020-12-15 02:40

    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.

    0 讨论(0)
  • 2020-12-15 02:44

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题