| 1 | package Plagger::Plugin::Publish::GooBookmark; |
|---|
| 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_goo_bookmark; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | sub add_entry { |
|---|
| 32 | my ($self, $context, $args) = @_; |
|---|
| 33 | |
|---|
| 34 | my @tags = @{$args->{entry}->tags}; |
|---|
| 35 | map{ tr/,/_/ } @tags; # separator as comma in goo bookmark |
|---|
| 36 | my $tag_string = @tags ? join(',', @tags) : ''; |
|---|
| 37 | |
|---|
| 38 | my $summary; |
|---|
| 39 | if ($self->conf->{post_body}) { |
|---|
| 40 | $summary = encode('utf-8', $args->{entry}->body_text); # xxx should be summary |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | my $uri = URI->new('http://bookmark.goo.ne.jp/add/detail/'); |
|---|
| 44 | $uri->query_form( |
|---|
| 45 | url => $args->{entry}->link, |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | my $res = eval { $self->{mech}->get($uri->as_string) }; |
|---|
| 49 | if ($res && $res->is_success) { |
|---|
| 50 | eval { |
|---|
| 51 | $self->{mech}->submit_form( |
|---|
| 52 | form_name => 'boomarkEdit', |
|---|
| 53 | fields => { |
|---|
| 54 | title => encode('utf-8', $args->{entry}->title), |
|---|
| 55 | keywordlist => encode('utf-8', $tag_string), |
|---|
| 56 | comment => $summary, |
|---|
| 57 | publicno => ($self->conf->{default_publicno} || 0), |
|---|
| 58 | point => ($self->conf->{default_rate} || 1), |
|---|
| 59 | }, |
|---|
| 60 | button => ($self->{mech}->form_name('boomarkEdit')->find_input('addDetail') ? 'addDetail' : 'editEdit') |
|---|
| 61 | ) |
|---|
| 62 | }; |
|---|
| 63 | if ($@) { |
|---|
| 64 | $context->log(info => "can't submit: " . $@); |
|---|
| 65 | } else { |
|---|
| 66 | $context->log(info => "Post entry success."); |
|---|
| 67 | } |
|---|
| 68 | } else { |
|---|
| 69 | $context->log(error => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 73 | $context->log(info => "sleep $sleeping_time."); |
|---|
| 74 | sleep( $sleeping_time ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub login_goo_bookmark { |
|---|
| 78 | my $self = shift; |
|---|
| 79 | unless ($self->conf->{username} && $self->conf->{password}) { |
|---|
| 80 | Plagger->context->log(error => 'set your username and password before login.'); |
|---|
| 81 | } |
|---|
| 82 | my $res = $self->{mech}->get('https://login.mail.goo.ne.jp/certify-cgi/login.cgi?site=bookmark.goo.ne.jp'); |
|---|
| 83 | $self->{mech}->submit_form( |
|---|
| 84 | form_name => 'f1', |
|---|
| 85 | fields => { |
|---|
| 86 | uname => $self->conf->{username}, |
|---|
| 87 | pass => $self->conf->{password}, |
|---|
| 88 | }, |
|---|
| 89 | ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | 1; |
|---|
| 93 | |
|---|
| 94 | __END__ |
|---|
| 95 | |
|---|
| 96 | =head1 NAME |
|---|
| 97 | |
|---|
| 98 | Plagger::Plugin::Publish::GooBookmark - Post to goo bookmark automatically |
|---|
| 99 | |
|---|
| 100 | =head1 SYNOPSIS |
|---|
| 101 | |
|---|
| 102 | - module: Publish::GooBookmark |
|---|
| 103 | config: |
|---|
| 104 | username: your-username |
|---|
| 105 | password: your-password |
|---|
| 106 | interval: 2 |
|---|
| 107 | post_body: 1 |
|---|
| 108 | default_rate: 1 |
|---|
| 109 | default_publicno: 0 |
|---|
| 110 | |
|---|
| 111 | =head1 DESCRIPTION |
|---|
| 112 | |
|---|
| 113 | This plugin automatically posts feed updates to goo bookmark |
|---|
| 114 | L<http://bookmark.goo.ne.jp/>. It supports automatic tagging as well. It |
|---|
| 115 | might be handy for synchronizing delicious feeds into goo bookmark. |
|---|
| 116 | |
|---|
| 117 | =head1 CONFIG |
|---|
| 118 | |
|---|
| 119 | =over 4 |
|---|
| 120 | |
|---|
| 121 | =item username, password |
|---|
| 122 | |
|---|
| 123 | username and password for Goo Bookmark. Required. |
|---|
| 124 | |
|---|
| 125 | =item default_rate |
|---|
| 126 | |
|---|
| 127 | Optional. default rate value is 1. |
|---|
| 128 | |
|---|
| 129 | =item default_publicno |
|---|
| 130 | |
|---|
| 131 | Optional. default publish operation value is 0 as publish. |
|---|
| 132 | |
|---|
| 133 | =item interval |
|---|
| 134 | |
|---|
| 135 | Optional. |
|---|
| 136 | |
|---|
| 137 | =item timeout |
|---|
| 138 | |
|---|
| 139 | Optional. |
|---|
| 140 | |
|---|
| 141 | =back |
|---|
| 142 | |
|---|
| 143 | =head1 AUTHOR |
|---|
| 144 | |
|---|
| 145 | Yasuhiro Matsumoto |
|---|
| 146 | Masafumi Otsune |
|---|
| 147 | |
|---|
| 148 | =head1 SEE ALSO |
|---|
| 149 | |
|---|
| 150 | L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize> |
|---|
| 151 | |
|---|
| 152 | =cut |
|---|