我运行了我的代码,获得了一些数字= 50000条推文的推文,但在得到其中一些后我得到了这个错误。我在错误消息中查看了以下链接,但无法获得任何帮助
[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)
错误的第2部分
[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
我找到了一些没有解决方案的类似帖子,除了一个我没有做好的帖子,由于我的声誉,我无法写评论!
答案 0 :(得分:2)
twitter4j
中的有RateLimitStatus
个对象。您可以在一些api调用后访问此对象。例如:
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();
也许您可以使用函数来处理速率限制,如下所示:
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();
}
}
}
希望这有帮助。
任何其他/更好的方法也将受到赞赏。