#!/usr/bin/perl
##
## A blosxom-centric del.icio.us parser that
## creates blosxom entries of your daily
## del.icio.us links.
##
## Best if cron-ed to run daily.
##
## Make sure you check out the link output area near
## the bottom of the script for formatting options
## and info. on how to access other tags.
##
## By:  Brett O'Connor (bretto AT blimpsgo90.com)
##      Mike Hostetler (thehaas AT binary.net)
##
## Last Revision Date: 2007-01-01
#############################################################
require XML::Parser;
require LWP::UserAgent;
require HTTP::Request::Common;
require Crypt::SSLeay; #for SSL connection to API
require Encode;

#your del.icio.us account info
$login = "login";
$password = "password";

#file name and location setup for ouputting to file 
$timestamp = time();
$outfile = "/path/to/file/".$timestamp.".txt";

#ouput header and footer
$header = "today's del.icio.us links\n<ul>";
$footer = "</ul>(<i>Keep your own bookmarks online at <a href=\"http://del.icio.us/\">del.icio.us.</a>)</i>";

#api call
$api_url = "https://api.del.icio.us/v1/posts/get?";

#if output sent to STDOUT this will make it unicode happy
binmode STDOUT, ":utf8";

#get todays entries
($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDat, $DayOfYear, $IsDST) =
localtime(time);
my $RealMonth = $Month + 1;
if ($RealMonth < 10)
{
    $RealMonth = "0" . $RealMonth;
}
if ($Day < 10)
{
    $Day = "0" . $Day;
}
$FixedYear = $Year + 1900;
$date = "$FixedYear-$RealMonth-$Day";

#create the output
my $parser = new XML::Parser (Style=>'Subs', Pkg=>'SubHandlers', ErrorContext=>
2);
$parser->setHandlers(Char => \&charData);

my $out;

$parser->parse(fetchPage($login,$password,$date));

#write out entry if to file
#$outputTo = 'screen'; #debugging purposes
$outputTo = 'file';
if ($out ne '') {
    if ($outputTo eq 'file') {
        open(FILE, ">:encoding(iso-8859-7)",$outfile);
        print FILE "$header";
        print FILE "$out";
        print FILE "$footer";
        close(FILE);
    } else {
        printf($header);
        printf($out);
        printf($footer);
    }
}

#subroutines
sub fetchPage {
    my ($user,$pass,$date) = @_;

    #setup user agent
    my $ua = new LWP::UserAgent;
    $ua->agent("Deloxom Server Side/0.4 " . $ua->agent);
    $ua->credentials("api.del.icio.us:443","del.icio.us API",$user,$pass);

    #setup request
    my $req = new HTTP::Request GET => $api_url."&dt=".$date."";

    #request data
    my $response = $ua->request($req);

    if ($response->is_success) {
        return $response->content();
    } else {
        print $response->error_as_HTML();
    }
}
sub charData 
{
    #kill misc data
}
#xml handling subroutines
package SubHandlers;
sub post {
    my $expat = shift; my $element = shift;
    while (@_) {
        my $att = shift;
        my $val = shift;
        $attr{$att} = $val;
    }

    ####################
    # LINK OUTPUT AREA #
    ####################

    # add to output the link preceeded by a raquo:
    $out .= "<li><a href=\"".$attr{'href'}."\">".$attr{'description'}."</a></li>";
    # extended tag output:
    if ($attr{'extended'}) {
        $out .= "<blockquote>".$attr{'extended'}."</blockquote>";
        undef $attr{'extended'};
    }

    # some other stuff you might be interested in:
    #
    # $attr{'time'} << timestamp like "2004-02-10T05:11:37Z"
    # $attr{'tag'} << tags as a space-seperated list
    #
}
sub post_ {
    # by default nothing but put any formatting 
    # you want to appear after each post here
}
#EOF