Manindar
Manindar

Reputation: 998

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.

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..

Upvotes: 4

Views: 1837

Answers (3)

harishankarv
harishankarv

Reputation: 338

You need to start the paging with 1, and then increment the page. However, note that you will be rate limited, if you exceed 15 requests per 15 minutes (or 15* 20 = 300 statuses per 15 minutes).

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

Upvotes: 2

Manindar
Manindar

Reputation: 998

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.

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. If anybody tried this then please share the info like how to get all favorites of a given user.

Upvotes: 1

anon
anon

Reputation:

One of the Twitter4J samples does exactly this.

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

Upvotes: 1

Related Questions