class TrumpcareTracker::TweetBot
Provide a tweet bot that can be run as a rake task
require 'trumpcare_tracker/tweet_bot' TrumpcareTracker::TweetBot.new
('screen_user_name') do
"Text for optional intro tweet to thread"
end
$ bundle exec rake tracker:tweet_bot
Attributes
first_tweet_block[R]
screen_name[R]
Public Class Methods
new(screen_name, &first_tweet_block)
click to toggle source
# File lib/trumpcare_tracker/tweet_bot.rb, line 17 def initialize(screen_name, &first_tweet_block) @screen_name = screen_name @first_tweet_block = first_tweet_block tweet_bot_task end
Public Instance Methods
post(rep, index, reply_to_tweet)
click to toggle source
# File lib/trumpcare_tracker/tweet_bot.rb, line 64 def post(rep, index, reply_to_tweet) puts "Sending requests to Twitter API for #{rep.official_full}" tracker = TrumpcareTracker.new(rep.official_full, rep.twitter) tweet = tracker.to_tweet(in_reply_to_status: reply_to_tweet) sleep(rand(1..5)) contacts = "#{rep.official_full}\n"\ "#{rep.office_locations.map { |off| "#{off.city} - #{off.phone}" }.join("\n")}" while contacts.length > 140 contacts = contacts.split("\n")[0..-2].join("\n") end tweet = tracker.client.update(contacts, in_reply_to_status: tweet) puts "#{index + 1} down. Pausing for some time to avoid hitting API rate limit." sleep(rand(30..60)) tweet end
tweet_bot(caucus)
click to toggle source
# File lib/trumpcare_tracker/tweet_bot.rb, line 35 def tweet_bot(caucus) tracker = TrumpcareTracker.new('TrumpCareTracker', screen_name) tweet = Twitter::Tweet.new(id: nil) first_tweet = tracker.client.update(first_tweet_block.call) if block_given? send(caucus).each_with_index do |rep, i| next if rep.twitter.nil? reply_to_tweet = if i.zero? first_tweet else tweet end begin tweet = post(rep, i, reply_to_tweet) rescue Twitter::Error => e puts e.message puts 'Waiting 5 minutes to see if the issue can be resolved.' sleep(300) tweet = post(rep, i, reply_to_tweet) rescue Twitter::Error => e puts e.message puts 'Waiting 5 more minutes to try one more time. '\ 'If there\'s another exception I\'ll let it fail' sleep(300) tweet = post(rep, i, reply_to_tweet) end end end
tweet_bot_task()
click to toggle source
# File lib/trumpcare_tracker/tweet_bot.rb, line 23 def tweet_bot_task namespace(:tracker) do namespace(:tweet_bot) do desc 'Audit Democrats Trumpcare tweet activity and post a thread of updates' task(:democrats) { tweet_bot(:democrats) } desc 'Audit Republicans Trumpcare tweet activity and post a thread of updates' task(:republicans) { tweet_bot(:republicans) } end end end