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

Revision 4855, 3.7 kB (checked in by mattn, 11 months ago)

lang/perl/plagger/lib/Plagger/Plugin/Publish/HatenaHaiku.pm: added Publish::HatenaHaiku?

Line 
1package Plagger::Plugin::Publish::HatenaHaiku;
2use strict;
3use base qw( Plagger::Plugin );
4
5use Encode;
6use Time::HiRes qw(sleep);
7use URI;
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->quiet(1);
25        $self->{mech} = $mech;
26    }
27    $self->login_hatena_haiku;
28}
29
30
31sub add_entry {
32    my ($self, $context, $args) = @_;
33
34    unless ($self->conf->{default_keyword}) {
35        Plagger->context->log(error => 'set default_keyword.');
36    }
37    my $summary = encode('utf-8', $args->{entry}->title)
38        . "\n" . encode('utf-8', $args->{entry}->link);
39    my $keyword = $self->conf->{default_keyword};
40
41    my $keyword_behaviour = $self->conf->{keyword_behaviour};
42    if ('default' ne $keyword_behaviour) {
43        if ('tag' eq $keyword_behaviour) {
44            my @tags = @{$args->{entry}->tags};
45            $keyword = $tags[0] if ($tags[0]);
46        }
47        if ('title' eq $keyword_behaviour) {
48            if ($summary =~ /^\[([^\]]+)\]/) {
49                $keyword = $1;
50            }
51        }
52    }
53
54    my $res = eval { $self->{mech}->get('http://h.hatena.ne.jp/') };
55    if ($res && $res->is_success) {
56        eval {
57            $self->{mech}->submit_form(
58                form_number => 1,
59                fields => {
60                    word       => encode('utf-8', $keyword),
61                    body       => $summary,
62                },
63            )
64        };
65        if ($@) {
66            $context->log(info => "can't submit: " . $@);
67        } else {
68            $context->log(info => "Post entry success.");
69        }
70    } else {
71       $context->log(error => "fail to post HTTP Status: " . $res->code);
72    }
73 
74    my $sleeping_time = $self->conf->{interval} || 3;
75    $context->log(info => "sleep $sleeping_time.");
76    sleep( $sleeping_time );
77}
78
79sub login_hatena_haiku {
80    my $self = shift;
81    unless ($self->conf->{username} && $self->conf->{password}) {
82        Plagger->context->log(error => 'set your username and password before login.');
83    }
84    my $res = $self->{mech}->get('https://www.hatena.ne.jp/login?location=http://h.hatena.ne.jp/');
85    $self->{mech}->submit_form(
86        form_number => 1,
87        fields => {
88            name  => $self->conf->{username},
89            password => $self->conf->{password},
90        },
91    );
92    if ($self->{mech}->content !~ 'http-equiv="Refresh"') {
93        Plagger->context->log(error => "failed to login to hatena haiku.");
94    }
95}
96
971;
98
99__END__
100
101=head1 NAME
102
103Plagger::Plugin::Publish::HatenaHaiku - Post to hatena haiku automatically
104
105=head1 SYNOPSIS
106
107  - module: Publish::HatenaHaiku
108    config:
109      username: your-username
110      password: your-password
111      default_keyword: id:mattn
112      #keyword_behaviour: title
113      #keyword_behaviour: tag
114      #interval: 2
115
116=head1 DESCRIPTION
117
118This plugin automatically posts feed updates to hatena haiku
119L<http://h.hatena.ne.jp/>. It supports automatic tagging(keyword) as well.
120It might be handy for synchronizing your blog feeds into hatena haiku.
121
122=head1 CONFIG
123
124=over 4
125
126=item username, password
127
128username and password for Hatena Haiku. Required.
129
130=item default_keyword
131
132default keyword string. Required.
133
134=item keyword_behaviour
135
136Optional. if set 'title', it should accept 'plagger' in '[plagger]title'.
137if set 'tag', it should accept a first of tags in feed.
138
139=item interval
140
141Optional.
142
143=item timeout
144
145Optional.
146
147=back
148
149=head1 AUTHOR
150
151Yasuhiro Matsumoto
152
153=head1 SEE ALSO
154
155L<Plagger>, L<Plagger::Mechanize>
156
157=cut
Note: See TracBrowser for help on using the browser.