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 [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 ',
day, RFC2822_MONTH_NAME[mon-1], year,
hour, min, sec) +
if utc?
off = utc_offset
sign = off < 0 ? '-' : '+'
sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))

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'>
<title>Masterpiece of the Day</title>
<description>A daily random masterwork painting from the ibiblio WebMuseum.</description>
<title>#{$1} #{sprintf('%s %d, %d', RFC2822_MONTH_NAME[],,}</title>
<description>&lt;img src='#{url}'&gt;</description>
</rss>""motd.xml", "w") {|file|
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 ""`


Post a Comment

<< Home