| 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__ |