Is it possible to get more than 100 tweets?
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.
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);
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.
To get the previous 100 tweets:
- find the the lowest id in the set that you just retrieved with your query
- perform the same query with the
max_id
option set to the id you just found.
To get the next 100 tweets:
- find the the highest id in the set that you just retrieved with your query
- perform the same query with the
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
.
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());