Wednesday, December 01, 2004

Comments

Richard wrote a nice article on the code that generates his del.icio.us linkblog sidebar. I think it's a funny coincidence that I was working on a similar thing last night and ran into the same del.icio.us downtime that reminded him to integrate some error-checking code into his work. My code is not as feature-ful as his (related links? neat!), but it's elegant and worth a look.

We'll start using a set of modules to make the job a little easier:

#!/usr/local/bin/perl use strict ; use XML::RSS ; use XML::RSS::Parser ; use CGI qw{ :standard } ;

My code takes advantage of the fact that I keep a copy of the del.icio.us RSS feed for my bookmarks on the local server, because I tend to rebuild my homepage more often than I want to be hitting up the Del servers. I build an RSS object out of the file, and initialize a database hash reference to hold bookmark info:

my $p = new XML::RSS::Parser ; my $feed = $p->parsefile( "$HTML/feeds/delicious.xml" ) ; my $db ;

Next, iterate over all the items in the feed and store them in the database, a perl hash (python dictionary), using the date as key:

foreach my $entry ( $feed->items ) { my( $date, $description, $link, $title ) = ( "", "", "", "" ) ; foreach my $tag ( $entry->children ) { $description = $tag->value if $tag->name =~ m#description$# ; $date = $tag->value if $tag->name =~ m#date$# ; $link = $tag->value if $tag->name =~ m#link$# ; $title = $tag->value if $tag->name =~ m#title$# ; } $db->{ $date }{ 'description' } = $description ; $db->{ $date }{ 'link' } = $link ; $db->{ $date }{ 'title' } = $title ; $db->{ $date }{ 'source' } = $channel_title ; }

Now we'll start working on each item in the RSS database. At this point, I could limit it to a certain amount of items; this is actually a simplified version of what I wrote... in the actual code, I do limit to a specified number of bookmarks.

foreach my $id ( sort keys %$db ) {

For each bookmarks, we pull the info out of the database and do a little formatting, a little preparation for printing.

my %link_attributes ; my( $title, $link, $comment ) = ( $entry{ $id }{ 'title' }, $entry{ $id }{ 'link' }, $entry{ $id }{ 'description' }, ) ; $link =~ s#\&\;#\&#g ; $link_attributes{ '-href' } = $link ; $link_attributes{ '-title' } = $comment if $comment ne '' ;

The CGI module makes the output code elegant, if not easier to read for those unfamiliar with this module. Let's push a link for each bookmark onto an array stack:

push( @link_array, a( \%link_attributes, $title ) ) }

Now that we've gone through all the bookmarks, let's simply print out an unordered list, where the list item element is distributed over all the links in the array we collected up above.

print ul( { -class=>'linkblog' }, li( \@link_array ) ) ;

Ta-dah. I don't have a fancy-shmancy source repository in which to store this example, but you can download this for a while from my code directory.

0 Comments:

Post a Comment

Post a Comment

« Home