Class: TrumpcareTracker::TweetBot
- Includes:
- RakeTask::Methods
- 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 Method Summary collapse
-
#initialize(screen_name, &first_tweet_block) ⇒ TweetBot
constructor
A new instance of TweetBot.
- #post(rep, index, reply_to_tweet) ⇒ Object
Methods included from RakeTask::Methods
#audit_rep, #audit_tweets, #handles, #mentions_mapper, #reps
Constructor Details
#initialize(screen_name, &first_tweet_block) ⇒ TweetBot
Returns a new instance of TweetBot.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/trumpcare_tracker/tweet_bot.rb', line 17 def initialize(screen_name, &first_tweet_block) namespace(:tracker) do desc 'Audit Trumpcare tweet activity and post a thread of updates' task(:tweet_bot) do tracker = TrumpcareTracker.new('TrumpCareTracker', screen_name) tweet = Twitter::Tweet.new(id: nil) first_tweet = tracker.client.update(first_tweet_block.call) if block_given? reps.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 end end |
Instance Method Details
#post(rep, index, reply_to_tweet) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/trumpcare_tracker/tweet_bot.rb', line 52 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 |