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

Revision 4459, 2.0 kB (checked in by kan, 11 months ago)

add Plagger::Plugin::Publish::HatenaGraph?

Line 
1package Plagger::Plugin::Publish::HatenaGraph;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use WebService::Hatena::Graph;
7
8sub register {
9    my($self, $context) = @_;
10    $context->register_hook(
11        $self,
12        'plugin.init'   => \&initialize,
13        'publish.entry' => \&post_to_graph,
14    );
15}
16
17sub rule_hook { 'publish.entry' }
18
19sub initialize {
20    my ($self, $context, $args) = @_;
21    $self->{graph} = WebService::Hatena::Graph->new(
22        username => $self->conf->{username},
23        password => $self->conf->{password},
24    );
25}
26
27sub post_to_graph {
28    my ($self, $context, $args) = @_;
29
30    for my $graph (@{$self->conf->{graphs}}) {
31        for my $value (@{$args->{entry}->meta->{$graph->{meta_name}}||[]}) {
32            if ($graph->{append}) {
33                # append: 1$B$J$i!"8=:_$N%0%i%U$NCM$K2C;;(B
34                my $data = $self->{graph}->get_data(
35                    graphname => encode('utf-8', $graph->{graph_name}),
36                    username  => $self->conf->{username},
37                );
38                $value += $data->{$args->{entry}->date->ymd} || 0;
39            }
40            $self->{graph}->post_data(
41                graphname => encode('utf-8', $graph->{graph_name}),
42                date      => $args->{entry}->date->ymd,
43                value     => $value,
44            );
45            $context->log(info => "Post $value to graph '@{[$graph->{graph_name}]}' success. ");
46        }
47    }
48}
49
501;
51
52__END__
53
54=head1 NAME
55
56Plagger::Plugin::Publish::HatenaGraph - Post to Hatena::Graph automatically
57
58=head1 SYNOPSIS
59
60  - module: Publish::HatenaGraph
61    config:
62      username: your-username
63      password: your-password
64      graphs:
65        -
66          title: today's weight
67          name: weight
68          pattern: ([0-9.]+)kg
69
70=head1 DESCRIPTION
71
72This plugin automatically posts feed updates to Hatena Graph
73L<http://graph.hatena.ne.jp/>.
74
75=head1 AUTHOR
76
77Kan Fushihara <kan.fushihara at gmail.com>
78
79=head1 SEE ALSO
80
81L<Plagger>, L<WebService::Hatena::Graph>
82
83=cut
Note: See TracBrowser for help on using the browser.