#june29jp

P::P::Publish::MixiDiaryに「interval」を追加

2006-11-01

ありがたく使わせていただいているP::P::Publish::MixiDiaryですが,P::P::Publish::HatenaBookmarkなどのように「interval」を設定できたら便利だなぁということでやりました.ほとんど何も変えていないけれど一応メモとして残しておきます.

MixiDiary.pm
[perl]
package Plagger::Plugin::Publish::MixiDiary;
use strict;
use warnings;
use base qw ( Plagger::Plugin );

use WWW::Mixi;
use Encode;
use Time::HiRes qw(sleep);

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),
);

my $sleeping_time = $self->conf->{interval} || 3;
if ($self->{mixi}->get_add_diary_confirm(%diary)) {
$context->log(info => “Making diary succeeded. sleep $sleeping_time.”);
sleep( $sleeping_time );
} else {
$context->log(error => “Making diary failed.”);
}
}

1:
__END__
[/perl]

YAMLはこんな感じ.

[xml]
global:
timezone: Asia/Tokyo
log:
level: info

plugins:
– module: Subscription::Config
config:
feed:
– url: http://www.sample.com/rss

– module: Filter::Rule
rule:
module: Deduped
path: /home/user/plagger/cache_rule/feed2mixidiary.tmp
compare_body: 1

– module: Filter::Reverse

– module: Publish::MixiDiary
config:
username: username@example.com
password: password
interval: 10
[/xml]

激しく更新されるFeedをmixi日記に吐くときは,intervalを入れないとmixiに怒られちゃいますからね.良い子のみんなはそんなFeedをmixiにブチ込んじゃダメだよ.

おもしろかったら、シェアやブックマークや送金などぜひぜひお願いします。サイト運営の励みになります!

シェアや送金などお待ちしています!