#!/usr/bin/perl # blogpost.pl # # Usage # blogpost.pl blogname textfile # # -t title (blog title) # # # ======================================= # # XML-RPC API # # metaWeblog.newPost # Description: Creates a new post, and optionally publishes it. # # Parameters: String blogid, String username, String password, struct content, boolean publish # # Return value: # on success, String postid of new post; # on failure, fault # # Notes: the struct content can contain the following standard keys: # title, for the title of the entry; # description, for the body of the entry; # dateCreated, to set the created-on date of the entry. # If specified, dateCreated should be in ISO.8601 format. # # In addition, Movable Type's implementation allows you to pass in values # for five other keys: # int mt_allow_comments, the value for the allow_comments field; # int mt_allow_pings, the value for the allow_pings field; # String mt_convert_breaks, the value for the convert_breaks field; # String mt_text_more, the value for the additional entry text; # String mt_excerpt, the value for the excerpt field; # String mt_keywords, the value for the keywords field; # array mt_tb_ping_urls, the list of TrackBack ping URLs for this entry. # # # ======================================= # Configuration file format # %weblogs = ( # mywordpress => { # title => "My Wordpress Weblog", # blogid => '1', # username => 'me', # password => 'password', # server => 'http://domain.com/path/to/wordpress/xmlrpc.php', # bloglog => 'mywordpress', # }, # ); # # 1; use XMLRPC::Lite; use Data::Dumper; use strict; #use warnings; our $DEBUG = 0; # required globals our (%weblogs); # defined in the config file our $blogdir = '/usr/local/etc/www/weblogs'; our $logfile = "$blogdir/blogposts.log"; our $config = "blogs.conf"; our ($blogref, $textfile); our ($blogid, $server, $username, $password); # for ease of use # ($columns, $wrap) # from Text::Wrap our $today; BEGIN { $blogref = $ARGV[0]; $textfile = $ARGV[1]; die "usage: $0 blogref textfile\n" unless $blogref && $textfile; die "usage: $0 blogref textfile\n" unless -f $textfile; } main: { my ($bloglog); my ($title, @body, $body); my ($call, $rpc, $som); open (LOG, ">>$logfile") or die "Cannot open logfile $logfile [$!]\n"; if (-e "$blogdir/$config") { require "$blogdir/$config"; } else { print LOG "Fatal: Cannot find required $blogdir/$config\n"; die "Cannot find required $blogdir/$config\n"; } unless (defined($weblogs{$blogref})) { print LOG "Fatal: No such weblog $blogref\n"; die "No such weblog $blogref\n"; } $today = `date`; chomp($today); $server = $weblogs{$blogref}{server}; $blogid = $weblogs{$blogref}{blogid}; $username = $weblogs{$blogref}{username}; $password = $weblogs{$blogref}{password}; $bloglog = $weblogs{$blogref}{bloglog}; $call = 'metaWeblog.newPost'; open (IN, "<$textfile"); # title: first line # body: remainder @body = ; close (IN); $title = $body[0]; shift @body; $body = join "", @body; if ($DEBUG) { print "metaWeblog.newPost', $blogid, $username, *****, \n"; print $title, "\n"; print $body, "\n"; # exit; } print LOG "\n$today\n"; print LOG "$blogref\n"; $rpc = XMLRPC::Lite->new; $rpc->proxy($server); $som = $rpc->call('metaWeblog.newPost', $blogid, $username, $password, { 'title' => $title, 'description' => $body, }, 1 # 1 = publish ); if ($som->fault) { die "Error: ", $som->faultcode, " ", $som->faultstring, "\n"; } append2log($bloglog, $title); } END { close(LOG); } # append2log # append entry to $bloglog # sub append2log { my ($bloglog, $title) = @_; open (OUT, ">>$blogdir/$bloglog"); printf OUT ("%-8d %s\n", $today, $title); close (OUT); } sub getPostCategories { # mt.getPostCategories Return value: # on success, an array of structs containing # String categoryName, String categoryId, and boolean isPrimary; # on failure, fault. my $postid = shift; my ($call, $rpc, $som, $catestruct); $call = 'mt.getPostCategories'; $rpc = XMLRPC::Lite->new; $rpc->proxy($server); $som = $rpc->call( $call, $postid, $username, $password); if ($som->fault) { die "Error: ", $som->faultcode, " ", $som->faultstring, "\n"; } # return categories as ref to array of hash refs $catestruct = $som->result; return $catestruct; }