<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: xml code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 03:34:24 GMT</pubDate>
    <description>DZone Snippets: xml code</description>
    <item>
      <title>RSS Twitter Bot</title>
      <link>http://snippets.dzone.com/posts/show/3714</link>
      <description>Republish an RSS feed on a twitter account. This was the source I used to run the &lt;a href="http://twitter.com/woot"&gt;Woot Twitter Bot&lt;/a&gt; before they took it over.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'active_record'&lt;br /&gt;require 'simple-rss'&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'twitter'&lt;br /&gt;&lt;br /&gt;#twitter account to post to&lt;br /&gt;twitter_email = "yourtwitteremail@bla.com"&lt;br /&gt;twitter_password = "secret"&lt;br /&gt;&lt;br /&gt;#rss feed to post&lt;br /&gt;rss_url = "http://yoursite.com/index.xml"&lt;br /&gt;rss_user_agent = "http://twitter.com/yourbot"&lt;br /&gt;&lt;br /&gt;#sqlite db&lt;br /&gt;path_to_sqlite_db = "/PATH/TO/db.sqlite"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.logger = Logger.new(STDERR)&lt;br /&gt;ActiveRecord::Base.colorize_logging = false&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.establish_connection(&lt;br /&gt;    :adapter =&gt; "sqlite3",&lt;br /&gt;    :dbfile  =&gt; path_to_sqlite_db&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;#uncomment this section the first time to create the table&lt;br /&gt;#&lt;br /&gt;#ActiveRecord::Schema.define do&lt;br /&gt;#    create_table :item do |table|&lt;br /&gt;#        table.column :title, :string&lt;br /&gt;#        table.column :link, :string&lt;br /&gt;#    end&lt;br /&gt;#end&lt;br /&gt;&lt;br /&gt;class Item &lt; ActiveRecord::Base&lt;br /&gt;  def to_s&lt;br /&gt;    "#{self.title[0..(130-self.link.length)]} - #{self.link}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#run the beast&lt;br /&gt;rss_items = SimpleRSS.parse open(rss_url ,"User-Agent" =&gt; rss_user_agent)&lt;br /&gt;&lt;br /&gt;for item in rss_items.items&lt;br /&gt;  Item.transaction do&lt;br /&gt;    unless existing_item = Item.find(:all, :conditions =&gt; ["link=?", item.link]).first&lt;br /&gt;      twitter ||= Twitter::Base.new(twitter_email, twitter_password)&lt;br /&gt;      new_item = Item.create(:title =&gt; item.title, :link =&gt; item.link) &lt;br /&gt;      twitter.post(new_item.to_s)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Run this once with the lines uncommented to create the DB, then slap it in your crontab.</description>
      <pubDate>Thu, 22 Mar 2007 04:20:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3714</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
