September 01, 2004

Trackback Ping からエントリを投稿

Blog Hacks サポートページ で追加情報や新 Hack の集約に使用しているスクリプトを適当に公開。Trackback Ping を受けて、その title, excerpt などを XML-RPC で Movable Type にポストしています。

#!/usr/local/bin/perl -w
use strict;
use CGI;
use Jcode;
use XMLRPC::Lite;

our $BlogCharset = "utf-8";  # XML-RPC is done in UTF-8
our %CategoryID  = qw(bonus 60 hack 61);
our $DefaultCategoryId = 61;
our $XmlRpcURL   = "http://example.com/mt/mt-xmlrpc.cgi";
our $BlogID      = 19;
our $Username    = "foobar";
our $Password    = "blahblah";

my $q = CGI->new();
my $charset = $q->param('charset');

my %postdata = get_postdata($q, $charset);

my $category = $q->path_info() || "";
   $category =~  s!^/!!;

my $category_id = $CategoryID{$category} || $DefaultCategoryId;

if ($postdata{title}) {
    post_entry(\%postdata, $category_id);

    # XXX no error handling
    print $q->header('text/xml'), <<XML;
<?xml version="1.0" encoding="UTF-8"?>
<response>
<error>0</error>
</response>
XML
    ;
} else {
    print $q->header('text/xml'), <<XML;
<?xml version="1.0" encoding="UTF-8"?>
<response><error>1</error><message>Invalid Trackback parameter</message></response>
XML
    ;
}

# get TrackBack parameters
sub get_postdata {
    my($q, $charset) = @_;
    my @keys = qw(blog_name url title excerpt);
    return map { $_ => get_param($q, $_, $charset) } @keys;
}

# get parameter from CGI considering $charset and $BlogCharset
sub get_param {
    my($q, $key, $charset) = @_;
    my $data = $q->param($key);
    my %code = ('euc-jp' => 'euc', 'utf-8' => 'utf8', 'Shift_JIS' => 'sjis');
    my $code = ($charset && $code{$charset}) ? $code{$charset} : Jcode::getcode($data);
    my $outcode = $code{$BlogCharset};
    return Jcode->new($data, $code)->$outcode;
}

# Post entry using MT XML-RPC API
sub post_entry {
    my($postdata, $category_id) = @_;

    XMLRPC::Lite->import('trace' => 'debug');
    my $entry_id = create_item($postdata);
    set_category($entry_id, $category_id);
    publish_item($entry_id);
}

sub create_item {
    my $postdata = shift;

    # create item data from Trackback data
    my $item;
    $item->{title} = "$postdata->{title} (via $postdata->{blog_name})";
    $item->{description} = <<DATA
<blockquote>$postdata->{excerpt}</blockquote>
<p><a href="$postdata->{url}">Continue Reading "$postdata->{title}"</a></p>
DATA
    ;

    my $publish = 0; # no publish here
    my @args = ($BlogID, $Username, $Password, $item, $publish);
    return XMLRPC::Lite->proxy($XmlRpcURL)->call("metaWeblog.newPost", @args)->result;
}

sub set_category {
    my($entry_id, $category_id) = @_;
    my @args = ($entry_id, $Username, $Password,
    [ { categoryId => $category_id } ]);
    XMLRPC::Lite->proxy($XmlRpcURL)->call("mt.setPostCategories", @args);
}

sub publish_item {
    my $entry_id = shift;
    my @args = ($entry_id, $Username, $Password);
    XMLRPC::Lite->proxy($XmlRpcURL)->call("mt.publishPost", @args);
}

まあ特にコメントは必要ないかとおもいますが。XML-RPC でカテゴリをセットするためには、metaWeblog.newPost でエントリを publish=0 で作成したあと、mt.setPostCategories でカテゴリをセット、その後 mt.publishPost で公開というメンドウなステップを踏む必要ありです。

Posted by miyagawa at September 1, 2004 01:38 AM | Permalink | Comments (0) | TrackBack(3)
Comments
Trackbacks
TrackBack URL for this entry: http://blog.bulknews.net/mt3/mt-tb.cgi/1226
blog.bulknews.net
Excerpt: More info can be found on my page...
Weblog: Kushanir Blog
Tracked: July 4, 2005 08:02 AM
Thomas Anders
Excerpt: Trackback Ping からエ�...
Weblog: Lisa Viktoria
Tracked: August 13, 2005 10:15 AM
Thomas Anders
Excerpt: Trackback Ping からエ�...
Weblog: Lisa Viktoria
Tracked: August 13, 2005 10:15 AM
Post a comment