?

Log in

No account? Create an account
Weekly tag Perl script (Version 0.1) - Z303 [entries|archive|friends|userinfo]
Zoe

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

Weekly tag Perl script (Version 0.1) [Apr. 19th, 2005|08:04 pm]
Zoe
Update 2005-04-20: Newer version here.

When I was first working on the daily links script, I thought it would be interesting to see what tags I was using most in a particular week, so I would have some way of tracking what memes I focused on. My first thought was just to code up a simple (and very boring) top ten list.

After thinking about that I could do and being inspired by the del.icio.us and Flickr tag walls and the header at the top of near near future, I came up with the current tag list, last week was not the best to show off the output but when I was testing using other dates it did show more interesting patterns. The fun bit will be to look back in a few months and see how my focus has changed.

Most of the todo list right now is the same as the daily links script (which this script share most of its code with , code reuse yeah) but I'm, sure over time they will become more and more distinct.

Todos
  • Make sure it follows the rules on del.icio.us/doc/api. The biggest problem right now is I don't check that a minute has passed between each request, which is very naughty, hopefully I'll get that sort in the next few days.
  • 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.
  • Replace the header and footer with a templating system. Making this change is easier in this script than in the daily links one, as no links count needs doing. I should just be able to output an xml file and apply a XSLT stylesheet to that then post to LJ.
  • 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 tags 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 ="weeklytags.xsl";

my $headername ="weeklytagsheader.xml";
my $footername ="weeklytagsfooter.xml";

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

# get the tags for the last 7 days
my $guts = "";
my $date;
my $i;

for ($i=1;$i<8;$i++)
{
$date = move_back_days($year, $month, $day, $i);
$guts = $guts.get_tags_for_date($date);
}

# lower case all the tags and
# sort the links into alphabetic order
my @tags_list = split (' ',$guts);

my $numberoflinks = 0;

foreach (@tags_list){
$_ = lc($_);

$numberoflinks++;
}

if ($numberoflinks>0)
{
# count how often the tags were used
my %tag_counts;

foreach (@tags_list){
$tag_counts{$_}= $tag_counts{$_} +1;
}

# create an order list of tag names
my @sorted_tag_list;

my $tag_name;
my $tag_count;
my $list_count= 0;

while (($tag_name,$tag_count) =each(%tag_counts)) {
@sorted_tag_list[$list_count] = $tag_name;

$list_count++;
}

@sorted_tag_list = sort (@sorted_tag_list);

# count the number of tags
my $numberoflinks =$list_count -1;

# generate the final html for the blog entry
my $final_tag_html = "";

foreach (@sorted_tag_list){
$final_tag_html = $final_tag_html.scale_text(convert_tag_to_link($delUser,$_),$tag_counts{$_})." ";
# print "$_ $tag_counts{$_}\n";
}

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

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

# create the subject line
my $start_date = move_back_days($year, $month, $day, 8);
my $end_date = move_back_days($year, $month, $day, 1);

my $subject_line = "Tags for the week ".$start_date." to ".$end_date;


# post the entry
post_lj_entry($subject_line,$final_tag_html,$LJUser,$LJPW);


}

sub convert_tag_to_link
{
my $username = $_[0];
my $tag = $_[1];

my $link = ''.$tag.'';


return $link;
}

sub scale_text
{
my $text = $_[0];
my $scale = 1+$_[1];

my $scale_cut_off =6;

my $scaled_text;

if ($scale>$scale_cut_off)
{
$scale = $scale_cut_off;
$scaled_text = ''.$text.'';
}
else
{
$scaled_text = ''.$text.'';
}

return $scaled_text;
}


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

my $xml = get_links_for_date($date,$delUser,$delPW);
my $xslt = XML::XSLT->new ($xsl);

$xslt->transform ($xml);

my $guts=$xslt->toString;

return $guts;
}

sub move_back_days
{
my $today_year = $_[0];
my $today_month = $_[1];
my $today_day = $_[2];
my $Offset_days = -$_[3];

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


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