| 1 | package Plagger::Plugin::Publish::HatenaHaiku; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Plugin ); |
|---|
| 4 | |
|---|
| 5 | use Encode; |
|---|
| 6 | use Time::HiRes qw(sleep); |
|---|
| 7 | use URI; |
|---|
| 8 | use Plagger::Mechanize; |
|---|
| 9 | |
|---|
| 10 | sub register { |
|---|
| 11 | my($self, $context) = @_; |
|---|
| 12 | $context->register_hook( |
|---|
| 13 | $self, |
|---|
| 14 | 'publish.entry' => \&add_entry, |
|---|
| 15 | 'publish.init' => \&initialize, |
|---|
| 16 | ); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | sub 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 | |
|---|
| 31 | sub 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 | |
|---|
| 79 | sub 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 | |
|---|
| 97 | 1; |
|---|
| 98 | |
|---|
| 99 | __END__ |
|---|
| 100 | |
|---|
| 101 | =head1 NAME |
|---|
| 102 | |
|---|
| 103 | Plagger::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 | |
|---|
| 118 | This plugin automatically posts feed updates to hatena haiku |
|---|
| 119 | L<http://h.hatena.ne.jp/>. It supports automatic tagging(keyword) as well. |
|---|
| 120 | It 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 | |
|---|
| 128 | username and password for Hatena Haiku. Required. |
|---|
| 129 | |
|---|
| 130 | =item default_keyword |
|---|
| 131 | |
|---|
| 132 | default keyword string. Required. |
|---|
| 133 | |
|---|
| 134 | =item keyword_behaviour |
|---|
| 135 | |
|---|
| 136 | Optional. if set 'title', it should accept 'plagger' in '[plagger]title'. |
|---|
| 137 | if set 'tag', it should accept a first of tags in feed. |
|---|
| 138 | |
|---|
| 139 | =item interval |
|---|
| 140 | |
|---|
| 141 | Optional. |
|---|
| 142 | |
|---|
| 143 | =item timeout |
|---|
| 144 | |
|---|
| 145 | Optional. |
|---|
| 146 | |
|---|
| 147 | =back |
|---|
| 148 | |
|---|
| 149 | =head1 AUTHOR |
|---|
| 150 | |
|---|
| 151 | Yasuhiro Matsumoto |
|---|
| 152 | |
|---|
| 153 | =head1 SEE ALSO |
|---|
| 154 | |
|---|
| 155 | L<Plagger>, L<Plagger::Mechanize> |
|---|
| 156 | |
|---|
| 157 | =cut |
|---|