| 1 |
package Plagger::Plugin::Publish::LivedoorClip; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use Time::HiRes qw(sleep); |
|---|
| 7 |
use URI; |
|---|
| 8 |
use Plagger::Mechanize; |
|---|
| 9 |
|
|---|
| 10 |
sub register { |
|---|
| 11 |
my($self, $context) = @_; |
|---|
| 12 |
$context->register_hook( |
|---|
| 13 |
$self, |
|---|
| 14 |
'publish.entry' => \&add_entry, |
|---|
| 15 |
'publish.init' => \&initialize, |
|---|
| 16 |
); |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sub initialize { |
|---|
| 20 |
my $self = shift; |
|---|
| 21 |
unless ($self->{mech}) { |
|---|
| 22 |
my $mech = Plagger::Mechanize->new; |
|---|
| 23 |
$mech->agent_alias('Windows IE 6'); |
|---|
| 24 |
$mech->quiet(1); |
|---|
| 25 |
$self->{mech} = $mech; |
|---|
| 26 |
} |
|---|
| 27 |
$self->login_livedoor_clip; |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
sub add_entry { |
|---|
| 32 |
my ($self, $context, $args) = @_; |
|---|
| 33 |
|
|---|
| 34 |
my @tags = @{$args->{entry}->tags}; |
|---|
| 35 |
my $tag_string = @tags ? join(' ', @tags) : ''; |
|---|
| 36 |
|
|---|
| 37 |
my $summary; |
|---|
| 38 |
if ($self->conf->{post_body}) { |
|---|
| 39 |
$summary = encode('utf-8', $args->{entry}->body_text); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
my $uri = URI->new('http://clip.livedoor.com/clip/add'); |
|---|
| 43 |
$uri->query_form( |
|---|
| 44 |
link => $args->{entry}->link, |
|---|
| 45 |
jump => 'page', |
|---|
| 46 |
tags => encode('utf-8', $tag_string), |
|---|
| 47 |
title => encode('utf-8', $args->{entry}->title), |
|---|
| 48 |
notes => $summary, |
|---|
| 49 |
); |
|---|
| 50 |
|
|---|
| 51 |
my $add_url = $uri->as_string; |
|---|
| 52 |
my $res = eval { $self->{mech}->get($add_url) }; |
|---|
| 53 |
if ($res && $res->is_success) { |
|---|
| 54 |
eval { $self->{mech}->submit_form(form_name => 'clip') }; |
|---|
| 55 |
if ($@) { |
|---|
| 56 |
$context->log(info => "can't submit: " . $args->{entry}->link); |
|---|
| 57 |
} else { |
|---|
| 58 |
$context->log(info => "Post entry success."); |
|---|
| 59 |
} |
|---|
| 60 |
} else { |
|---|
| 61 |
$context->log(info => "fail to clip $add_url HTTP Status: " . $res->code); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 65 |
$context->log(info => "sleep $sleeping_time."); |
|---|
| 66 |
sleep( $sleeping_time ); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
sub login_livedoor_clip { |
|---|
| 70 |
my $self = shift; |
|---|
| 71 |
unless ($self->conf->{livedoor_id} && $self->conf->{password}) { |
|---|
| 72 |
Plagger->context->log(error => 'set your livedoor_id and password before login.'); |
|---|
| 73 |
} |
|---|
| 74 |
unless ($self->_has_clip_account) { |
|---|
| 75 |
Plagger->context->log(error => 'register to livedoor clip before using this module.'); |
|---|
| 76 |
} |
|---|
| 77 |
my $res = $self->{mech}->get('http://clip.livedoor.com/register/'); |
|---|
| 78 |
$self->{mech}->submit_form( |
|---|
| 79 |
form_name => 'loginForm', |
|---|
| 80 |
fields => { |
|---|
| 81 |
livedoor_id => $self->conf->{livedoor_id}, |
|---|
| 82 |
password => $self->conf->{password}, |
|---|
| 83 |
}, |
|---|
| 84 |
); |
|---|
| 85 |
|
|---|
| 86 |
$self->{mech}->get('http://clip.livedoor.com/register/'); |
|---|
| 87 |
$self->{_logged_in} = $self->{mech}->uri =~ m{^http://clip\.livedoor\.com/} ? 1 : 0; |
|---|
| 88 |
unless ($self->{_logged_in}) { |
|---|
| 89 |
Plagger->context->log(error => "failed to login to livedoor clip."); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
sub _has_clip_account { |
|---|
| 94 |
my $self = shift; |
|---|
| 95 |
my $myclip_url = sprintf('http://clip.livedoor.com/clips/%s', $self->conf->{livedoor_id}); |
|---|
| 96 |
my $res = $self->{mech}->get($myclip_url); |
|---|
| 97 |
return $res->is_success ? 1 : 0; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
1; |
|---|
| 102 |
|
|---|
| 103 |
__END__ |
|---|
| 104 |
|
|---|
| 105 |
=head1 NAME |
|---|
| 106 |
|
|---|
| 107 |
Plagger::Plugin::Publish::LivedoorClip - Post to livedoor clip automatically |
|---|
| 108 |
|
|---|
| 109 |
=head1 SYNOPSIS |
|---|
| 110 |
|
|---|
| 111 |
- module: Publish::LivedoorClip |
|---|
| 112 |
config: |
|---|
| 113 |
livedoor_id: your-username |
|---|
| 114 |
password: your-password |
|---|
| 115 |
interval: 2 |
|---|
| 116 |
post_body: 1 |
|---|
| 117 |
|
|---|
| 118 |
=head1 DESCRIPTION |
|---|
| 119 |
|
|---|
| 120 |
This plugin automatically posts feed updates to livedoor clip |
|---|
| 121 |
L<http://clip.livedoor.com/>. It supports automatic tagging as well. It |
|---|
| 122 |
might be handy for synchronizing delicious feeds into livedoor clip. |
|---|
| 123 |
|
|---|
| 124 |
=head1 AUTHOR |
|---|
| 125 |
|
|---|
| 126 |
Kazuhiro Osawa, Koichi Taniguchi |
|---|
| 127 |
|
|---|
| 128 |
=head1 SEE ALSO |
|---|
| 129 |
|
|---|
| 130 |
L<Plagger>, L<Plagger::Plugin::Publish::HatenaBookmark>, L<Plagger::Mechanize> |
|---|
| 131 |
|
|---|
| 132 |
=cut |
|---|