| 1 | package Plagger::Plugin::Publish::YahooBookmark; |
|---|
| 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_yahoo_bookmark; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | sub add_entry { |
|---|
| 32 | my ($self, $context, $args) = @_; |
|---|
| 33 | |
|---|
| 34 | my @tags = @{$args->{entry}->tags}; |
|---|
| 35 | my $tag_string = @tags ? join(',', @tags) : ''; |
|---|
| 36 | |
|---|
| 37 | my $summary; |
|---|
| 38 | if ($self->conf->{post_body}) { |
|---|
| 39 | $summary = encode('utf-8', $args->{entry}->body_text); # xxx should be summary |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | my $uri = URI->new('http://bookmarks.yahoo.co.jp/action/bookmark'); |
|---|
| 43 | $uri->query_form( |
|---|
| 44 | u => $args->{entry}->link, |
|---|
| 45 | t => encode('utf-8', $args->{entry}->title), |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | my $res = eval { $self->{mech}->get($uri->as_string) }; |
|---|
| 49 | if ($res && $res->is_success) { |
|---|
| 50 | eval { |
|---|
| 51 | my $folder = 'root'; |
|---|
| 52 | my $folder_name = $self->conf->{default_folder}; |
|---|
| 53 | # parse folder key from folder name |
|---|
| 54 | if ($folder_name) { |
|---|
| 55 | my $html = $self->{mech}->content; |
|---|
| 56 | utf8::decode($html) unless utf8::is_utf8($html); |
|---|
| 57 | $html =~ s!^.*<select name="newpfid[^>]*>!!isg; |
|---|
| 58 | $html =~ s!</select>.*!!sg; |
|---|
| 59 | $html =~ s!.*<option value=["']([^"']+)["'][^>]*> $folder_name</option>.*!$1!isg; |
|---|
| 60 | $folder = $html if $html =~ /\w\+/; |
|---|
| 61 | } |
|---|
| 62 | $self->{mech}->submit_form( |
|---|
| 63 | form_name => 'form_save', |
|---|
| 64 | fields => { |
|---|
| 65 | tags => encode('utf-8', $tag_string), |
|---|
| 66 | desc => $summary, |
|---|
| 67 | newpfid => $folder, |
|---|
| 68 | visibility => ($self->conf->{default_public} ? 1 : 0), |
|---|
| 69 | }, |
|---|
| 70 | button => 'adminEditAction' |
|---|
| 71 | ) |
|---|
| 72 | }; |
|---|
| 73 | if ($@) { |
|---|
| 74 | $context->log(info => "can't submit: " . $@); |
|---|
| 75 | } else { |
|---|
| 76 | $context->log(info => "Post entry success."); |
|---|
| 77 | } |
|---|
| 78 | } else { |
|---|
| 79 | $context->log(error => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 83 | $context->log(info => "sleep $sleeping_time."); |
|---|
| 84 | sleep( $sleeping_time ); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | sub login_yahoo_bookmark { |
|---|
| 88 | my $self = shift; |
|---|
| 89 | unless ($self->conf->{username} && $self->conf->{password}) { |
|---|
| 90 | Plagger->context->log(error => 'set your username and password before login.'); |
|---|
| 91 | } |
|---|
| 92 | my $res = $self->{mech}->get('http://bookmarks.yahoo.co.jp/all'); |
|---|
| 93 | $self->{mech}->submit_form( |
|---|
| 94 | form_name => 'login_form', |
|---|
| 95 | fields => { |
|---|
| 96 | login => $self->conf->{username}, |
|---|
| 97 | passwd => $self->conf->{password}, |
|---|
| 98 | }, |
|---|
| 99 | ); |
|---|
| 100 | if ($self->{mech}->form_name('login_form')) { |
|---|
| 101 | Plagger->context->log(error => "failed to login to yahoo bookmark."); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | 1; |
|---|
| 106 | |
|---|
| 107 | __END__ |
|---|
| 108 | |
|---|
| 109 | =head1 NAME |
|---|
| 110 | |
|---|
| 111 | Plagger::Plugin::Publish::YahooBookmark - Post to yahoo bookmark automatically |
|---|
| 112 | |
|---|
| 113 | =head1 SYNOPSIS |
|---|
| 114 | |
|---|
| 115 | - module: Publish::YahooBookmark |
|---|
| 116 | config: |
|---|
| 117 | username: your-username |
|---|
| 118 | password: your-password |
|---|
| 119 | interval: 2 |
|---|
| 120 | post_body: 1 |
|---|
| 121 | default_folder: MyFolder |
|---|
| 122 | default_public: 0 |
|---|
| 123 | |
|---|
| 124 | =head1 DESCRIPTION |
|---|
| 125 | |
|---|
| 126 | This plugin automatically posts feed updates to yahoo bookmark |
|---|
| 127 | L<http://bookmarks.yahoo.co.jp/>. It supports automatic tagging as well. It |
|---|
| 128 | might be handy for synchronizing delicious feeds into yahoo bookmark. |
|---|
| 129 | |
|---|
| 130 | =head1 CONFIG |
|---|
| 131 | |
|---|
| 132 | =over 4 |
|---|
| 133 | |
|---|
| 134 | =item username, password |
|---|
| 135 | |
|---|
| 136 | username and password for Yahoo Bookmark. Required. |
|---|
| 137 | |
|---|
| 138 | =item default_folder |
|---|
| 139 | |
|---|
| 140 | Optional. default folder name to post, 'root' is default. |
|---|
| 141 | |
|---|
| 142 | =item default_public |
|---|
| 143 | |
|---|
| 144 | Optional. default publish operation value is 0 as publish. |
|---|
| 145 | |
|---|
| 146 | =item interval |
|---|
| 147 | |
|---|
| 148 | Optional. |
|---|
| 149 | |
|---|
| 150 | =item timeout |
|---|
| 151 | |
|---|
| 152 | Optional. |
|---|
| 153 | |
|---|
| 154 | =back |
|---|
| 155 | |
|---|
| 156 | =head1 AUTHOR |
|---|
| 157 | |
|---|
| 158 | Yasuhiro Matsumoto |
|---|
| 159 | |
|---|
| 160 | =head1 SEE ALSO |
|---|
| 161 | |
|---|
| 162 | L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize> |
|---|
| 163 | |
|---|
| 164 | =cut |
|---|