Skip to content

Instantly share code, notes, and snippets.

@pdxmph
Created January 11, 2012 22:21
Show Gist options
  • Save pdxmph/1597133 to your computer and use it in GitHub Desktop.
Save pdxmph/1597133 to your computer and use it in GitHub Desktop.
autotweet an rss feed
require "rss/1.0"
require "rss/2.0"
require "open-uri"
require "yaml"
require "sqlite3"
require "bitly"
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "/Users/mike/smttools/autotweet.sqlite3"
)
class Tweet < ActiveRecord::Base
end
config = YAML.load(File.open('smt_config.yml'))
sites = config["sites"]
bitly_user = config['bitly']['user']
bitly_apikey = config['bitly']['api_key']
feed_path = "/rss/twitter-handles"
sites.each do |s|
#bitly = Bitly.new(bitly_user, bitly_apikey)
feed_url = "http://#{s[1]['url']}#{feed_path}"
content = ""
open(feed_url) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
rss.items.each do |i|
#bitly_url = bitly.shorten(new_url).short_url
bitly_url = i.link
tweet = "#{i.title} #{bitly_url}"
unless Tweet.find(:first, :conditions => ["guid = ?", i.guid])
puts "I would tweet: #{tweet}"
tweet = Tweet.new do |t|
t.site = s[1]['abbrev']
t.guid = i.guid
t.tweet = tweet
end
tweet.save
else
puts "I already tweeted this"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment