Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Monday, February 21, 2005

Rolling Your Own RSS Feed: Be Sure To Use pubDate And lastBuildDate!



Well, I'm pretty happy with the results of my first RSS feed created from scratch. It's called Masterpiece of the Day, and it showcases a new famous painting every day, from the archives of the ibiblio WebMuseum. I do want to share this technical tidbit however, for other people embarking on producing their own RSS feeds from scratch.

You must include the optional pubDate and lastBuildDate tags. If you don't, then the very popular RSS aggregator Bloglines will fail to pick up your new posts, and you will be very frustrated and you will curse RSS for several hours.

So be good to yourself. Be good to your heart. And include the pubDate and lastBuildDate in your RSS feed. Check out mine for an example.

BTW here is an updated Ruby script that gets run every day to generate the feed. It starts off with some code to enhance the Time class to format dates in RFC 822 format. I love how Ruby allows you to add methods to any class at runtime.


#Time code from http://www.ruby-doc.org/stdlib/libdoc/time/rdoc/classes/Time.html [Jon Aquino 2005-02-20]
RFC2822_DAY_NAME = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
RFC2822_MONTH_NAME = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
class Time
def rfc2822
sprintf('%s, %02d %s %d %02d:%02d:%02d ',
RFC2822_DAY_NAME[wday],
day, RFC2822_MONTH_NAME[mon-1], year,
hour, min, sec) +
if utc?
'-0000'
else
off = utc_offset
sign = off < 0 ? '-' : '+'
sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
end
end
end



urls = File.readlines("motd.txt")
url = urls[rand(urls.size)].strip
url =~ /.*\/auth\/(.*).jpg/
#Include the date in the title, to make the title unique,
#so that feedreaders won't think it is the same as an
#older item, if the painting has appeared before.
#[Jon Aquino 2005-02-21]
rss = "<?xml version='1.0'?>
<rss version='0.91'>
<channel>
<title>Masterpiece of the Day</title>
<link>http://jonaquino.blogspot.com/2005/02/masterpiece-of-day.html</link>
<description>A daily random masterwork painting from the ibiblio WebMuseum.</description>
<pubDate>#{Time.new.rfc2822}</pubDate>
<lastBuildDate>#{Time.new.rfc2822}</lastBuildDate>
<item>
<title>#{$1} #{sprintf('%s %d, %d', RFC2822_MONTH_NAME[Time.new.mon-1], Time.new.day, Time.new.year)}</title>
<link>#{url}</link>
<description>&lt;img src='#{url}'&gt;</description>
<pubDate>#{Time.new.rfc2822}</pubDate>
</item>
</channel>
</rss>"
File.open("motd.xml", "w") {|file|
file.write(rss)
}
puts rss
puts `ftp -i -n -s:"motd.ftp"`
#Hit the URL. Hopefully this will flush any caching going on
#on my ISP's server. Because I'm finding that Bloglines is not
#picking up new items. [Jon Aquino 2005-02-20]
puts `lynx -source -reload "http://members.shaw.ca/Jon_Aquino/motd.xml"`

2 Comments:

Post a Comment

<< Home