?

Log in

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

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

Daily links digest Perl script (Version 0.2) [Mar. 8th, 2005|09:04 pm]
Zoe
Update 2005-04-18: Newer Version here.

I've had another hacking session on the daily links script, still not done everything on the todo list, but I've made a good start.

Todos
  • Way, way more testing. but you can never do enough, I'm sure it still contains bugs especially in error cases.
  • Better error handling and reporting.
  • Add code to not post an entry when that day has no links.
  • Add the code to work out what the current date minus a day is so I can have it automatically run it at four in the morning. Not done yet but I know how.
  • Add some code to put in a lj-cut if the number of links is greater than some constant (I'm thinking about 6-8 as a default). (Currently set to 4)
  • Add Technorati Tags at the bottom of each entry.
  • Sort out the formatting. but I'm still not 100% happy.
New
  • Refactor the code, it needs to be split up and use some functions.
  • Find a way to count the number of links using XSLT.
  • 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.
Its a lot more useable now, with progress messages so you can see what its trying to do. I'm going to keep working on it, hopefully I'll do some more this week but last time I said that it was nearer a month, so we will see.

#!/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 LJ::Simple; # Livejournal API

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

# set up local variables - change to suit your needs.

my $LJUser = "test";
my $LJPW = "test";

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

my $numberofpostsbeforecut = 4;

my $xsl ="links.xsl";


print "del.icio.us to LiveJournal daily links poster V0.2 (2005-03-08)\n\n";

# get today's date and format it

my $date = strftime( "%Y-%m-%d", localtime());

my $subjectline ="Links for " . $date;


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

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

# Count the number of links
my $countxsl ="countlinks.xsl";
my $countxslt = XML::XSLT->new ($countxsl);
$countxslt->transform ($xml);
my $countguts = $countxslt->toString;

$_ = $countguts;

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

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

# Any links to post?
if ($numberoflinks>0) {

print "Applying the XSLT style sheet to the links\n";

# set up xslt stuff and transform the xml to html
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";

$guts = ''.$guts.'</ lj-cut>';

}

# post to LJ

print "Creating the Livejournal entry\n";

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

(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,$subjectline)
|| 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,$guts)
|| 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";

print "Daily links post done.\n";

}
LinkReply