超过限速 [英] Rate limit exceeded
问题描述
我运行了我的代码,得到了一些带有 number = 50000 条推文的推文,但在得到其中一些推文后,我收到了这个错误.我查看了错误消息中的以下链接,但没有得到任何帮助
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)
错误的第二部分
[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 !
推荐答案
在 twitter4j
中有 RateLimitStatus
对象.您可以在一些 api 调用后访问此对象.例如:
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();
}
}
}
或者这个:
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();
}
}
}
希望这会有所帮助.
任何其他/更好的方法也将不胜感激.
Any other/better methods would also be appreciated.
这篇关于超过限速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!