Mercurial > rsstweet
changeset 76:0c023d35cd80
Allows usage of multiple twitter keys
Working around api rate limit
author | nanaya <me@myconan.net> |
---|---|
date | Sun, 26 Jun 2016 16:46:02 +0900 |
parents | 585952f98978 |
children | fc2bc34e6d89 |
files | app/models/tweet.rb config/config_init.rb |
diffstat | 2 files changed, 24 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/app/models/tweet.rb Thu Jun 23 01:47:33 2016 +0900 +++ b/app/models/tweet.rb Sun Jun 26 16:46:02 2016 +0900 @@ -1,9 +1,17 @@ class Tweet def timeline(options = {}) - @timeline ||= - Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do - @client.user_timeline(@twitter_id, options) - end + start_client_config_id = @client_config_id + + begin + @timeline ||= + Rails.cache.fetch({ :timeline => @twitter_id, :options => options.dup }, :expires_in => 5.minutes) do + @client.user_timeline(@twitter_id, options) + end + rescue Twitter::Error::TooManyRequests + next_client_config_id = @client_config_id + 1 + init_client next_client_config_id + retry unless @client_config_id == start_client_config_id + end end def user @@ -18,11 +26,20 @@ end def initialize(twitter_id) + @client = init_client + @twitter_id = twitter_id + end + + def init_client(config_id = 0) + @client_config_id = config_id + config_hash_key = "twitter#{config_id}".to_sym + + return init_client(0) if $cfg[config_hash_key].nil? + @client = Twitter::REST::Client.new do |config| - $cfg[:twitter].each do |cfg_key, cfg_value| + $cfg[config_hash_key].each do |cfg_key, cfg_value| config.public_send("#{cfg_key}=", cfg_value) end end - @twitter_id = twitter_id end end
--- a/config/config_init.rb Thu Jun 23 01:47:33 2016 +0900 +++ b/config/config_init.rb Sun Jun 26 16:46:02 2016 +0900 @@ -1,5 +1,5 @@ $cfg = { - :twitter => { + :twitter0 => { :consumer_key => ENV["RT_CONSUMER_KEY"], :consumer_secret => ENV["RT_CONSUMER_SECRET"], :access_token => ENV["RT_ACCESS_TOKEN"],