?

Log in

No account? Create an account
Daily links digest Perl script (Version 0.3) - Z303 [entries|archive|friends|userinfo]
Zoe

[ website | Z303 ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Daily links digest Perl script (Version 0.3) [Apr. 18th, 2005|06:09 pm]
Zoe
[Current Music |Tag's Trance Trip on Soma FM]

So over the weekend I played around with the daily links script some more and I'm mostly happy with how it looks. It all seem fairly robust (this is the point where is dies horribly) with no real changes from the last time I played with the code, just some more testing and tidy ups.

This is what the current Todo list looks like:

Todos
  • Make sure it follows the rules on del.icio.us/doc/api. It should be mostly ok with the list but double checking would not be a bad idea.
  • Check the del.icio.us accounts last updated date before going off to grab the links, so I don't go asking for nonexistent links.
  • Find a way to count the number of links using XSLT. This just seem a much neater way of doing things and should mean the LJ-CUT code can be moved into the XSLT file.
  • Replace the header and footer with a templating system. Sort of a follow on from the todo above, It should give more control of the layout than the current system of bolting on a header and footer after styling the main body of the post. It will also seperate the layout and posting code into different files which is good from a coding point of view.
  • Error checking. Boring to code but would be a good idea.
  • Use LWP instead of Curl to get the links from del.icio.us. This is mostly so I can User-Agent to something identifiable.


#!/usr/bin/perl
#
# Post Delicious Links to LiveJournal
#
#
# Distributed under the Creative Commons "Share Alike" license.
# http://creativecommons.org/licenses/sa/1.0/
#

use strict; # Silly not to be

use POSIX qw(strftime); # Date calculation
use Date::Calc qw(Add_Delta_Days);

use LJ::Simple; # Livejournal API

use XML::XSLT; # http://xmlxslt.sourceforge.net/

# set up local variables - change to suit your needs.
my $LJUser = "";
my $LJPW = "";

my $delUser = "";
my $delPW = "";

my $numberofpostsbeforecut = 4;

my $xsl ="dailylinks.xsl";

my $headername ="dailyheader.xml";
my $footername ="dailyfooter.xml";

# Get today's date
my $year = strftime( "%Y", localtime());
my $month = strftime( "%m", localtime());
my $day = strftime( "%d", localtime());

# Skip back a day
my $date = yesterday($year, $month, $day);

# go get the links
print "Getting the bookmarks for ";
print $date;
print " from del.icio.us account ";
print $delUser;
print "\n";

my $xml = get_links_for_date($date,$delUser,$delPW);

# Count the number of links
my $numberoflinks = count_links($xml);

print "Found ";
print $numberoflinks;
print " links.\n";

# Any links to post?
if ($numberoflinks>0) {
# create the subjectline
my $subjectline ="Links for " . $date;

# set up xslt stuff and transform the xml to html
print "Applying the XSLT style sheet to the links\n";

my $xslt = XML::XSLT->new ($xsl);
$xslt->transform ($xml);
my $guts=$xslt->toString;

# check if we need to add an lj-cut to this post
if ($numberoflinks>$numberofpostsbeforecut) {
print "Over ";
print $numberofpostsbeforecut;
print " links, so adding an lj-cut\n";

$guts = '< lj-cut text="'.$numberoflinks.' Links">'.$guts.'</ lj-cut >';

}

# add the header
my $header = read_file_to_string($headername);
$guts = $header.$guts;

#add the footer
my $footer = read_file_to_string($footername);
$guts = $guts.$footer;

# post to LJ
print "Creating the Livejournal entry\n";

post_lj_entry($subjectline,$guts,$LJUser,$LJPW);

print "Daily links post done.\n";
}

sub yesterday
{
my $today_year = $_[0];
my $today_month = $_[1];
my $today_day = $_[2];

my $yesterday_year;
my $yesterday_month;
my $yesterday_day;
($yesterday_year, $yesterday_month, $yesterday_day) = Add_Delta_Days($today_year, $today_month, $today_day, -1);


my $date = $yesterday_year."-".$yesterday_month."-".$yesterday_day;

return $date;
}

sub get_links_for_date
{
my $date = $_[0];

my $del_user = $_[1];
my $del_pass = $_[2];

my $xml = `curl -s -u $del_user:$del_pass http://del.icio.us/api/posts/get?dt=$date`;

return $xml;
}

sub count_links
{
my $xml= $_[0];

my $countxsl ="countlinks.xsl";
my $countxslt = XML::XSLT->new ($countxsl);

$countxslt->transform ($xml);
my $countguts = $countxslt->toString;

$_ = $countguts;

my $numberoflinks = tr/a-z//;

return $numberoflinks;
}

sub read_file_to_string
{
my $filename= $_[0];

open (TEXTFILE,$filename);

my $string = "";
my $line;

while ($line = ) {
chomp ($line);
$string = $string.$line;
}

close (TEXTFILE);

return $string;
}

sub post_lj_entry
{
my $subject = $_[0];
my $body = $_[1];
my $lj_user = $_[2];
my $lj_pass = $_[3];


my $lj = new LJ::Simple ({
user => $lj_user,
pass => $lj_pass,
});

(defined $lj)
|| die "$0: Failed to log into LiveJournal: $LJ::Simple::error\n";

my %Event=();

$lj->NewEntry(\%Event) ||
die "$0: Failed to create new entry: $LJ::Simple::error\n";


$lj->SetSubject(\%Event,$subject)
|| die "$0: Failed to set subject - $LJ::Simple::error\n";

$lj->Setprop_preformatted(\%Event,1) ||
die "$0: Failed to set property - $LJ::Simple::error\n";

$lj->SetEntry(\%Event,$body)
|| die "$0: Failed to set entry: $LJ::Simple::error\n";

my ($item_id,$anum,$html_id)=$lj->PostEntry(\%Event);
(defined $item_id)
|| die "$0: Failed to post journal entry: $LJ::Simple::error\n";
}


[ Technorati Tags: , , , , , ]
LinkReply