root/lang/perl/plagger/lib/Plagger/Plugin/Publish/Pookmark.pm

Revision 607, 4.0 kB (checked in by mattn, 14 months ago)

lang/perl/plagger/lib/Plagger/Plugin/Publish/Pookmark.pm: added Publish::Pookmark plugin.

Line 
1package Plagger::Plugin::Publish::Pookmark;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use Time::HiRes qw(sleep);
7use URI;
8use Plagger::Mechanize;
9
10sub register {
11    my($self, $context) = @_;
12    $context->register_hook(
13        $self,
14        'publish.entry' => \&add_entry,
15        'publish.init'  => \&initialize,
16    );
17}
18
19sub 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_pookmark;
28}
29
30
31sub 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); # xxx should be summary
40    }
41
42    if ($self->conf->{default_later}) {
43        my $uri = URI->new('http://pookmark.jp/later');
44        $uri->query_form(
45            url   => $args->{entry}->link,
46            title => encode('utf-8', $args->{entry}->title),
47        );
48        my $res = eval { $self->{mech}->get($uri->as_string) };
49        if ($res && $res->is_success) {
50           $context->log(info => "Post entry success.");
51        } else {
52           $context->log(info => "fail to bookmark HTTP Status: " . $res->code);
53        }
54    } else {
55        my $uri = URI->new('http://pookmark.jp/post');
56        $uri->query_form(
57            url   => $args->{entry}->link,
58            title => encode('utf-8', $args->{entry}->title),
59        );
60        my $res = eval { $self->{mech}->get($uri->as_string) };
61        if ($res && $res->is_success) {
62            eval {
63                my $post_form = $self->{mech}->form_number(2);
64                my $no_twitter = $post_form->find_input('no_twitter');
65                $no_twitter->value('on') if $no_twitter && $self->conf->{default_no_twitter};
66                $self->{mech}->submit_form(
67                    form_number => 2,
68                    fields => {
69                        url     => $args->{entry}->link,
70                        title   => encode('utf-8', $args->{entry}->title),
71                        comment => $summary,
72                        tags    => encode('utf-8', $tag_string),
73                        private => ($self->conf->{default_private} ? 'on' : 'off'),
74                    }
75                )
76            };
77            if ($@) {
78               $context->log(info => "can't submit: " . $args->{entry}->link);
79            } else {
80                $context->log(info => "Post entry success.");
81            }
82        } else {
83           $context->log(info => "fail to bookmark HTTP Status: " . $res->code);
84        }
85    }
86 
87    my $sleeping_time = $self->conf->{interval} || 3;
88    $context->log(info => "sleep $sleeping_time.");
89    sleep( $sleeping_time );
90}
91
92sub login_pookmark {
93    my $self = shift;
94    unless ($self->conf->{jugemkey_id} && $self->conf->{password}) {
95        Plagger->context->log(error => 'set your jugemkey_id and password before login.');
96    }
97    my $uri = URI->new('http://pookmark.jp/ajax/login');
98    $uri->query_form(
99        username => $self->conf->{jugemkey_id},
100        password => $self->conf->{password},
101    );
102    my $res = $self->{mech}->get($uri->as_string);
103        return $res ? $res->content eq '1' : 0;
104}
105
1061;
107
108__END__
109
110=head1 NAME
111
112Plagger::Plugin::Publish::Pookmark - Post to POOKMARK Airlines automatically
113
114=head1 SYNOPSIS
115
116  - module: Publish::Pookmark
117    config:
118      jugemkey_id: your-jugemkey-id
119      password: your-password
120      interval: 2
121      post_body: 1
122      default_no_twitter: 0
123      default_private: 0
124
125=head1 DESCRIPTION
126
127This plugin automatically posts feed updates to POOKMARK Airlines
128L<http://pookmark.jp/>. It supports automatic tagging as well. It
129might be handy for synchronizing delicious feeds into pookmark.
130
131=head1 AUTHOR
132
133Yasuhiro Matsumoto
134
135=head1 SEE ALSO
136
137L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize>
138
139=cut
Note: See TracBrowser for help on using the browser.