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

Revision 524, 3.0 kB (checked in by mattn, 14 months ago)

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

Line 
1package Plagger::Plugin::Publish::FC2Bookmark;
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_fc2bookmark;
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    my $res = eval { $self->{mech}->get('http://bookmark.fc2.com/user/post') };
43    if ($res && $res->is_success) {
44        eval {
45            $self->{mech}->submit_form(
46                form_number => 2,
47                fields => {
48                    "data[Bookmark][title]"        => encode('utf-8', $args->{entry}->title),
49                    "data[Bookmark][url]"          => $args->{entry}->link,
50                    "data[Bookmark][description]"  => $summary,
51                    "data[Bookmark][tag_txt]"      => encode('utf-8', $tag_string),
52                    "data[Bookmark][allow_access]" => "1",
53                    "data[Bookmark][favorite]"     => "0",
54                }
55            )
56        };
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 bookmark 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
71sub login_fc2bookmark {
72    my $self = shift;
73    unless ($self->conf->{usermail} && $self->conf->{password}) {
74        Plagger->context->log(error => 'set your usermail and password before login.');
75    }
76    my $res = $self->{mech}->get('http://bookmark.fc2.com/');
77    $self->{mech}->submit_form(
78        form_number => 2,
79        fields => {
80            email => $self->conf->{usermail},
81            pass  => $self->conf->{password},
82        },
83    );
84}
85
861;
87
88__END__
89
90=head1 NAME
91
92Plagger::Plugin::Publish::FC2Bookmark - Post to fc2bookmark automatically
93
94=head1 SYNOPSIS
95
96  - module: Publish::FC2Bookmark
97    config:
98      usermail: your-e-mail
99      password: your-password
100      interval: 2
101      post_body: 1
102
103=head1 DESCRIPTION
104
105This plugin automatically posts feed updates to fc2bookmark
106L<http://bookmark.fc2.com/>. It supports automatic tagging as well. It
107might be handy for synchronizing delicious feeds into fc2bookmark.
108
109=head1 AUTHOR
110
111Yasuhiro Matsumoto
112
113=head1 SEE ALSO
114
115L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize>
116
117=cut
Note: See TracBrowser for help on using the browser.