简体   繁体   English

使用Twitter4j如何获得微粒用户的所有收藏推文列表

[英]Using Twitter4j how do i get all the list of Favorited tweets by a particulate user

I want all the list of tweets which are favorited by a twitter user account. 我想要一个Twitter用户帐户收藏的所有推文列表。

I have done some sample code that will give me all the posts that the user posted but i want all the tweets that the user favorited. 我已经做了一些示例代码,它将向我提供用户发布的所有帖子,但我想要用户收藏的所有推文。

public List getAllTweetsOfUser(Twitter twitter, String user) {
    if (user != null && !user.trim().isEmpty()) {
        List statuses = new ArrayList();
        int pageno = 1;
        while (true) {
            try {
                int size = statuses.size();
                Paging page = new Paging(pageno++, 100);
                statuses.addAll(twitter.getUserTimeline(user, page));
                if (statuses.size() == size) {
                    break;
                }
            } catch (TwitterException e) {
            }
        }
        return statuses;
    } else {
        return null;
    }
}

Can any one help me in this.. 任何人都可以帮助我...

I have tried like below.. 我试过下面的..

ResponseList<Status> status = twitter.getFavorites(twitterScreenName);

It given me the favorite tweets of the user which i have passed as a parameter. 它给了我最喜欢的用户推文作为参数。 But the problem here is i am able to get only 20 favorites, though the user has so many tweets. 但这里的问题是我只能得到20个收藏夹,尽管用户有很多推文。

ResponseList<Status> status = twitter.getFavorites(twitterScreenName, paging);

I tried with the paging but i am not sure how to use this paging. 我尝试了分页,但我不知道如何使用这种分页。 So i am getting the top 20 favorites using my first code. 所以我使用我的第一个代码获得前20名收藏。 If anybody tried this then please share the info like how to get all favorites of a given user. 如果有人试过这个,请分享如何获得给定用户的所有收藏的信息。

You need to start the paging with 1, and then increment the page. 您需要以1开始分页,然后递增页面。 However, note that you will be rate limited, if you exceed 15 requests per 15 minutes (or 15* 20 = 300 statuses per 15 minutes). 但请注意,如果您每15分钟超过15个请求(或15 * 20 =每15分钟300个状态),您将受到速率限制。

     Paging paging = new Paging(1);
        List<Status> list;
        do{
            list = twitter.getFavorites(userID, paging);
            for (Status s : list) {
                //do stuff with s
                System.out.println(s.getText());
            }
            paging.setPage(paging.getPage() + 1);
        }while(list.size() > 0);

One of the Twitter4J samples does exactly this. 其中一个Twitter4J样本就是这样做的。

public final class GetFavorites {
    /**
     * Usage: java twitter4j.examples.favorite.GetFavorites
     *
     * @param args message
     */
    public static void main(String[] args) {
        try {
            Twitter twitter = new TwitterFactory().getInstance();
            List<Status> statuses = twitter.getFavorites();
            for (Status status : statuses) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
            }
            System.out.println("done.");
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get favorites: " + te.getMessage());
            System.exit(-1);
        }
    }
}

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

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