Dorward's Ramblings

CMS

Published on

To pass some time this afternoon I decided to move forwards with the CMS a little. Now I'm on Gentoo again I have managed to get Class::DBI installed and working, so I set myself a little project to begin to get to grips with it.

I've been keeping my bookmarks over at del.icio.us, and thought it would be nice to include the most recent ones on the site. I could simply parse the RSS feed and include it, but I also want to keep a backup. Out comes MySQL and Class::DBI.

First task is to extend Class::DBI very slightly so it knows about the database table.

package SBuilder::DB::Bookmarks;
use strict;
use warnings;
use base 'Class::DBI';
SBuilder::DB::Bookmarks->connection('dbi:mysql:not', 'in', 'public');
SBuilder::DB::Bookmarks->table('bookmarks');
SBuilder::DB::Bookmarks->columns(All => qw/link title description date/);

Then we download and parse the RSS feed before inserting it into the database.

#!/usr/bin/perl
use strict;
use warnings;
use XML::RAI;
use LWP::Simple;
use SBuilder::DB::Bookmarks;
my $xml = get('http://del.icio.us/rss/dorward');
my $rai = XML::RAI->parse($xml);
foreach my $item ( @{$rai->items} ) {
    my $title = $item->title;
    my $link = $item->link;
    my $isodate = $item->issued;
    my $description = $item->description || '';
    my $db = SBuilder::DB::Bookmarks->find_or_create({link => $link});
    $db->title($title);
    $db->date($isodate);
    $db->description($description);
    $db->update;
}

I love Perl, it makes so many things so easy. At some stage I need to add support for del.icio.us tags, and suck the entire history of my bookmarks into it, but this is a nice start.

Having populated my database, I simply pick the most recent five and drop them into a template so they appear on my homepage.