root/trunk/plagger/lib/Plagger/Plugin/Publish/MT.pm

Revision 1248 (checked in by miyagawa, 14 years ago)

Update UserAgent?::AuthenRequest? to use $agent->credentials() instead of overriding request() method.
Hack Publish::MT and XMLRPC::Lite internal to use Plagger::UserAgent? instead of LWP.

Line 
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; # need to preload
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     # hack to replace XMLRPC::Lite internal UserAgent class so we can add credentials
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     # FIXME: Can I use XML::RPC::Lite without hack?
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
77
78 Plagger::Plugin::Publish::MT - Post feeds to Movable Type
79
80 =head1 SYNOPSYS
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 specifiy 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
Note: See TracBrowser for help on using the browser.