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

Revision 680, 4.1 kB (checked in by mattn, 14 months ago)

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

Line 
1package Plagger::Plugin::Publish::Magnolia;
2use strict;
3use base qw( Plagger::Plugin );
4
5use XML::DOM;
6use Encode;
7use Time::HiRes qw(sleep);
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->add_header('Accept-Encoding', 'identity');
25        $mech->quiet(1);
26        $self->{mech} = $mech;
27    }
28    $self->{apikey} = $self->login_magnolia;
29}
30
31
32sub add_entry {
33    my ($self, $context, $args) = @_;
34
35    my @tags = @{$args->{entry}->tags};
36    my $tag_string = @tags ? join(',', @tags) : '';
37
38    my $summary;
39    if ($self->conf->{post_body}) {
40        $summary = encode('utf-8', $args->{entry}->body_text); # xxx should be summary
41    }
42
43        my $uri = URI->new('http://ma.gnolia.com/api/rest/1/bookmarks_add');
44    $uri->query_form(
45        api_key     => $self->{apikey},
46        title       => encode('utf-8', $args->{entry}->title),
47        description => $summary,
48        url         => $args->{entry}->link,
49        private     => ($self->conf->{default_private} || 0),
50        tags        => encode('utf-8', $tag_string),
51        rating      => ($self->conf->{default_rating} || 0),
52    );
53
54    my $res = eval { $self->{mech}->get($uri->as_string) };
55    if ($res && $res->is_success) {
56                my $topNode;
57                my $status;
58        eval {
59            $topNode = XML::DOM::Parser->new->parse( $res->content );
60            $status = lc $topNode->getElementsByTagName('response')->[0]->getAttribute('status');
61        };
62        if ($@) {
63            $context->log(info => "can't submit: " . $@);
64        } elsif ($status ne 'ok') {
65            $context->log(info => "can't submit: " . $status);
66        } else {
67            $context->log(info => "Post entry success.");
68        }
69    } else {
70       $context->log(error => "fail to bookmark HTTP Status: " . $res->code);
71    }
72 
73    my $sleeping_time = $self->conf->{interval} || 3;
74    $context->log(info => "sleep $sleeping_time.");
75    sleep( $sleeping_time );
76}
77
78sub login_magnolia {
79    my $self = shift;
80    unless ($self->conf->{username} && $self->conf->{password}) {
81        Plagger->context->log(error => 'set your username and password before login.');
82    }
83        my $uri = URI->new('https://ma.gnolia.com/api/rest/1/get_key');
84    $uri->query_form(
85        id => $self->conf->{username},
86        password => $self->conf->{password},
87    );
88    my $res = $self->{mech}->get($uri->as_string);
89        my $apikey;
90        eval {
91        my $topNode = XML::DOM::Parser->new->parse( $res->content );
92        my $responseNode = $topNode->getElementsByTagName('response')->[0];
93        if ("ok" eq lc $responseNode->getAttribute('status')) {
94                        my $keyNode = $responseNode->getElementsByTagName('key')->[0];
95                    $apikey = $keyNode->getChildNodes->[0]->getNodeValue;
96        }
97    };
98        unless($apikey) {
99        Plagger->context->log(error => "failed to login to ma.gnolia.");
100    }
101        $apikey;
102}
103
1041;
105
106__END__
107
108=head1 NAME
109
110Plagger::Plugin::Publish::Magnolia - Post to ma.gnolia automatically
111
112=head1 SYNOPSIS
113
114  - module: Publish::Magnolia
115    config:
116      username: your-username
117      password: your-password
118      interval: 2
119      post_body: 1
120      default_private: 0
121      default_rating: 3
122
123=head1 DESCRIPTION
124
125This plugin automatically posts feed updates to magnolia
126L<http://ma.gnolia.com/>. It supports automatic tagging as well. It
127might be handy for synchronizing delicious feeds into ma.gnolia.
128
129=head1 CONFIG
130
131=over 4
132
133=item username, password
134
135username and password for Magnolia. Required.
136
137=item default_private
138
139Optional. default private operation value is 1 as private.
140
141=item default_rating
142
143Optional. default rate value is 0.
144
145=item interval
146
147Optional.
148
149=item timeout
150
151Optional.
152
153=back
154
155=head1 NOTE
156
157ma.gnolia API require api_key, thus you should enable API access at
158L<http://ma.gnolia.com/account/advanced/>.
159
160=head1 AUTHOR
161
162Yasuhiro Matsumoto
163
164=head1 SEE ALSO
165
166L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize>
167
168=cut
Note: See TracBrowser for help on using the browser.