Twitter4j: Get list of replies for a certain tweet

前端 未结 5 1476
天命终不由人
天命终不由人 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:39

    I found the way to do this in https://github.com/klinker24/Talon-for-Twitter and modified it a little

    public ArrayList getDiscussion(Status status, Twitter twitter) {
        ArrayList replies = new ArrayList<>();
    
        ArrayList 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();
    
            do {
                System.out.println("do loop repetition");
    
                List 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;
    }
    

提交回复
热议问题