root/trunk/plagger/lib/Plagger/Plugin/Publish/HatenaDiary.pm

Revision 1998 (checked in by ko, 13 years ago)

added Publish::HatenaDiary?

Line 
1 package Plagger::Plugin::Publish::HatenaDiary;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Encode;
6 use WWW::HatenaDiary;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'plugin.init'      => \&initialize,
13         'publish.init'     => \&publish_init,
14         'publish.entry'    => \&publish_entry,
15         'publish.finalize' => \&publish_finalize,
16     );
17 }
18
19 sub initialize {
20     my($self, $context) = @_;
21     my $config = {
22         username => $self->conf->{username},
23         password => $self->conf->{password},
24         group    => $self->conf->{group},
25         mech_opt => {
26             agent => Plagger::UserAgent->new,
27         },
28     };
29     $self->{diary} = WWW::HatenaDiary->new($config);
30 }
31
32 sub publish_init {
33     my($self, $context, $args) = @_;
34     local $@;
35     eval { $self->{diary}->login };
36     if ($@) {
37         $context->log(error => $@);
38         delete $self->{diary};
39     }
40 }
41
42 sub publish_entry {
43     my($self, $context, $args) = @_;
44     return unless $self->{diary};
45
46     my $body = $self->templatize('template.tt', $args);
47     my $uri = $self->{diary}->create({
48         title => encode_utf8( $args->{entry}->title_text ),
49         body  => encode_utf8( $body ),
50     });
51     $context->log(debug => "Post entry success: $uri");
52
53     my $sleeping_time = $self->conf->{interval} || 3;
54     $context->log(info => "sleep $sleeping_time.");
55     sleep( $sleeping_time );
56 }
57
58 sub publish_finalize {
59     my($self, $context, $args) = @_;
60     return unless $self->{diary};
61     $self->{diary}->{login}->logout;
62 }
63
64 1;
65 __END__
66
67 =head1 NAME
68
69 Plagger::Plugin::Publish::HatenaDiary - Publish to HatenaDiary
70
71 =head1 SYNOPSIS
72
73   - module: Publish::HatenaDiary
74     config:
75       username: hatena-id
76       password: hatena-password
77
78 =head1 DESCRIPTION
79
80 This plugin sends feed entries to your Hatena Diary.
81
82 =head1 CONFIG
83
84 =over 4
85
86 =item username
87
88 Hatena username. Required.
89
90 =item password
91
92 Hatena password. Required.
93
94 =item group
95
96 Hatena group name. Optional.
97
98 =item interval
99
100 Optional.
101
102 =back
103
104 =head1 AUTHOR
105
106 Kazuhiro Osawa
107
108 =head1 SEE ALSO
109
110 L<Plagger>, L<WWW::HatenaDiary>
111
112 =cut
Note: See TracBrowser for help on using the browser.