Rate limit exceeded

浪子不回头ぞ 提交于 2019-12-04 07:04:30

问题


I ran my code that get some tweets with number = 50000 tweets but after getting some of them i got this error . I reviewed the below links in the error message but couldn't get anything help

[WARNING] 
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. See Rate Limiting in API v1.1.(https://dev.twitter.com/docs/rate-limiting/1.1)
message - Rate limit exceeded
code - 88

Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=d35baff5 or
http://www.google.co.jp/search?q=12c94134
TwitterException{exceptionCode=[d35baff5-12c94134], statusCode=429, 
message=Rate limit exceeded, code=88, retryAfter=-1, 
rateLimitStatus=RateLimitStatusJSONImpl{remaining=0, limit=180, 
resetTimeInSeconds=1497756414, secondsUntilReset=148}, version=3.0.3}
at 
twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)

part 2 of error

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-
plugin:1.6.0:java (default-cli)    An exception occured while executing the Java class. 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. See Rate Limiting in API 
v1.1.(https://dev.twitter.com/docs/rate-limiting/1.1)
[ERROR] message - Rate limit exceeded

I found some similar posts without solutions except one that i didn't get it well and i'm not able to write a comment due to my reputation !


回答1:


in twitter4j there is RateLimitStatus object. You can access this object after some api calls. For example:

User user = twitter.showUser(userId);
user.getRateLimitStatus();
//OR
IDs followerIDs = twitter.getFollowersIDs(user.getScreenName(), -1);
followerIDs.getRateLimitStatus();
//OR
QueryResult result = twitter.search(query);
result.getRateLimitStatus();

And maybe you can use a function to handle rate limit like this:

private void handleRateLimit(RateLimitStatus rateLimitStatus) {
    //throws NPE here sometimes so I guess it is because rateLimitStatus can be null and add this condition
    if (rateLimitStatus != null) {
        int remaining = rateLimitStatus.getRemaining();
        int resetTime = rateLimitStatus.getSecondsUntilReset();
        int sleep = 0;
        if (remaining == 0) {
            sleep = resetTime + 1; //adding 1 more seconds
        } else {
            sleep = (resetTime / remaining) + 1; //adding 1 more seconds
        }

        try {
            Thread.sleep(sleep * 1000 > 0 ? sleep * 1000 : 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

or maybe this:

private void handleRateLimit(RateLimitStatus rateLimitStatus) {
    int remaining = rateLimitStatus.getRemaining();
    if (remaining == 0) {
        int resetTime = rateLimitStatus.getSecondsUntilReset() + 5;
        int sleep = (resetTime * 1000);
        try {
            Thread.sleep(sleep > 0 ? sleep : 0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Hope this helps.

Any other/better methods would also be appreciated.



来源:https://stackoverflow.com/questions/44611659/rate-limit-exceeded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!