Class: TrumpcareTracker::TweetBot
- Defined in:
- lib/trumpcare_tracker/tweet_bot.rb
Overview
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
Instance Attribute Summary collapse
-
#first_tweet_block ⇒ Object
readonly
Returns the value of attribute first_tweet_block.
-
#screen_name ⇒ Object
readonly
Returns the value of attribute screen_name.
Instance Method Summary collapse
-
#initialize(screen_name, &first_tweet_block) ⇒ TweetBot
constructor
A new instance of TweetBot.
- #post(rep, index, reply_to_tweet) ⇒ Object
- #tweet_bot(caucus) ⇒ Object
- #tweet_bot_task ⇒ Object
Methods inherited from RakeTask
#audit_rep, #audit_tweets, #democrats, #handles, #house_democrats, #house_republicans, #mentions_mapper, #republicans, #senate_democrats, #senate_republicans
Constructor Details
#initialize(screen_name, &first_tweet_block) ⇒ TweetBot
Returns a new instance of TweetBot.
17 18 19 20 21 |
# 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 |
Instance Attribute Details
#first_tweet_block ⇒ Object (readonly)
Returns the value of attribute first_tweet_block.
15 16 17 |
# File 'lib/trumpcare_tracker/tweet_bot.rb', line 15 def first_tweet_block @first_tweet_block end |
#screen_name ⇒ Object (readonly)
Returns the value of attribute screen_name.
15 16 17 |
# File 'lib/trumpcare_tracker/tweet_bot.rb', line 15 def screen_name @screen_name end |
Instance Method Details
#post(rep, index, reply_to_tweet) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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. 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. 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 ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# 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 |