简体   繁体   English

Twitter4j:获取特定推文的回复列表

[英]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?是否可以使用 twitter4j 获取回复推文(或其回复)的推文列表? The twitter website and Android app have this feature.推特网站和安卓应用都有这个功能。

Here's a code I'm using in welshare这是我在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编辑:这将不再工作,因为 Twitter API v 1 现在不在使用中

You can use InReplyToStatusId field value using Status.getInReplyToStatusId()您可以使用InReplyToStatusId字段值使用Status.getInReplyToStatusId()

Use the code code below recursively to get all replies or conversations of a tweet using API v1.1:使用下面的代码递归地使用 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我在https://github.com/klinker24/Talon-for-Twitter 中找到了这样做的方法并对其进行了一些修改

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:".要从该帐户搜索对推文的回复,请使用“to:”。 See https://dev.twitter.com/rest/public/search for query operators.有关查询运算符,请参阅https://dev.twitter.com/rest/public/search

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

Twitter API does not have a function for getting replies of a tweet but you can achieve it with several steps. Twitter API 没有获取推文回复的功能,但您可以通过几个步骤来实现。 Here is the most efficient way for Twitter API version 7.0.这是 Twitter API 7.0 版最有效的方法。


Step 1 - Get all replies. 第 1 步 -获取所有回复。 (This function can only return up to 800 most recent replies) (此功能最多只能返回 800 条最近的回复)

 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() . 第 2 步 -如果用户回复他自己的推文,这些回复将返回给getUserTimeline()而不是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. 第 3 步 -使用getInReplyToStatusId()识别每个回复的原始推文。 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); } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM