Perl Programming Page 2 - Build a Perl RSS Aggregator with Templating Tools |
With this knowledge, putting together our RSS aggregator is pretty trivial; first, we grab all the feeds we're interested in, then sort out their stories and put them into a data structure suitable for feeding to a <TMPL_LOOP>. We'll use LWP and XML::RSS to obtain and parse the RSS feeds. In our example, we're going to pretend that we're behind a pretty impressive web cache, so we have no problems fetching the RSS feeds repeatedly; in real life, you may want to save the XML to files with fixed names and check how old the files on disk are before fetching them from the web again. We'll start our RSS aggregator by writing a little Perl program to grab and organize the feeds: #!/usr/bin/perl use LWP::Simple; STORY_NAME => $item->{'title'}, @stories = sort { $b->{STORY_DATE} cmp $a->{STORY_DATE} } @stories; __DATA__ Next we need to design a template to receive this list of feeds. Now, I'm an abysmal HTML designer, which is why I like templates so much. I can create something rough that does the job and hand it to someone with imagination to do the presentation bits. So here's a rough-and-ready template: <html> <TMPL_LOOP STORIES> (Notice that we're using short forms of the pseudotags: it's OK to say SOME_VARIABLE instead of NAME=SOME_VARIABLE where it's unambiguous.) Finally, we put the finishing touches on our driver program, which merely takes the array we generated and feeds it to HTML::Template: #!/usr/bin/perl use LWP::Simple; my @stories; while (<DATA>) { STORY_NAME => $item->{'title'}, my $template = HTML::Template->new(filename => "aggregator.tmpl"); $template->param( STORIES => [ delete $_->{STORY_DATE} for @stories; print "Content-Type: text/html\n\n", $template->output; __DATA__ We need to delete the STORY_DATE once we've used it for ordering, as HTML::Template gets irate if we have loop variables that we don't use in our template. Plug this into a CGI-enabled web server, and, lo and behold, we have a cheap and cheerful Amphetadesk clone.
blog comments powered by Disqus |
|
|
|
|
|
|
|