#!/usr/bin/perl -w # Creates a Movable Type weblog post containing a day's worth of # del.icio.us links. # v1.0 2004-12-15 # Phil Gyford - phil@gyford.com # http://www.gyford.com/phil/writing/2004/12/16/posting_links_fr.php # Either: # 1) Pass it a YYYY-MM-DD format date on the command line and it will # use the links from that day and set the MT post's "created on" # date to that date. # 2) With no date passed to it, it will use the current date; ideal # for running it daily just before midnight. # By default the title of the MT post will be the number of links. # You can include the tags for each link in the MT post by setting # the flag in the config section. # You'll have to change the two 'lib' paths below, and the details # in the 'Config' section. use strict; use lib "/path/to/your/cgi-bin/lib"; use lib "/path/to/your/cgi-bin/extlib"; # If you have MT Blacklist installed you may need this line to # prevent the script generating errors for some reason; I did. #use lib "/path/to/your/cgi-bin/plugins/Blacklist/lib"; use POSIX; use Net::Delicious; use MT; use MT::Entry; use MT::Blog; ############################################################# # Config - you'll have to edit stuff here... # Movable Type configuration. my $MT_CFG = "/path/to/your/cgi-bin/mt.cfg"; # Get these from the URLs in the MT admin screens. my $BLOG_ID = 8; my $AUTHOR_ID = 1; # del.icio.us account details. my $DEL_USER = 'username'; my $DEL_PASS = 'password'; # The time of day you want the MT post's "created on" time to be. # 24 hour clock, HHMM. my $TIME = '2359'; # Do you want each link's del.icio.us tags included in your MT post? # 1 or 0. my $INCLUDE_TAGS = 1; # End config. ############################################################# # We'll either take a date from the command line or use today. my $date = $ARGV[0] || strftime( "%Y-%m-%d", localtime()); # Check the command line date... if ($date !~ /\d\d\d\d-\d\d-\d\d/) { die "Date format should be YYYY-MM-DD, not '$date'"; } # Initialise Delicious stuff. my $del = Net::Delicious->new({ user => $DEL_USER, pswd => $DEL_PASS }); # Get all the posts for this day from delicious. my @posts = $del->posts({ tag => 'top', dt => $date }); if (scalar @posts == 0) { # There weren't any posts on this day. exit; } # Create the HTML that will be in the blog entry. my $html = ''; foreach my $post (@posts) { $html .= '
' . $post->description . "
\n
"; if ($post->extended) { $html .= $post->extended; } if ($INCLUDE_TAGS) { my (@tags) = split / /, $post->tags; for (my $i = 0; $i < @tags; $i++) { $tags[$i] = ''.$tags[$i].''; } $html .= ' '; $html .= join ', ', @tags; $html .= ""; } $html .= "
\n"; } $html = "
\n$html
\n"; # Convert date to MT's format for the created_on time. my ($hour, $min) = split/:/, $TIME; my ($year, $mon, $mday) = split /-/, $date; my $created_on = $year . $mon . $mday . $hour . $min . '00'; # Initialise MT stuff. my $mt = MT->new( Config => "$MT_CFG" ) or die "Make: ".MT->errstr; my $blog = MT::Blog->load($BLOG_ID); # Make the MT entry. my $entry = MT::Entry->new; $entry->blog_id($blog->id); $entry->author_id($AUTHOR_ID); $entry->created_on($created_on); my $num_posts = scalar @posts; my $plural = $num_posts == 1 ? 'Link' : 'Links'; # You could include the date in the title if you wanted... #$entry->title("$num_posts $plural for " . strftime("%e %B %Y", 00, $min, $hour, $mday, $mon-1, $year-1900)); $entry->title("$num_posts $plural"); $entry->text($html); $entry->status(MT::Entry::RELEASE()); $entry->allow_comments($blog->allow_comments_default); $entry->convert_breaks($blog->convert_paras); $entry->save or die "Entry saving error: ".$entry->errstr; $blog->save or die "Blog saving error: ".$blog->errstr; $mt->rebuild(BlogID => $BLOG_ID) or die "Rebuild error: " . $mt->errstr;