Class: HashtagRetweetBot

Inherits:
Object
  • Object
show all
Defined in:
lib/bot.rb

Overview

What we want the bot to do

1. Listen to an rss feed and store that stuff
2. Work out which tweets need to be tweeted by the bot
3. send the tweets and mark them as 'tweeted'
4. repeat ad nauseam every x seconds (as indicated, 180 is recommended)

Instance Method Summary collapse

Constructor Details

#initialize(tag, seconds = 180) ⇒ HashtagRetweetBot

Returns a new instance of HashtagRetweetBot.



22
23
24
25
26
# File 'lib/bot.rb', line 22

def initialize(tag, seconds=180)
  @tag = tag
  @seconds = seconds
  @client = nil
end

Instance Method Details

#authorizeObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bot.rb', line 28

def authorize
  if(File.exist?('config/oauth.yml'))
    puts "Found config/oauth.yml, using OAuth."
    authorize_using_oauth
  elsif(File.exist?('config/bot.yml'))
    puts "Using HTTP Auth (DEPRECATED)."
    authorize_using_httpauth
  else
    puts "No config file (neither oauth.yml nor bot.yml) found in config/, exiting..."
    exit
  end
end

#authorize_using_httpauthObject



54
55
56
57
58
59
60
# File 'lib/bot.rb', line 54

def authorize_using_httpauth
  http_config = YAML.load(File.open('config/bot.yml') { |f| f.read }).symbolize_keys!
  httpauth = Twitter::HTTPAuth.new(http_config[:login], http_config[:password])
  @client = Twitter::Base.new(httpauth)
  puts "Logged in successfully using HTTP Auth, bot should be working now."
  #@client.update("Heyo from HTTP Auth app at #{Time.now}")
end

#authorize_using_oauthObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bot.rb', line 41

def authorize_using_oauth
  oauth_config = YAML.load(File.open('config/oauth.yml') { |f| f.read }).symbolize_keys!
  oauth = Twitter::OAuth.new(oauth_config[:consumer][:token], oauth_config[:consumer][:secret])
  oauth.authorize_from_access(oauth_config[:access][:token], oauth_config[:access][:secret])
  
  @client = Twitter::Base.new(oauth)
  puts "Logged in successfully using OAuth, bot should be working now."
  #@client.update("Heyo from OAuth app at #{Time.now}")
rescue Twitter::Unauthorized
  puts "It seems the access token are no longer valid, we need to get new ones."
  regenerate_oauth(oauth_config)
end

#regenerate_oauth(oauth_config) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bot.rb', line 62

def regenerate_oauth(oauth_config)
  oauth = Twitter::OAuth.new(oauth_config[:consumer][:token], oauth_config[:consumer][:secret])
  request_token = oauth.request_token
  puts request_token.authorize_url
  puts "Visit the above link, grant access and type in PIN: "
  STDOUT.flush
  pin = STDIN.gets.chomp
  access_token = request_token.get_access_token(:oauth_verifier => pin)
  
  oauth_config[:access] = {:token => access_token.token, :secret => access_token.secret}
  
  File.open('config/oauth.yml', 'w') { |f| YAML.dump(oauth_config, f)}
  puts "New access token saved, please re-run the bot."
end

#runObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bot.rb', line 78

def run
  authorize

  adapter_attrs = YAML.load(File.open('config/database.yml') { |f| f.read })
  ActiveRecord::Base.establish_connection(adapter_attrs)

  feed_thread = Thread.new do
    while(true != false)
      begin
        # fetch the feed
        feed = get_tweets_tagged_with(@tag)
        feed.entries.reverse.each do |entry|
          tweet = Tweets.find_or_create_by_twitter_id(
                    :twitter_id => entry.id,
                    :published  => entry.published,
                    :title      => entry.title,
                    :content    => entry.content,
                    :link       => entry.url
                  )

          if tweet.tweeted.blank?
            id_extractor = /\d+$/ 
            tid = id_extractor.match(tweet.twitter_id).to_s
            @client.retweet(tid)
            puts "retweeted #{tid} at #{Time.now.to_s(:long)}" # poor mans logging
            tweet.update_attribute(:tweeted, true)
          end
        end
      rescue => exception
        puts exception.to_s
        #puts exception.backtrace
      end
      sleep(@seconds)
    end
  end

  feed_thread.join
end