root/trunk/plagger/lib/Plagger/Plugin/Publish/LivedoorClip.pm

Revision 1073 (checked in by ko, 14 years ago)

oops, remove .pm from SYNOPSIS

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