#!/usr/bin/perl -w
use strict;

use XML::XPath;
use DBI;
use YAML;
use Time::Local;

my $xpath = XML::XPath->new(filename => 'delicious.xml');

my $nodeset = $xpath->find('/posts/post');

binmode(STDOUT, ":utf8");

use Bookmarks;
my $bookmarks = Bookmarks->new({
    dbh => DBI->connect("dbi:SQLite:dbname=bookmarks.db", "", "", { RaiseError => 1 })
});

foreach my $node ($nodeset->get_nodelist) {
    my $timestamp = $node->getAttribute('time');
    my ($year, $month, $day, $hour, $minute, $second) = ($timestamp =~ /^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/);
    my $ctime = timegm($second, $minute, $hour, $day, $month - 1, $year);
    my %bookmark = (
        uri   => $node->getAttribute('href'),
        title => sanitize($node->getAttribute('description')),
        tags  => [ split ' ', $node->getAttribute('tag') ],
        ctime => $ctime,
    );
    print Dump(\%bookmark);
    #TODO: UTF-8 issues
    $bookmarks->add(\%bookmark);
}


# strip out Unicode control characters that are showing up in YouTube link titles
sub sanitize {
    my $string = shift;
    $string =~ s/[\x{202a}\x{202c}\x{200f}]//g;
    return $string;
}
