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

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

Fixes Publish::MT blog_id bug. Fixes #252

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
10 sub register {
11     my ($self, $context) = @_;
12     $context->register_hook(
13         $self,
14         'publish.feed' => \&feed,
15     );
16 }
17
18 sub mt {
19     my $self = shift;
20     return $self->{mt} if $self->{mt};
21     $self->{mt} = Net::MovableType->new($self->conf->{rsd});
22     unless (defined $self->{mt}) {
23         die "couldn't create MT object: " . Net::MovableType->errstr;
24     }
25     $self->{mt}->username($self->conf->{username});
26     $self->{mt}->password($self->conf->{password});
27     $self->{mt}->blogId($self->conf->{blog_id} || 1);
28     return $self->{mt};
29 }
30
31 sub feed {
32     my ($self, $context, $args) = @_;
33     my $body = $context->templatize(
34         $self,
35         $self->{conf}->{template} || 'mt.tt',
36         { feed => $args->{feed} }
37     );
38     eval {
39         my $id = $self->post_to_mt(
40             title => $args->{feed}->title,
41             body  => $body,
42         );
43         my $post = $self->mt->getPost($id);
44         $context->log(
45             info => sprintf(
46                 "Successfuly posted: %s",
47                 $post->{link},
48             )
49         );
50     }; if (my $err = $@) {
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.