我目前正在执行一个程序,它要求我使用Twitter4J收集并存储tweet。但是,我确实意识到,使用Twitter的Developer,每15分钟只能发出180个请求。
由于这个原因,我创建了一个方法,在程序获得10条tweet后停止该程序,时间为15分钟,而我的使用者和访问键则重置速率限制。然而,有时候利率限制还是会在这10条推特之间耗尽吗?所以我想要做的是改变这个方法,使它停止,因为利率限制即将耗尽。
例如..。
if (rate limit = 0){
stop program until rate limit resets
}但是,我刚才的方法只是实现了一个计数器,当计数器达到10,它就停止了,这不是很经济,也不是很有效。我认为10条推特的数量是足够的,但显然不是。这是我的方法。
public void getRateLimit() throws TwitterException{
if (count == 10){
try {
System.out.println("Rate Limit is about to be exhausted for resource...");
System.out.println("Please wait for 15 minutes, while it resets...");
TimeUnit.MINUTES.sleep(15);
count = 0;
} catch (InterruptedException e) {
System.out.println(e);
}
}我怎么能改变它,使它用尽时,利率限制即将用尽和停止,只有在它得到补充开始。谢谢你的帮助。
发布于 2016-04-18 07:05:22
我以前也遇到过同样的问题,我试着计算一次请求所花费的时间。如果这个请求低于程序等待的5500毫秒,直到它达到5500毫秒,而且对我来说是完美的,
你可以问为什么5500毫秒,这是因为180个请求15分钟使它5秒每请求。
这是我使用的代码,希望能有所帮助。
do {
final long startTime = System.nanoTime();
result = twitter.search(query);
statuses = result.getTweets();
for (Status status : statuses) {
tweet = new Tweet(status);
userProfile = new UserProfile(status.getUser());
imageDownloader.getMedia(tweet.mediaEntities);
imageDownloader.getProfilePhoto(userProfile.ProfileImageUrl);
System.out.println(tweet);
System.out.println(userProfile);
}
final long duration = System.nanoTime() - startTime;
if ((5500 - duration / 1000000) > 0) {
logger.info("Sleep for " + (6000 - duration / 1000000) + " miliseconds");
Thread.sleep((5500 - duration / 1000000));
}
} while ((query = result.nextQuery()) != null);https://stackoverflow.com/questions/35760269
复制相似问题