RSS Twitter Bot
1 2 require 'rubygems' 3 require 'active_record' 4 require 'simple-rss' 5 require 'open-uri' 6 require 'twitter' 7 8 #twitter account to post to 9 twitter_email = "yourtwitteremail@bla.com" 10 twitter_password = "secret" 11 12 #rss feed to post 13 rss_url = "http://yoursite.com/index.xml" 14 rss_user_agent = "http://twitter.com/yourbot" 15 16 #sqlite db 17 path_to_sqlite_db = "/PATH/TO/db.sqlite" 18 19 20 ActiveRecord::Base.logger = Logger.new(STDERR) 21 ActiveRecord::Base.colorize_logging = false 22 23 ActiveRecord::Base.establish_connection( 24 :adapter => "sqlite3", 25 :dbfile => path_to_sqlite_db 26 ) 27 28 #uncomment this section the first time to create the table 29 # 30 #ActiveRecord::Schema.define do 31 # create_table :item do |table| 32 # table.column :title, :string 33 # table.column :link, :string 34 # end 35 #end 36 37 class Item < ActiveRecord::Base 38 def to_s 39 "#{self.title[0..(130-self.link.length)]} - #{self.link}" 40 end 41 end 42 43 #run the beast 44 rss_items = SimpleRSS.parse open(rss_url ,"User-Agent" => rss_user_agent) 45 46 for item in rss_items.items 47 Item.transaction do 48 unless existing_item = Item.find(:all, :conditions => ["link=?", item.link]).first 49 twitter ||= Twitter::Base.new(twitter_email, twitter_password) 50 new_item = Item.create(:title => item.title, :link => item.link) 51 twitter.post(new_item.to_s) 52 end 53 end 54 end
Run this once with the lines uncommented to create the DB, then slap it in your crontab.