P::P::Publish::MixiDiary #2
Posted at 2006-08-23T22:02+09:00 in Development |
|
| ![]()
画像の投稿もさせようと思ったらこんなんなりました。
package Plagger::Plugin::Publish::MixiDiary;
use strict;
use warnings;
use base qw ( Plagger::Plugin );
use WWW::Mixi;
use Encode;
sub register {
my($self, $context) = @_;
$context->register_hook(
$self,
'publish.init' => \&initialize,
'publish.entry' => \&post_diary,
);
}
sub initialize {
my($self, $context, $args) = @_;
my $cookie_jar = $self->cookie_jar;
if (ref($cookie_jar) ne 'HTTP::Cookies') {
$self->conf->{username} ||= 'email@example.com',
$self->conf->{password} ||= 'p4ssw0rd',
}
$self->{mixi} = WWW::Mixi->new(
$self->conf->{username},
$self->conf->{password},
-log => 0,
);
$self->{mixi}->cookie_jar($cookie_jar);
unless ($self->{mixi}->login) {
$context->log(error => "Login failed.");
} else {
$context->log(info => "Login Successed.");
}
}
sub post_diary {
my($self, $context, $args) = @_;
my $e = $args->{entry};
my $title = $e->title;
my $body = $e->body_text;
my @images;
if ($e->has_enclosure) {
for my $enclosure (grep { defined $_->url && $_->is_inline && ($_->url =~ /.*\.jpg$/) } $e->enclosure) {
push(@images, $enclosure->local_path);
}
}
my %diary = (
diary_title => encode('euc-jp', $title),
diary_body => encode('euc-jp', $body),
photo1 => shift(@images),
photo2 => shift(@images),
photo3 => shift(@images),
);
if ($self->{mixi}->get_add_diary_confirm(%diary)) {
$context->log(info => "Making diary succeeded.");
} else {
$context->log(error => "Making diary failed.");
}
}
1;
__END__
yaml の方はこんな感じで。
plugins:
- module: Subscription::Config
config:
feed:
- url: http://d.hatena.ne.jp/xcezx/
- url: http://xcezx.vox.com/
- module: Filter::FindEnclosures
- module: Filter::FetchEnclosure
config:
dir: /home/xcezx/fetch-images
- module: Publish::MixiDiary
config:
username: email@example.com
password: p4ssw0rd
P::P::Filter::FindEnclosures でエントリ内の画像見つけて P::P::Filter::FetchEnclosure でローカルに落とし込む。で、 P::P::Publish::MixiDiary が jpg 画像だけ見つけた順にアップロード。
さぁ!! 誰かリファクタリングしてくれたまえよ。(何様
文字化けっちゅーか、& と ' の数値実体文字参照間違えてました。はてなブックマーク - yukiのブックマーク / 2006年08月27日 のブックマークコメントで初めて気づいた。
141 WebLog: Plagger にした。にコメントしてから気が付いた。s_nobuの日記 - Filter::FormatText#2使えばよさげ。試してないけど。というか、誰かに使われるとは思ってもみなかった。gtlt
grep の所も実体数値文字参照間違えてた。もうグダグダ。