Plaggerを触ろう触ろうと思いつつ、いまいち使う機会がなかったのですが、ようやくやりたいことが見つかったので触ってみました。
最近ネタがないこともあり、VoxでQotDに答えているのですが、これをMovableTypeにも同期したいなぁと思いPlaggerでやってみました。
ちなみにQotDというのは今日のお題ということでVoxから一日一個質問があってそれに答えればネタ切れのときに困らないよという仕組みです。
使うプラグインは- Subscription::Config - Voxからフィードの取得
- Filter::BreakEntriesToFeeds - フィードを各エントリーに分割
- Rule::Deduped - 一度投稿したものはだぶらせらない
- Publish::MT - MovableTypeに投稿
投稿日も同期したかったのとMTのカテゴリとmt_convert_breaksを指定したかったので一部変更加えました。(カテゴリーの指定に関してはここを参考にしました)
Filter::BreakEntriesToFeeds
$feed->add_entry($entry);
$feed->title($entry->title)
if $self->conf->{use_entry_title};
$feed->updated($entry->date)
if $self->conf->{use_entry_date};
$context->update->add($feed);
Publish::MT
sub feed {
my ($self, $context, $args) = @_;
my $body = $self->templatize(
$self->{conf}->{template} || 'mt.tt',
{ feed => $args->{feed} }
);
eval {
my $id = $self->post_to_mt(
title => $args->{feed}->title,
body => $body,
date => $args->{feed}->updated,
);
my $post = $self->mt->getPost($id);
$context->log(info => "Successfuly posted: $post->{link}");
}; if (my $err = $@) {
$err = $err->[0] if ref $err && ref $err eq 'ARRAY';
$context->error($err);
}
}
sub post_to_mt {
my $self = shift;
my %args = @_;
my $mt = $self->mt;
# FIXME: Can I use XML::RPC::Lite without hack?
Encode::_utf8_off($args{title});
Encode::_utf8_off($args{body});
my $opt = $self->conf->{options};
$opt->{dateCreated} = $args{date}->format('W3CDTF')
if $args{date};
my $id = $mt->newPost({
title => $self->conf->{title} || $args{title} || '',
description => $args{body} || '',
%{$opt},
}) or die $mt->errstr;
if ($self->conf->{category}) {
$mt->setPostCategories($id, $self->conf->{category});
}
$mt->publishPost($id);
$id;
}
で、アセットをこんな感じに
assets/plugins/Publish-MT/mt.tt[% FOREACH entry = feed.entries -%] <p> <a href="[% entry.link %]">[% entry.title %]</a> from Vox </p> [% entry.body %] [% END %]
でレシピはこんな感じです。
global:
timezone: Asia/Tokyo
assets_path: [/path/to/assets]
plugin_path: [/path/to/lib]
plugins:
- module: Subscription::Config
config:
feed:
- url: [url to Vox feed]
- module: Filter::BreakEntriesToFeeds
config:
use_entry_title: 1
use_entry_date: 1
- module: Filter::Rule
rule:
module: Deduped
path: [/path/to/cache]
- module: Publish::MT
config:
rsd: [url to MT rsd]
username: [username]
password: [password]
blog_id: [blog id]
category: QotD
options:
mt_convert_breaks: 0
[追記]
Voxのフィードにatom.xmlを指定すると
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:at="http://www.sixapart.com/ns/at">とか入って気色悪いのでrss.xmlを指定することにしてます。 例えば
http://hide.vox.com/library/posts/tags/qotd/rss.xmlこんな感じです
