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

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

fixed Publish::LivedoorClip?

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