<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -2,7 +2,7 @@
 # David Moreno &lt;david@axiombox.com&gt;
 #
 
-require &quot;#{File.expand_path(File.dirname(__FILE__))}/rfeed.rb&quot;
+require &quot;#{File.expand_path(File.dirname(__FILE__))}/lib/rfeed.rb&quot;
 
 task :'db:migrate' =&gt; :environment do
 	ActiveRecord::Migrator.migrate(&quot;#{File.dirname(__FILE__)}/db/migrate&quot;, ENV[&quot;VERSION&quot;] ? ENV[&quot;VERSION&quot;].to_i : nil)</diff>
      <filename>Rakefile</filename>
    </modified>
    <modified>
      <diff>@@ -10,14 +10,18 @@ raise ArgumentError, &quot;Sin comando&quot; unless ARGV[0]
 
 cmd = ARGV.shift
 case cmd
-	when &quot;update&quot;
-		fs = Feed.find :all
-		fs.each do |f|
-			f.update
+	when &quot;fetch&quot;
+		if ARGV[0]
+			fs = Feed.find ARGV[0]
+			fs.fetch
+		else
+			fs = Feed.find :all
+			fs.each do |f|
+				f.fetch
+			end
 		end
 
 	when &quot;post&quot;
-	when &quot;add&quot;
 	when &quot;new&quot;
 		raise ArgumentError, &quot;No feed specified&quot; unless ARGV[0]
 		f =	Feed.new :feed =&gt; ARGV[0]
@@ -32,6 +36,30 @@ case cmd
 			end
 		end
 
+	when /(del|delete|remove)/i
+		raise ArgumentError, &quot;No feed id specified to remove&quot; unless ARGV[0]
+		f = Feed.find ARGV[0]
+		if f.destroy
+			puts &quot;#{f.name} successfully deleted.&quot;
+		else
+			$stderr.puts &quot;ERROR:&quot;
+			f.errors.full_messages.each do |e|
+				$stderr.puts &quot; #{e}&quot;
+			end
+		end
+
+
+	when &quot;list&quot;
+		fs = Feed.find :all
+		fs.each do |f|
+			puts &quot;#{ f.id}:#{f.feed_url}&quot;
+			pp f
+		end
+	
+	when /^\d+$/
+		f = Feed.find cmd
+		pp f
+
 	else
 		raise ArgumentError, &quot;No comand found for `#{cmd}'&quot;
 end</diff>
      <filename>bin/rfeed</filename>
    </modified>
    <modified>
      <diff>@@ -8,7 +8,7 @@ class CreatePosts &lt; ActiveRecord::Migration
 			p.text :description
 			p.timestamp :date
 			p.timestamp :updated_at
-			p.timestamp :last_processed_at
+			p.timestamp :created_at
 		end
 	end
 </diff>
      <filename>db/migrate/002_create_posts.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,7 @@
 require &quot;rfeedparser&quot;
 require &quot;feedbag&quot;
 require &quot;goodies/goodies/lwr-simple&quot;
+require 'digest/md5'
 
 class Feed &lt; ActiveRecord::Base
 	has_many :posts
@@ -9,9 +10,13 @@ class Feed &lt; ActiveRecord::Base
 	validates_presence_of :link, :message =&gt; &quot;wasn't found on `feed_url'&quot;
 	validates_uniqueness_of :feed_url, :message =&gt; &quot;already exists on database&quot;
 
-	attr_accessor :fp
+	attr_accessor :fp, :changed
 
-	def parse(url)
+	def name
+		return &quot;#{self.id}:#{self.feed_url}&quot;
+	end
+
+	def parse(url, params = {})
     url = LWR::Simple.normalize(url).to_s
 
     # checking that the url is a feed:
@@ -26,13 +31,18 @@ class Feed &lt; ActiveRecord::Base
       $stderr.puts &quot;URL provided not a feed but using `#{url}'&quot;
     end
 
-		fp = FeedParser.parse url
+		fp = FeedParser.parse url, { :etag =&gt; params[:etag], :modified =&gt; params[:modified] }
 
 		if fp.status.nil? or fp.status &gt;= 400.to_s
       $stderr.puts &quot;Errors with #{url}!&quot;
       return nil
     end
 
+		if fp.status == &quot;304&quot; and fp.feed.empty? and fp.entries.empty? # not changed!
+			self.changed = false
+			return self
+		end
+
     # there's no real easy way to do this
     # so we'll only do if the feed_validator gem is available
     validate = false # assuming it's not there
@@ -56,22 +66,44 @@ class Feed &lt; ActiveRecord::Base
 		
 		self.link = fp.feed.link
 		self.title = fp.feed.title
-		self.etag = fp.etag
-		self.last_modified = fp.modified_time
+		if params[:new]
+			self.etag = nil
+			self.last_modified = nil
+		else
+			puts &quot;ahh!&quot;
+			self.etag = fp.etag
+			self.last_modified = fp.modified_time
+		end
 		self.feed_url = url
+		self.changed = true
 		self.fp = fp
+		self
 	end
 
-	def update
-		p = self.parse(self.feed_url)
+	def fetch
+		parse(self.feed_url, :etag =&gt; self.etag, :modified =&gt; self.last_modified)
+
+		unless self.changed
+			puts &quot;#{self.name}: Feed unchanged&quot;
+			return
+		end
+
+		self.fp.entries.each do |e|
+			post = Post.entry(e, self)
+		end
+
+		self.save
 
-		p.entries.each do |e|
-#			Post.new  !!!!
+		if feed.save
+			puts &quot;#{feed.name} successfully fetched!&quot;
+		else
+			puts feed.errors.full_messages
+			pp feed
 		end
 	end
 
 	def feed=(url)
-		p = self.parse(url)
+		p = self.parse(url, :new =&gt; true)
 
 		if p.nil?
 			return nil</diff>
      <filename>lib/rfeed/models/Feed.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,4 +2,34 @@ class Post &lt; ActiveRecord::Base
 	belongs_to :feed
 	validates_presence_of :entry_id, :feed_id, :title, :link
 
+	validates_uniqueness_of :entry_id
+
+	def self.entry(e, feed = nil)
+		entry_id = nil
+		if not e[&quot;id&quot;].empty? # e.id will try to trigger Object.id
+			puts &quot;using id&quot;
+			entry_id = e[&quot;id&quot;]
+		elsif not e.link.empty?
+			puts &quot;using link&quot;
+			entry_id = e.link
+		elsif not e.title.empty?
+			puts &quot;using title&quot;
+			entry_id = MD5::Digest.hexdigest(e.title)
+		elsif not e.summary.empty?
+			puts &quot;using summary&quot;
+			entry_id = MD5::Digest.hexdigest(e.summary)
+		else
+			$stderr.puts &quot;No entry_id found for #{pp e}&quot;
+      return nil
+		end
+
+		post = Post.find_or_create_by_entry_id(entry_id)
+		post.feed_id = feed[&quot;id&quot;]
+		post.link = e.link || e.links[0][&quot;href&quot;] || feed.link || &quot;#&quot;
+		post.title = e.title || e.link
+		post.description = e.description || e.content || e.summary || &quot;no content&quot;
+		post.date = e.created_time || e.updated_time || e.published_time || feed.fp.updated_time || feed.fp.modified_time || Time.now
+		return post
+	end
+
 end</diff>
      <filename>lib/rfeed/models/Post.rb</filename>
    </modified>
    <modified>
      <diff>@@ -42,3 +42,15 @@ Migrating to CreatePosts (2)
   *[4;36;1mSQL (0.2ms)*[0m   *[0;1mSELECT version FROM schema_migrations*[0m
 Migrating to CreateFeeds (1)
 Migrating to CreatePosts (2)
+  *[4;36;1mSQL (0.1ms)*[0m   *[0;1mSET SQL_AUTO_IS_NULL=0*[0m
+  *[4;35;1mSQL (0.2ms)*[0m   *[0mSHOW TABLES*[0m
+  *[4;36;1mSQL (9.3ms)*[0m   *[0;1mCREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB*[0m
+  *[4;35;1mSQL (3.9ms)*[0m   *[0mCREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)*[0m
+  *[4;36;1mSQL (0.2ms)*[0m   *[0;1mSHOW TABLES*[0m
+  *[4;35;1mSQL (0.2ms)*[0m   *[0mSELECT version FROM schema_migrations*[0m
+Migrating to CreateFeeds (1)
+  *[4;36;1mSQL (5.6ms)*[0m   *[0;1mCREATE TABLE `feeds` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `link` varchar(255), `feed_url` varchar(255), `title` varchar(255), `etag` varchar(255), `last_modified` datetime, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB*[0m
+  *[4;35;1mSQL (0.4ms)*[0m   *[0mINSERT INTO schema_migrations (version) VALUES ('1')*[0m
+Migrating to CreatePosts (2)
+  *[4;36;1mSQL (20.4ms)*[0m   *[0;1mCREATE TABLE `posts` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `entry_id` varchar(255), `feed_id` int(11), `title` varchar(255), `link` varchar(255), `description` text, `date` datetime, `updated_at` datetime, `created_at` datetime) ENGINE=InnoDB*[0m
+  *[4;35;1mSQL (0.6ms)*[0m   *[0mINSERT INTO schema_migrations (version) VALUES ('2')*[0m</diff>
      <filename>log/database.log</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>471905fe43779d13a62da0d4847e6ad7acf97715</id>
    </parent>
  </parents>
  <author>
    <name>David Moreno</name>
    <email>david@axiombox.com</email>
  </author>
  <url>http://github.com/damog/rfeed/commit/5b4f68620de7ef53f115c399d6aa2ecfb71a2293</url>
  <id>5b4f68620de7ef53f115c399d6aa2ecfb71a2293</id>
  <committed-date>2009-01-11T18:53:19-08:00</committed-date>
  <authored-date>2009-01-11T18:53:19-08:00</authored-date>
  <message>i cant stand this is broken</message>
  <tree>b835397174f307bad1ca97f0b600e810f07e18a2</tree>
  <committer>
    <name>David Moreno</name>
    <email>david@axiombox.com</email>
  </committer>
</commit>
