Ruby Twitter Gem,超出速率限制 [英] Ruby Twitter Gem, rate limit exceeded
问题描述
我正在尝试使用 ruby gem Twitter (https://github.com/sferik/twitter) 来获取来自 Twitter api 的用户的关注者.
I am trying to use the ruby gem Twitter (https://github.com/sferik/twitter) to fetch the followers of a user from twitter api.
根据文档(https://dev.twitter.com/docs/api/1/get/followers/ids),twitter 在一个请求中返回 5000 个用户.根据速率限制设置,twitter 允许我每 15 分钟拨打 15 个电话(https://dev.twitter.com/docs/rate-limiting/1.1/limits)
As per the documentation (https://dev.twitter.com/docs/api/1/get/followers/ids), twitter returns 5000 users in one request. As per the rate limit settings twitter allows me to make 15 calls per 15 minutes (https://dev.twitter.com/docs/rate-limiting/1.1/limits)
问题:
当我检索拥有超过 75000 个关注者 (5000*15) 的用户的关注者时,我收到超出速率限制"错误.
When I retrieve followers of a user with more than 75000 followers (5000*15) I get a 'Rate limit exceeded' error.
当我使用 gem 方法 Twitter.followers_id(user) 时,我会自动获得所有关注者,而不是在 5000 桶中使用光标.我想 gem 会在内部处理这个问题,因此我无法控制或延迟这些请求.
When I user the gem method Twitter.followers_id(user), I get all the followers automagically and not in buckets of 5000 with cursor. I guess the gem takes care of this internally, and hence I am unable to control or delay these requests.
在 gem 文档中有一个限速示例 (https://github.com/sferik/twitter#rate-limiting),但我不知道这是否会处理已经检索到的用户或重新开始.
In the gem documentation there is an example of rate limiting (https://github.com/sferik/twitter#rate-limiting), but I dont know if that takes care of the already retrieved users or again starts from fresh.
我的问题是如何以及何时对此应用限制机制以吸引所有关注者?
My question is How and when do I apply a throttling mechanism to this to get all the followers?
问候
推荐答案
这个问题的解决方法很好地解释了这里.
The workaround for this problem is explained very well here.
MAX_ATTEMPTS = 3
num_attempts = 0
begin
num_attempts += 1
retweets = Twitter.retweeted_by_user("sferik")
rescue Twitter::Error::TooManyRequests => error
if num_attempts <= MAX_ATTEMPTS
# NOTE: Your process could go to sleep for up to 15 minutes but if you
# retry any sooner, it will almost certainly fail with the same exception.
sleep error.rate_limit.reset_in
retry
else
raise
end
end
这篇关于Ruby Twitter Gem,超出速率限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!