| 1 | package Plagger::Plugin::Publish::HatenaGraph; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Plugin ); |
|---|
| 4 | |
|---|
| 5 | use Encode; |
|---|
| 6 | use WebService::Hatena::Graph; |
|---|
| 7 | |
|---|
| 8 | sub register { |
|---|
| 9 | my($self, $context) = @_; |
|---|
| 10 | $context->register_hook( |
|---|
| 11 | $self, |
|---|
| 12 | 'plugin.init' => \&initialize, |
|---|
| 13 | 'publish.entry' => \&post_to_graph, |
|---|
| 14 | ); |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | sub rule_hook { 'publish.entry' } |
|---|
| 18 | |
|---|
| 19 | sub 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 | |
|---|
| 27 | sub 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 | |
|---|
| 50 | 1; |
|---|
| 51 | |
|---|
| 52 | __END__ |
|---|
| 53 | |
|---|
| 54 | =head1 NAME |
|---|
| 55 | |
|---|
| 56 | Plagger::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 | |
|---|
| 72 | This plugin automatically posts feed updates to Hatena Graph |
|---|
| 73 | L<http://graph.hatena.ne.jp/>. |
|---|
| 74 | |
|---|
| 75 | =head1 AUTHOR |
|---|
| 76 | |
|---|
| 77 | Kan Fushihara <kan.fushihara at gmail.com> |
|---|
| 78 | |
|---|
| 79 | =head1 SEE ALSO |
|---|
| 80 | |
|---|
| 81 | L<Plagger>, L<WebService::Hatena::Graph> |
|---|
| 82 | |
|---|
| 83 | =cut |
|---|