Plagger::Plugin::Publish::LivedoorClip

なんか刺身君も頑張ってるみたいだから僕も頑張ってみたわ。
といいながら僕が敬愛するid:nipotanさんのWWW::SyncSBS::H2Lをインスパイアしただけですわ。

日本語のtagsが反映されないのとコ−ドが雑なので、だれかリファクタリングしてPlagge
rにcommitしていただけるとありがたいです。
そんときは僕の名前けしちゃってな。

package Plagger::Plugin::Publish::LivedoorClip;
use strict;
use base qw( Plagger::Plugin );
                                                                                                                               
use Encode;
use Time::HiRes qw(sleep);
use URI;
use WWW::Mechanize;
                                                                                                                               
sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.entry.fixup' => \&add_entry,
        'publish.init'        => \&initialize,
    );
}
                                                                                                                               
sub initialize {
    my $self = shift;
    unless ($self->{mech}) {
        my $mech = WWW::Mechanize->new;
        $mech->agent_alias('Windows IE 6');
        $self->{mech} = $mech;
    }
    if ($self->conf->{livedoor_id} && $self->conf->{password}) {
        $self->login_livedoor_clip;
    }
}
                                                                                                                               
                                                                                                                               
sub add_entry {
    my ($self, $context, $args) = @_;
                                                                                                                               
    my @tags = @{$args->{entry}->tags};
    my $tag_string = @tags ? join(' ', @tags) : '';
                                                                                                                               
    my $summary;
    if ($self->conf->{post_body}) {
        $summary = encode('utf-8', $tag_string . $args->{entry}->body_text); # xxx should be summary
    } else {
        $summary =  encode('utf-8', $tag_string);
    }
                                                                                                                               
    my $uri = URI->new('http://clip.livedoor.com/clip/add');
    $uri->query_form(
        link  => $args->{entry}->link,
        jump  => 'page',
        tags  => $tag_string,
        title => $args->{entry}->title,
        notes => $summary,
    );
                                                                                                                               
    my $add_url = $uri->as_string;
    my $res = eval { $self->{mech}->get($add_url) };
    if ($res && $res->is_success) {
        eval { $self->{mech}->submit_form(form_name => 'clip') };
        if ($@) {
           $context->log(error => "can't submit: " . $args->{entry}->link);
        }
    }
    else {
       $context->log(error => "fail to clip $add_url HTTP Status: " . $res->code);
    }
                                                                                                                               
    my $sleeping_time = $self->conf->{interval} || 3;
    $context->log(info => "Post entry success. sleep $sleeping_time.");
    sleep( $sleeping_time );
}
                                                                                                                               
sub login_livedoor_clip {
    my $self = shift;
    unless ($self->conf->{livedoor_id} && $self->conf->{password}) {
        Plagger->context->log(error => 'set your livedoor_id and password before login.');
    }
    unless ($self->_has_clip_account) {
        Plagger->context->log(error => 'register to livedoor clip before using this module.');
    }
    my $res = $self->{mech}->get('http://clip.livedoor.com/register/');
    $self->{mech}->submit_form(
        form_name => 'loginForm',
        fields => {
            livedoor_id => $self->conf->{livedoor_id},
            password    => $self->conf->{password},
        },
                             );
    # XXX login checking (WWW::Mechanize->uri() doesn't work correct).
    $self->{mech}->get('http://clip.livedoor.com/register/');
    $self->{_logged_in} =
        $self->{mech}->uri =~ m{^http://clip\.livedoor\.com/} ? 1 : 0;
    unless ($self->{_logged_in}) {
        Plagger->context->log(error => "failed to login to livedoor clip.");
    }
}
                                                                                                                               
sub _has_clip_account {
    my $self = shift;
    my $myclip_url =
        sprintf('http://clip.livedoor.com/clips/%s', $self->conf->{livedoor_id});
    my $res = $self->{mech}->get($myclip_url);
    return $res->is_success ? 1 : 0;
}
                                                                                                                               
                                                                                                                               
1;
                                                                                                                               
__END__
                                                                                                                               
=head1 NAME
                                                                                                                               
Plagger::Plugin::Publish::LivedoorClip - Post to livedoor Clip automatically
                                                                                                                               
=head1 SYNOPSIS
                                                                                                                               
  - module: Publish::LivedoorClip.pm
    config:
      username: your-username
      password: your-password
      interval: 2
      post_body: 1
                                                                                                                               
=head1 DESCRIPTION
                                                                                                                               
                                                                                                                               
=head1 AUTHOR
                                                                                                                               
j708
                                                                                                                               
=head1 SEE ALSO
                                                                                                                               
L<Plagger>, L<Plagger::Plugin::Publish::HatenaBookmark.pm>, L<WWW::SyncSBS::H2L>
                                                                                                                               
=cut