root/lang/perl/plagger/lib/Plagger/Plugin/Publish/NiftyClip.pm @ 559

Revision 559, 3.5 kB (checked in by mattn, 6 years ago)

lang/perl/plagger/lib/Plagger/Plugin/Publish/NiftyClip.pm: fixed: bits changes for treating tags.

Line 
1package Plagger::Plugin::Publish::NiftyClip;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use Time::HiRes qw(sleep);
7use Plagger::Mechanize;
8
9sub register {
10    my($self, $context) = @_;
11    $context->register_hook(
12        $self,
13        'publish.entry' => \&add_entry,
14        'publish.init'  => \&initialize,
15    );
16}
17
18sub initialize {
19    my $self = shift;
20    unless ($self->{mech}) {
21        my $mech = Plagger::Mechanize->new;
22        $mech->agent_alias('Windows IE 6');
23        $mech->quiet(1);
24        $self->{mech} = $mech;
25    }
26    $self->login_nifty_clip;
27}
28
29
30sub add_entry {
31    my ($self, $context, $args) = @_;
32
33    my @tags = @{$args->{entry}->tags};
34
35    my $summary;
36    if ($self->conf->{post_body}) {
37        $summary = encode('utf-8', $args->{entry}->body_text); # xxx should be summary
38    }
39
40    my $res = eval { $self->{mech}->get('http://clip.nifty.com/create') };
41    if ($res && $res->is_success) {
42        eval {
43            for (my $num = 0; $num < @tags; $num++) {
44                if ($num == 0) {
45                    $self->{mech}->form_name('savingForm')->find_input(
46                        'tagname')->value($tags[0]);
47                } else {
48                    $self->{mech}->form_name('savingForm')->push_input(
49                        'text', {
50                            name  => 'tagname',
51                            value => $tags[$num],
52                        });
53                }
54            }
55            $self->{mech}->submit_form(
56                form_name => 'savingForm',
57                fields => {
58                    title       => encode('utf-8', $args->{entry}->title),
59                    url         => encode('utf-8', $args->{entry}->link),
60                    comment     => $summary,
61                    public_flag => ($self->conf->{default_public} ? 'on' : 'off'),
62                }
63            )
64        };
65        if ($@) {
66            $context->log(info => "can't submit: " . $@);
67        } else {
68            $context->log(info => "Post entry success.");
69        }
70    } else {
71       $context->log(error => "fail to bookmark HTTP Status: " . $res->code);
72    }
73 
74    my $sleeping_time = $self->conf->{interval} || 3;
75    $context->log(info => "sleep $sleeping_time.");
76    sleep( $sleeping_time );
77}
78
79sub login_nifty_clip {
80    my $self = shift;
81    unless ($self->conf->{username} && $self->conf->{password}) {
82        Plagger->context->log(error => 'set your username and password before login.');
83    }
84    my $res = $self->{mech}->get('https://clip.nifty.com/login');
85    $self->{mech}->submit_form(
86        form_name => 'login-form',
87        fields => {
88            username => $self->conf->{username},
89            password => $self->conf->{password},
90        },
91    );
92}
93
941;
95
96__END__
97
98=head1 NAME
99
100Plagger::Plugin::Publish::NiftyClip - Post to nifty clip automatically
101
102=head1 SYNOPSIS
103
104  - module: Publish::NiftyClip
105    config:
106      username: your-username
107      password: your-password
108      interval: 2
109      post_body: 1
110      default_public: 1
111
112=head1 DESCRIPTION
113
114This plugin automatically posts feed updates to nifty clip
115L<http://clip.nifty.com/>. It supports automatic tagging as well. It
116might be handy for synchronizing delicious feeds into nifty clip.
117
118=head1 CONFIG
119
120=over 4
121
122=item username, password
123
124username and password for Nifty Clip. Required.
125
126=item default_public
127
128Optional. default publish operation value is '1' as publish.
129
130=item interval
131
132Optional.
133
134=item timeout
135
136Optional.
137
138=back
139
140=head1 AUTHOR
141
142Yasuhiro Matsumoto
143
144=head1 SEE ALSO
145
146L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize>
147
148=cut
Note: See TracBrowser for help on using the browser.