Is it possible to get more than 100 tweets using the Twitter4j API?
If so can anyone point out the way to do so??
To add to Luke's approach Twitter4j does provide pagination to the queries. You can try fetching more than one pages for your query. Set results per page and the page number.
But I suggest first try the since_id
and then try pagination.
You can't load more than 100 tweet per request but i don't know why you want this, instead you can load all tweets in "Endless page" i.e loading 10 items each time the user scroll a list .
for example
Query query = new Query("stackoverflow");
query.setCount(10);// sets the number of tweets to return per page, up to a max of 100
QueryResult result = twitter.search(query);
now if you want to load the next page simple easy
if(result.hasNext())//there is more pages to load
{
query = result.nextQuery();
result = twitter.search(query);
}
and so on.
Would need to see your code to provide a code example specific to your case, but you can do this through since_id
or max_id
.
This information is for the Twitter API.
max_id
option set to the id you just found.since_id
option set to the id you just found.In Twitter4j, your Query
object has two fields that represent the above API options: sinceId
and maxId
.
When you get a response containing your first 100 results, you also get the next id with the response. This id can be used as a query parameter "next"= {the id you received from the previous call} while making the call again and it will give you the next 100 tweets.
Here is how to get ALL tweets for a user (or at least up to ~3200):
import java.util.*;
import twitter4j.*;
import twitter4j.conf.*;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("");
cb.setOAuthConsumerSecret("");
cb.setOAuthAccessToken("");
cb.setOAuthAccessTokenSecret("");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
int pageno = 1;
String user = "cnn";
List statuses = new ArrayList();
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) {
e.printStackTrace();
}
}
System.out.println("Total: "+statuses.size());
Some Java code that iterates to older pages by using lowest id might look like:
Query query = new Query("test");
query.setCount(100);
int searchResultCount;
long lowestTweetId = Long.MAX_VALUE;
do {
QueryResult queryResult = twitterInstance.search(query);
searchResultCount = queryResult.getTweets().size();
for (Status tweet : queryResult.getTweets()) {
// do whatever with the tweet
if (tweet.getId() < lowestTweetId) {
lowestTweetId = tweet.getId();
query.setMaxId(lowestTweetId);
}
}
} while (searchResultCount != 0 && searchResultCount % 100 == 0);