Categorify!

Today I got fed up (sorry) with my current RSS reader, and set out to find a new one. I settled on NewsFire. Unfortunately, NewsFire does not display the categories associated with each item. There are a few feeds (notably Planet Case) where I want to view this information, so I came up with a solution.

The solution is a CGI script, written in Ruby, that transforms an RSS 2.0 feed (Atom is not supported) so the categories are listed at the top of the item body. I called it "categorify.cgi" and dropped it into my machine's web server root directory.

Now, instead of subscribing to the feed directly in NewsFire, I instead subscribe to the categorify script. For example, instead of subscribing to http://planet.case.edu/rss20.xml, I would subscribe to http://localhost/~andrew/categorify.cgi?rss=http://planet.case.edu/rss20.xml.

As an added "nifty" feature, the script supports turning each category name into a link. So http://localhost/~andrew/categorify.cgi?rss=http://planet.case.edu/rss20.xml&link=http://blog.case.edu/topics/ would link each category name to its corresponding page on blog.case.edu/topics/ - handy for viewing other blog entries with the same tag.

Here's the source code of the categorify.cgi script:

#!/usr/bin/env ruby

require 'cgi'
require 'rss/2.0'
require 'open-uri'

cgi = CGI.new
rss_uri = cgi.params['rss'].first
link_uri = cgi.params['link'].first

rss_text = open(rss_uri).readlines.join
rss = RSS::Parser.parse(rss_text, false)

rss.channel.items.each do |item|
  cats = item.categories.collect do |cat|
    cat.content
  end
  if link_uri
    cats.map! do |cat|
      "<a href=\"#{link_uri}#{cat}\">#{cat}</a>"
    end
  end
  if cats.length > 0
    item.description =<<-END
<p style="font-size: 85%; margin-top: 0;">
  <strong>Tags:</strong> #{cats.join(', ')}
</p>
#{item.description}
    END
  end
end

cgi.out { rss.to_s }
Trackbacks http://blog.case.edu/ajw33/mt-tb.cgi/5821
Comments
Post a comment