Using Twitter4j, how can I get the entire list of selected tweets by a user as particles
I want the entire list of tweets that have been selected by a Twitter user account.
I've made some code examples that will give me all posts posted by a user, but I want all the tweets that the user preferred.
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 anyone help me on this.
source to share
I have tried as shown below.
ResponseList<Status> status = twitter.getFavorites(twitterScreenName);
It gave me the user's favorite tweets, which I passed as a parameter. But the problem here is that I can only get 20 favorites even though the user has so many tweets.
ResponseList<Status> status = twitter.getFavorites(twitterScreenName, paging);
I've tried with paging, but I'm not sure how to use this paging. So I get the top 20 favorites using my first code. If anyone has tried this, please share information on how to get all the selected data for a given user.
source to share
You need to start paging from 1 and then enlarge the page. Note, however, that you will be rate limited if you exceed 15 requests in 15 minutes (or 15 * 20 = 300 statuses in 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);
source to share
One of the Twitter4J samples does just that.
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);
}
}
}
source to share