I've been using the Liferea RSS news reader to aggregate headlines from news sites, blogs, and comics for a while now, usually content with the non-automated method of copying the target of the RSS link from a web page and pasting it into Liferea's new subscription dialog.
But today, I decided to sign up for InformationWeek's feed, and found out that they were way too cool to just have a plain RSS link; instead they have several specialized links for many desktop and online news readers, along with a generic one called USM. Liferea doesn't seem to support this format natively, so I decided to read about the format and write some kind of wrapper.
A USM feed is just a normal RSS feed, with two special parts: first, it uses the application/rss+xml content type to signal a browser that it's a special feed, and second, an Atom link element containing the URL that points to the feed. Finding this element and extracting its link target requires just a few lines of XML::Parser-based code to match the element name and attributes in a start tag handler
Once I have that URL, I can use Liferea's D-Bus interface to tell the running process to subscribe to the feed. Liferea comes with a little bash script to do this, which i ported to Perl and Net::DBus:
dbus-send --session --dest=org.gnome.feed.Reader /org/gnome/feed/Reade +r org.gnome.feed.Reader.Subscribe string:$URL
And here is the Perl script to put all this together. It takes a filename on the command line (and is thus suitable for calling from a web browser) and starts parsing it as XML. Once it finds a link that matches its criterea, it sends the URL to Liferea then exits, so as not to waste time walking through the rest of the feed.
#!/usr/bin/perl use strict; use warnings; use XML::Parser; use Net::DBus; sub start_handler { my ($expat, $elem, %attr) = @_; return unless $elem =~ /link/ && exists $attr{rel}; if ($attr{rel} eq 'self' || $attr{rel} eq 'start') { register_feed($attr{href}); exit 0; } } my $xp = new XML::Parser(Handlers => {Start => \&start_handler}); $xp->parsefile($ARGV[0]); exit 1; sub register_feed { my $url = shift; my $bus = Net::DBus->session; my $service = $bus->get_service('org.gnome.feed.Reader'); my $liferea = $service->get_object('/org/gnome/feed/Reader', 'org.gnome.feed.Reader'); $liferea->Subscribe($url); }
|
|---|