簡體   English   中英

Twitter4r 的事件循環

[英]Event loop for Twitter4r

Twitter4R 是否有一個事件循環,當用戶提到機器人的句柄時,它會通知機器人?

我試圖讓它每 5 秒檢查一次client.mentions_timeline ,看看是否有新的推文提到了機器人的句柄。 但是,它起作用了,在多次檢查client.mentions_timeline后,經常會超出速率限制。 有什么方法可以優化這個還是我遺漏了什么?

         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/users.rb:237:in `user'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/users.rb:53:in `verify_credentials'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:57:in `perform_get_with_object'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
         2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:50:in `perform_request'
         1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/request.rb:39:in `perform'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/request.rb:81:in `fail_or_return_response_body': Rate limit exceeded (Twitter::Error::TooManyRequests)

謝謝!

gem 只是 API 的一個包裝器,因此請參閱 Twitter 上的Rate Limits ,我們可以在其中找到:

GET 端點 此表中描述的標准 API 速率限制是指 GET(讀取)端點。 請注意,圖表中未列出的端點默認為每個分配的用戶 15 個請求。 所有請求窗口的長度均為 15 分鍾。 這些速率限制僅適用於標准 API 端點,不適用於高級 API。

GET statuses/mentions_timeline  statuses    75  0

所以看起來你每 15 分鍾只能擊中該端點 75 次,簡單的數學計算是

15 * 60 / 75 = 12

因此,為了安全起見,您可能不應該每 12-15 秒擊中一次以上。

您還可以隨着時間的推移跟蹤您的調用,挽救異常並在達到速率限制時重試請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM