Yak Shaving

davorg on 2009-02-18T13:23:06

For a few months I've been playing with conky - which is a nice system for writing stuff onto a Linux desktop. I was introduced to it by a series of LifeHacker posts last year.

Just last week, they featured a really nice set-up which I wanted to go some way to recreated. The post included a link to the programs that were used to create the desktop, so it was easy to work out what was going on.

Most of the data was pulled from web feeds and converted to flat text. That's a nice approach as once you've got that working, there's no limit to the data you can use.

I was slightly disappointed, however, to see that the code included in the article had three separate scripts (one for each source used) and that they were all bash scripts which used curl to grab the feeds and load of sed and grep to extract the relevant parts. What this really needed was a generic approach.

So I reached for the Template Toolkit. And I reached for Template::Plugin::XML::RSS. And then I stopped myself. Not all web feeds are RSS these days (that's why we've largely stopped calling them RSS feeds) so XML::RSS wouldn't always be the right tool. What I really needed was XML::Feed - which handles both RSS and Atom and treats them both in the same way.

But there wasn't a Template::Plugin::XML::Feed. I say "wasn't" rather than "isn't" as there is one now - I uploaded it last night.

I didn't get much time to play with conky. But I've now got all of the tools I need. In particular, I can create simple programs like this to access web feeds.

#!/usr/bin/perl

use strict;
use warnings;

use Template;
use URI;

my $t = Template->new;
my $uri = URI->new('http://search.twitter.com/search.atom?q=@davorg');
$t->process(\*DATA, { uri => $uri })
  or die $t->error;

__END__
[% USE tweets = XML.Feed(uri);
   USE autoformat(right => 80);
   FOREACH tweet IN tweets.entries -%]
[% tweet.author %]:
[% tweet.title | autoformat -%]
[% LAST IF loop.count == 5 -%]
[% END -%]

Of course, I need to remove the hard-coded URI and put the template into a separate file. That's tonight's first little project.