Twitter api speed limit workaround

I assembled a group of users and put them in the "users" variable. I iterate over them and try to follow them using my new Twitter account. However, after about 15 years, Twitter has been haunting me for exceeding the bid limit. I want to run this again, but without the users that I have already followed. How to remove "i" from the "users" array after they have been respected, or somehow return a new array from this with users that I still have to stick to? I know methods like pop and unshift, etc., but I'm not sure where "i" comes from the "users" array. I am an eternal novice, so please provide as many details as possible

No, users are actually a "cursor", not an array, so it does not have a length method

>> users.each do |i| ?> myuseraccount.twitter.follow(i) >> end 

Twitter :: Error :: TooManyRequests: speed limit exceeded

+4
source share
3 answers

A simple hack could use the call to sleep(n) :

 >> users.each do |i| ?> myuseraccount.twitter.follow(i) ?> sleep(3) >> end 

Increase the amount of sleep mode until twitter-api stops throwing errors.

The correct solution to this problem is achieved through rate-limiting .

A possible ruby ​​solution to limit the speed of a method call would be glutton_ratelimit .

Edit - And, as Kyle noted, there is a documented solution to this problem .

The following is an extended version of this solution:

 def rate_limited_follow (account, user) num_attempts = 0 begin num_attempts += 1 account.twitter.follow(user) rescue Twitter::Error::TooManyRequests => error if num_attempts % 3 == 0 sleep(15*60) # minutes * 60 seconds retry else retry end end end >> users.each do |i| ?> rate_limited_follow(myuseraccount, i) >> end 
+7
source

There are several solutions, but the simplest one in your case is probably shift :

 while users.length > 0 do myuseraccount.twitter.follow(users.first) users.shift end 

This will remove each user from the array as it is processed.

+2
source

Here is what i did

  def self.careful(&block) begin client = get_current_client() yield client rescue Twitter::Error::TooManyRequests => error current_user= User.find_by_token(client.instance_variable_get("@oauth_token")) current_user.update_attribute(:rate_limit_at, Time.now) change_current_client() retry end end 

this block makes an api call using the current client. If it reaches the speed limit, it changes the client to another using the change_current_client () method, and then repeats the call using the new client. you can add sleep () there if you want.

It can be used as

 careful{|client| client.search("#something")} 
+1
source

Source: https://habr.com/ru/post/1439600/


All Articles