#! /usr/bin/perl -w

use strict;
use HTML::TreeBuilder;
use LWP::UserAgent;

my $ua = new LWP::UserAgent;

my $url = 'http://gaim.sf.net/themes.php?action=listall&start=0&limit=300';
my $response = $ua->get($url);
die unless $response->is_success;
my $root = new HTML::TreeBuilder;
$root->parse($response->content);
# Locate first table
my $table = $root->find('table');
die unless defined $table;
my $got_headers = 0;
my @th;
# Iterate over rows
for my $tr ($table->find('tr')) {
    unless ($got_headers) {
        $got_headers = 1;
        @th = map {$_->as_text} $tr->find('th');
        next;
    }
    # Get cells
    my @td = map {$_->as_text} $tr->find('td');
    next unless @td;
    die unless @th == @td;
    my %td = map {$th[$_] => $td[$_]} (0 .. $#td);
    # Get URL
    my $url = '?';
    my $a = $tr->find('a');
    $url = $a->attr('href') if defined $a and defined $a->attr('href');
    # Emit data
    print join("\t", $td{Name}, $td{"Brief Description"},
               $td{Version}, $td{Author}, $url), "\n";
}
