Skip to content

Instantly share code, notes, and snippets.

@xtoddx
Created June 6, 2009 19:14
Show Gist options
  • Save xtoddx/124978 to your computer and use it in GitHub Desktop.
Save xtoddx/124978 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'rubygems'
gem 'twitter'
require 'twitter'
class AutoRetweet
def initialize search_phrase, config_file_name = 'config.yml'
@phrase = search_phrase
@config_file = config_file_name
@config = YAML.load(File.read(config_file_name))
end
def get_min_id
@config[:min_id]
end
def set_min_id id
old_id = @config[:min_id] || 0
return unless id > old_id
@config[:min_id] = id
File.open(@config_file, 'w'){|f| f.write(@config.to_yaml)}
end
def search
results = Twitter::Search.new.contains(@phrase).fetch.results
results.each do |r|
reply(r) unless r.from_user == @config[:username]
set_min_id(r.id)
end
end
def reply msg
auth = Twitter::HTTPAuth.new(@config[:username], @config[:password])
client = Twitter::Base.new(auth)
header = "RT @#{msg.from_user}: "
len = header.length
m = msg.text
if m.length + len > 120
m= m[0, 120 - len - 3]
m= "#{header}#{m}..."
else
m= "#{header}#{m}"
end
client.update(m, {:in_reply_to_status_id => msg.id})
end
end
config = YAML.load(File.read('config.yml'))
s = AutoRetweet.new(config[:retweet_search])
while true
s.search
sleep(30 * 60 * 60)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment