| 1 |
package Plagger::Plugin::Publish::MT; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use warnings; |
|---|
| 4 |
use base qw (Plagger::Plugin); |
|---|
| 5 |
|
|---|
| 6 |
our $VERSION = 0.01; |
|---|
| 7 |
|
|---|
| 8 |
use Net::MovableType; |
|---|
| 9 |
use SOAP::Transport::HTTP; |
|---|
| 10 |
|
|---|
| 11 |
sub register { |
|---|
| 12 |
my ($self, $context) = @_; |
|---|
| 13 |
$context->register_hook( |
|---|
| 14 |
$self, |
|---|
| 15 |
'publish.feed' => \&feed, |
|---|
| 16 |
); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sub mt { |
|---|
| 20 |
my $self = shift; |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
local $SOAP::Transport::HTTP::Client::USERAGENT_CLASS = "Plagger::UserAgent"; |
|---|
| 24 |
|
|---|
| 25 |
return $self->{mt} if $self->{mt}; |
|---|
| 26 |
$self->{mt} = Net::MovableType->new($self->conf->{rsd}); |
|---|
| 27 |
unless (defined $self->{mt}) { |
|---|
| 28 |
die "couldn't create MT object: " . Net::MovableType->errstr; |
|---|
| 29 |
} |
|---|
| 30 |
$self->{mt}->username($self->conf->{username}); |
|---|
| 31 |
$self->{mt}->password($self->conf->{password}); |
|---|
| 32 |
$self->{mt}->blogId($self->conf->{blog_id} || 1); |
|---|
| 33 |
return $self->{mt}; |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
sub feed { |
|---|
| 37 |
my ($self, $context, $args) = @_; |
|---|
| 38 |
my $body = $self->templatize( |
|---|
| 39 |
$self->{conf}->{template} || 'mt.tt', |
|---|
| 40 |
{ feed => $args->{feed} } |
|---|
| 41 |
); |
|---|
| 42 |
eval { |
|---|
| 43 |
my $id = $self->post_to_mt( |
|---|
| 44 |
title => $args->{feed}->title, |
|---|
| 45 |
body => $body, |
|---|
| 46 |
); |
|---|
| 47 |
my $post = $self->mt->getPost($id); |
|---|
| 48 |
$context->log(info => "Successfuly posted: $post->{link}"); |
|---|
| 49 |
}; if (my $err = $@) { |
|---|
| 50 |
$err = $err->[0] if ref $err && ref $err eq 'ARRAY'; |
|---|
| 51 |
$context->error($err); |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
sub post_to_mt { |
|---|
| 56 |
my $self = shift; |
|---|
| 57 |
my %args = @_; |
|---|
| 58 |
my $mt = $self->mt; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
Encode::_utf8_off($args{title}); |
|---|
| 62 |
Encode::_utf8_off($args{body}); |
|---|
| 63 |
|
|---|
| 64 |
my $id = $mt->newPost({ |
|---|
| 65 |
title => $self->conf->{title} || $args{title} || '', |
|---|
| 66 |
description => $args{body} || '', |
|---|
| 67 |
}) or die $mt->errstr; |
|---|
| 68 |
$mt->publishPost($id); |
|---|
| 69 |
$id; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
1; |
|---|
| 73 |
|
|---|
| 74 |
__END__ |
|---|
| 75 |
|
|---|
| 76 |
=head1 NAME |
|---|
| 77 |
|
|---|
| 78 |
Plagger::Plugin::Publish::MT - Post feeds to Movable Type |
|---|
| 79 |
|
|---|
| 80 |
=head1 SYNOPSIS |
|---|
| 81 |
|
|---|
| 82 |
- module: Publish::MT |
|---|
| 83 |
config: |
|---|
| 84 |
rsd: http://www.example.com/mt/rsd.xml |
|---|
| 85 |
username: Melody |
|---|
| 86 |
password: Nelson |
|---|
| 87 |
blog_id: 1 |
|---|
| 88 |
title: "Today's post from Plagger" |
|---|
| 89 |
|
|---|
| 90 |
=head1 CONFIG |
|---|
| 91 |
|
|---|
| 92 |
=head2 rsd |
|---|
| 93 |
|
|---|
| 94 |
URL of rsd.xml on your Movable Type, which includes your API |
|---|
| 95 |
end-point. |
|---|
| 96 |
|
|---|
| 97 |
=head2 username |
|---|
| 98 |
|
|---|
| 99 |
Your username on Movable Type. |
|---|
| 100 |
|
|---|
| 101 |
=head2 password |
|---|
| 102 |
|
|---|
| 103 |
Specify your password. Note that it's not your login password but API |
|---|
| 104 |
password. |
|---|
| 105 |
|
|---|
| 106 |
=head2 blog_id |
|---|
| 107 |
|
|---|
| 108 |
Your blog's ID number. |
|---|
| 109 |
|
|---|
| 110 |
=head2 title |
|---|
| 111 |
|
|---|
| 112 |
You can specify the title of new entry which will be defaults to |
|---|
| 113 |
title of the feed. |
|---|
| 114 |
|
|---|
| 115 |
=head1 AUTHOR |
|---|
| 116 |
|
|---|
| 117 |
Naoya Ito E<lt>naoya@bloghackers.netE<gt> |
|---|
| 118 |
|
|---|
| 119 |
=head1 SEE ALSO |
|---|
| 120 |
|
|---|
| 121 |
L<Plagger>, L<Net::MovableType> |
|---|
| 122 |
|
|---|
| 123 |
=cut |
|---|