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

Revision 8395, 3.0 kB (checked in by mattn, 8 months ago)

lang/perl/plagger/lib/Plagger/Plugin/Publish/Diigo.pm:
fixed tabs to spaces. X-(

Line 
1package Plagger::Plugin::Publish::Diigo;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use Time::HiRes qw(sleep);
7use Plagger::Mechanize;
8use JSON::Syck;
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 Mozilla');
24        $mech->quiet(1);
25        $self->{mech} = $mech;
26    }
27    $self->login_diigo;
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 $body = JSON::Syck::Dump({
43        title       => encode('utf-8', $args->{entry}->title),
44        description => $summary || '',
45        tags        => $tag_string,
46        url         => encode('utf-8', $args->{entry}->link),
47        mode        => ($self->conf->{default_public} ? '0' : '2'),
48    });
49
50    my $uri = URI->new('http://preview.diigo.com/bookmarklet2');
51    $uri->query_form(
52        cmd  => 'bm_saveBookmark',
53        json => $body,
54        v    => 11,
55    );
56    my $res = eval { $self->{mech}->get($uri->as_string) };
57    if (!$res || !$res->is_success) {
58        $context->log(info => "can't submit: " . $args->{entry}->link);
59    } else {
60        $context->log(info => "Post entry success.");
61    }
62 
63    my $sleeping_time = $self->conf->{interval} || 3;
64    $context->log(info => "sleep $sleeping_time.");
65    sleep( $sleeping_time );
66}
67
68sub login_diigo {
69    my $self = shift;
70    unless ($self->conf->{username} && $self->conf->{password}) {
71        Plagger->context->log(error => 'set your username and password before login.');
72    }
73    my $res = $self->{mech}->get('http://www.diigo.com/sign-in');
74    $self->{mech}->submit_form(
75        form_name => 'loginForm',
76        fields => {
77            username => $self->conf->{username},
78            password => $self->conf->{password},
79        },
80    );
81    $res && $res->is_success;
82}
83
841;
85
86__END__
87
88=head1 NAME
89
90Plagger::Plugin::Publish::Diigo - Post to diigo bookmark automatically
91
92=head1 SYNOPSIS
93
94  - module: Publish::Diigo
95    config:
96      username: your-username
97      password: your-password
98      interval: 2
99      post_body: 1
100      #default_public: 1
101
102=head1 DESCRIPTION
103
104This plugin automatically posts feed updates to diigo bookmark
105L<http://www.diigo.com/>. It supports automatic tagging as well. It
106might be handy for synchronizing delicious feeds into diigo bookmark.
107
108=head1 CONFIG
109
110=over 4
111
112=item username, password
113
114username and password for Diigo. Required.
115
116=item default_public
117
118Optional. default publish operation value is '1' as publish.
119
120=item interval
121
122Optional.
123
124=item timeout
125
126Optional.
127
128=back
129
130=head1 AUTHOR
131
132Yasuhiro Matsumoto
133
134=head1 SEE ALSO
135
136L<Plagger>, L<Plagger::Mechanize>
137
138=cut
Note: See TracBrowser for help on using the browser.