I need some help with Twitter API error
429:Returned in API v1.1 when a request cannot be served due to the application's rate limit having been exhausted for the resource.
I am using twitter4j and I am trying to store my friends' timelines. The thing is that at the point my code hangs (twitter bans me) don't believe I have reached the Rate Limit yet. It is important to understand in order to put properly the wait() or sleep() method.
My code is the following:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("appKey")
.setOAuthConsumerSecret("appSecret")
.setOAuthAccessToken("userKey")
.setOAuthAccessTokenSecret("userSecret")
.setUseSSL(true);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter;
twitter = tf.getInstance();
Paging paging;
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Long tweetsinceId=0L;
try{
long cursor = -1;
IDs ids = twitter.getFriendsIDs(cursor);
long[] id = ids.getIDs();
ResponseList<User> users = twitter.lookupUsers(id);
int numoftweets=1;
int count=0;
/**************START: Store the friends of the user in the list usernames - The search will be performed only for the accounts included in the list**************************************************************************************/
for (User user : users) {
numoftweets = user.getStatusesCount();
userId = user.getId();
System.out.println("INSERT INTO userinfo " +
"VALUES ("+userId+",'"+user.getName()+"','"+ user.getScreenName()+"','"+dateformat.format(user.getCreatedAt())+"',"+ user.getFavouritesCount()+","+ user.getFollowersCount()
+","+user.getFriendsCount()+",'"+ user.getDescription()+"','"+user.getLocation()+"',null,"+ numoftweets+",'"+ user.getURL()
+"','"+ user.getMiniProfileImageURL()+"',null, null, '"+user.getLang()+"',"+ user.getAccessLevel()+");");
count++;
int numberofpages = Math.round(numoftweets / 100);
for (int j=1;j<=numberofpages;j++){
if (tweetsinceId==0L){
paging = new Paging(j, 100);
}
else{
paging = new Paging(j, 100).maxId(tweetsinceId);
}
if(twitter.getUserTimeline(userId,paging)!=null){
List<Status> statusess = twitter.getUserTimeline(userId,paging);
for (Status status3 : statusess)
{
//if (count==600){
// Thread.sleep(1000);
// }
String tweet = status3.getText();
Boolean isfavourite = status3.isFavorited();
Boolean isretweet = status3.isRetweet();
Boolean isretweetedbyme=status3.isRetweetedByMe();
System.out.println("INSERT INTO tweetinfo " +
"VALUES ("+userId+","+status3.getId()+",'"+tweet+"','"+ dateformat.format(status3.getCreatedAt())
+"',null,"+ status3.getCurrentUserRetweetId()+",null,null,null,null"
+",null,"+status3.getRetweetCount()+",null,null,"+ isfavourite+","+isretweet+","+isretweetedbyme+",null,'"+ status3.getSource()+"');");
count++;
System.out.println(count);
tweetsinceId = status3.getId();
}//end for
} //end if
}//end for
}
}
catch (Exception e) {
e.getStackTrace();
}
//}catch(SQLException se){
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
So, for the above program twitter API denies access when I am receiving second user's tweets. At this point, I have 1 twitter.getFriendsIDs(cursor), 1 twitter.lookupUsers(id) and 2 twitter.getUserTimeline calls to twitter API
Am I counting wrong the number of calls?
Why does Twitter API already reach the rate limit for my application?
I have already read https://dev.twitter.com/docs/rate-limiting/1.1/limits but maybe I have something misunderstood.
Any kind of help is appreciated.