| 1 | package Plagger::Plugin::Publish::FC2Bookmark; |
|---|
| 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_fc2bookmark; |
|---|
| 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 $res = eval { $self->{mech}->get('http://bookmark.fc2.com/user/post') }; |
|---|
| 43 | if ($res && $res->is_success) { |
|---|
| 44 | eval { |
|---|
| 45 | $self->{mech}->submit_form( |
|---|
| 46 | form_number => 2, |
|---|
| 47 | fields => { |
|---|
| 48 | "data[Bookmark][title]" => encode('utf-8', $args->{entry}->title), |
|---|
| 49 | "data[Bookmark][url]" => $args->{entry}->link, |
|---|
| 50 | "data[Bookmark][description]" => $summary, |
|---|
| 51 | "data[Bookmark][tag_txt]" => encode('utf-8', $tag_string), |
|---|
| 52 | "data[Bookmark][allow_access]" => "1", |
|---|
| 53 | "data[Bookmark][favorite]" => "0", |
|---|
| 54 | } |
|---|
| 55 | ) |
|---|
| 56 | }; |
|---|
| 57 | if ($@) { |
|---|
| 58 | $context->log(info => "can't submit: " . $args->{entry}->link); |
|---|
| 59 | } else { |
|---|
| 60 | $context->log(info => "Post entry success."); |
|---|
| 61 | } |
|---|
| 62 | } else { |
|---|
| 63 | $context->log(info => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 67 | $context->log(info => "sleep $sleeping_time."); |
|---|
| 68 | sleep( $sleeping_time ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub login_fc2bookmark { |
|---|
| 72 | my $self = shift; |
|---|
| 73 | unless ($self->conf->{usermail} && $self->conf->{password}) { |
|---|
| 74 | Plagger->context->log(error => 'set your usermail and password before login.'); |
|---|
| 75 | } |
|---|
| 76 | my $res = $self->{mech}->get('http://bookmark.fc2.com/'); |
|---|
| 77 | $self->{mech}->submit_form( |
|---|
| 78 | form_number => 2, |
|---|
| 79 | fields => { |
|---|
| 80 | email => $self->conf->{usermail}, |
|---|
| 81 | pass => $self->conf->{password}, |
|---|
| 82 | }, |
|---|
| 83 | ); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | 1; |
|---|
| 87 | |
|---|
| 88 | __END__ |
|---|
| 89 | |
|---|
| 90 | =head1 NAME |
|---|
| 91 | |
|---|
| 92 | Plagger::Plugin::Publish::FC2Bookmark - Post to fc2bookmark automatically |
|---|
| 93 | |
|---|
| 94 | =head1 SYNOPSIS |
|---|
| 95 | |
|---|
| 96 | - module: Publish::FC2Bookmark |
|---|
| 97 | config: |
|---|
| 98 | usermail: your-e-mail |
|---|
| 99 | password: your-password |
|---|
| 100 | interval: 2 |
|---|
| 101 | post_body: 1 |
|---|
| 102 | |
|---|
| 103 | =head1 DESCRIPTION |
|---|
| 104 | |
|---|
| 105 | This plugin automatically posts feed updates to fc2bookmark |
|---|
| 106 | L<http://bookmark.fc2.com/>. It supports automatic tagging as well. It |
|---|
| 107 | might be handy for synchronizing delicious feeds into fc2bookmark. |
|---|
| 108 | |
|---|
| 109 | =head1 AUTHOR |
|---|
| 110 | |
|---|
| 111 | Yasuhiro Matsumoto |
|---|
| 112 | |
|---|
| 113 | =head1 SEE ALSO |
|---|
| 114 | |
|---|
| 115 | L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize> |
|---|
| 116 | |
|---|
| 117 | =cut |
|---|