Chip's Tips for Developers

Contains coding, but not narcotic.

OPML-based feed subscriptions made easy

July 30th, 2010 12:50:12 pm pst by Sterling Camden

I’ve been a much happier feed consumer since I started using Newspipe to pump my feeds to mutt. Subscribing to new feeds, however, got a bit more complicated. I’d have to edit my OPML file and make sure I got the new element inserted with all the right syntax. Even before that, I’d have to find the link to the feed I wanted. You’d be suprised how many sites don’t provide a direct link to their feed, so I’d often have to view the page source and search for an RSS autodiscovery link.

Then a few days ago, a reader asked me if I knew of a utility that would list all the autodiscovered feeds on a site. Since I could obviosuly use the same thing myself, I wrote it. It’s called feeds.rb. You just type a command like this at the command prompt:
feeds.rb http://example.com

… and it lists out all the feeds that have autodiscovery links on that page.

The next step, of course, is to automate including one of these links in my OPML file. Yes, I’m lazy — it’s one of the traits of an efficient programmer. I created a second script, called opmlsub.rb:

opmlsub.rb myopmlfile.opml -s http://example.com > myopmlfile.new

This one takes the given URL and adds an outline element for it to the incoming OPML file, spitting the result to stdout. If the URL is a feed, it will just use it directly. If it’s an HTML page, it will look for autodiscovery links and use the first RSS 2.0 link, if available, or the first ATOM link if an RSS 2.0 link can’t be found (I have to prefer RSS 2.0, being a member of The Board).

You can also instruct opmlsub.rb where to place the new link. The option -i TEXT specifies the value of the “text” attribute of an existing “outline” entry inside which the new link will be placed, as the new last child element. If not specified or not found, then the new element will be the last child of the “body” element.

I didn’t include options for editing and deleting elements. That’s easy enough to do with vim on the rare occasions when it’s needed.

Finally, I wanted to be able to do all this while looking at a page in Firefox (or Chromium, when it’s released on FreeBSD). So I created a third script named ‘clipsub’, which takes a URL from the clipboard (using xclip) and adds it to my OPML file without any intervention. I then mapped that to a key shortcut (mod4+shift+S) in my window manager, xmonad. Because opmlsub.rb validates a subset of the feed, I don’t have to worry about accidentally invoking this when a non-URL is on the clipboard. If any error occurs, clipsub pops up the error message in an xmessage window.

So now, when I see a site to which I’d like to subscribe, I just press ctrl+L (highlight the URL in the address bar), ctrl+C (copy it to the clipboard), and mod4+shift+S (subscribe) — and the posts start magically showing up in my feeds folder in mutt.

Mercurial repository on BitBucket

download

Posted in ATOM, OPML, RSS, Ruby | 1 Comment » RSS 2.0 | Sphere it!

Better Tag Cloud