Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
# Read your tweets for messages prefixed with @foo and post to that user # # Read tyler-durden's tweets for any messages starting with '@project-mayhem' # # $ ruby twitter_bot.rb tyler-durden project-mayhem my-password # # Read tyler-durden's and cornelius's twitter feeds for @project-mayhem # # $ ruby twitter_bot.rb tyler-durden/cornelius project-mayhem my-password # # Stores the last modified and highest id in a yaml file in the pwd by the name of "cache.USERNAME" # Should block against dupes and be easy against twitter's servers require 'rubygems' require 'fjson' require 'net/http' require 'open-uri' require 'yaml' CACHE_FORMAT = "cache.%s" sources = ARGV[0] dest = ARGV[1] pass = ARGV[2] sources.split("/").each do |source| url = "http://twitter.com/statuses/user_timeline/#{source}.json" now = Time.now.utc options = {} max_id = 0 cache_file = CACHE_FORMAT % source cache = File.exist?(cache_file) ? YAML.load_file(cache_file) : {} if cache[:last_modified] options['If-Modified-Since'] = cache[:last_modified].httpdate end begin open url, options do |f| JSON.parse(f.read).each do |tweet| max_id = [tweet['id'], max_id].max next unless tweet['id'] > cache[:last_id].to_i && tweet['text'] =~ %r{^@#{dest}} status = tweet['text'].gsub(%r{@#{dest}}, '').strip Net::HTTP.post_form(URI.parse("http://#{dest}:#{pass}@twitter.com/statuses/update.json"), 'status' => status) puts "#{dest}: #{status}" end end File.open cache_file, 'w' do |f| f.write({:last_modified => now, :last_id => max_id}.to_yaml) end rescue OpenURI::HTTPError puts "No tweet! #{$!}" end end
This paste will be private.
From the Design Piracy series on my blog: