| 1 | package Plagger::Plugin::Publish::Pookmark; |
|---|
| 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_pookmark; |
|---|
| 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 | if ($self->conf->{default_later}) { |
|---|
| 43 | my $uri = URI->new('http://pookmark.jp/later'); |
|---|
| 44 | $uri->query_form( |
|---|
| 45 | url => $args->{entry}->link, |
|---|
| 46 | title => encode('utf-8', $args->{entry}->title), |
|---|
| 47 | ); |
|---|
| 48 | my $res = eval { $self->{mech}->get($uri->as_string) }; |
|---|
| 49 | if ($res && $res->is_success) { |
|---|
| 50 | $context->log(info => "Post entry success."); |
|---|
| 51 | } else { |
|---|
| 52 | $context->log(info => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 53 | } |
|---|
| 54 | } else { |
|---|
| 55 | my $uri = URI->new('http://pookmark.jp/post'); |
|---|
| 56 | $uri->query_form( |
|---|
| 57 | url => $args->{entry}->link, |
|---|
| 58 | title => encode('utf-8', $args->{entry}->title), |
|---|
| 59 | ); |
|---|
| 60 | my $res = eval { $self->{mech}->get($uri->as_string) }; |
|---|
| 61 | if ($res && $res->is_success) { |
|---|
| 62 | eval { |
|---|
| 63 | my $post_form = $self->{mech}->form_number(2); |
|---|
| 64 | my $no_twitter = $post_form->find_input('no_twitter'); |
|---|
| 65 | $no_twitter->value('on') if $no_twitter && $self->conf->{default_no_twitter}; |
|---|
| 66 | $self->{mech}->submit_form( |
|---|
| 67 | form_number => 2, |
|---|
| 68 | fields => { |
|---|
| 69 | url => $args->{entry}->link, |
|---|
| 70 | title => encode('utf-8', $args->{entry}->title), |
|---|
| 71 | comment => $summary, |
|---|
| 72 | tags => encode('utf-8', $tag_string), |
|---|
| 73 | private => ($self->conf->{default_private} ? 'on' : 'off'), |
|---|
| 74 | } |
|---|
| 75 | ) |
|---|
| 76 | }; |
|---|
| 77 | if ($@) { |
|---|
| 78 | $context->log(info => "can't submit: " . $args->{entry}->link); |
|---|
| 79 | } else { |
|---|
| 80 | $context->log(info => "Post entry success."); |
|---|
| 81 | } |
|---|
| 82 | } else { |
|---|
| 83 | $context->log(info => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 88 | $context->log(info => "sleep $sleeping_time."); |
|---|
| 89 | sleep( $sleeping_time ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | sub login_pookmark { |
|---|
| 93 | my $self = shift; |
|---|
| 94 | unless ($self->conf->{jugemkey_id} && $self->conf->{password}) { |
|---|
| 95 | Plagger->context->log(error => 'set your jugemkey_id and password before login.'); |
|---|
| 96 | } |
|---|
| 97 | my $uri = URI->new('http://pookmark.jp/ajax/login'); |
|---|
| 98 | $uri->query_form( |
|---|
| 99 | username => $self->conf->{jugemkey_id}, |
|---|
| 100 | password => $self->conf->{password}, |
|---|
| 101 | ); |
|---|
| 102 | my $res = $self->{mech}->get($uri->as_string); |
|---|
| 103 | return $res ? $res->content eq '1' : 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | 1; |
|---|
| 107 | |
|---|
| 108 | __END__ |
|---|
| 109 | |
|---|
| 110 | =head1 NAME |
|---|
| 111 | |
|---|
| 112 | Plagger::Plugin::Publish::Pookmark - Post to POOKMARK Airlines automatically |
|---|
| 113 | |
|---|
| 114 | =head1 SYNOPSIS |
|---|
| 115 | |
|---|
| 116 | - module: Publish::Pookmark |
|---|
| 117 | config: |
|---|
| 118 | jugemkey_id: your-jugemkey-id |
|---|
| 119 | password: your-password |
|---|
| 120 | interval: 2 |
|---|
| 121 | post_body: 1 |
|---|
| 122 | default_no_twitter: 0 |
|---|
| 123 | default_private: 0 |
|---|
| 124 | |
|---|
| 125 | =head1 DESCRIPTION |
|---|
| 126 | |
|---|
| 127 | This plugin automatically posts feed updates to POOKMARK Airlines |
|---|
| 128 | L<http://pookmark.jp/>. It supports automatic tagging as well. It |
|---|
| 129 | might be handy for synchronizing delicious feeds into pookmark. |
|---|
| 130 | |
|---|
| 131 | =head1 AUTHOR |
|---|
| 132 | |
|---|
| 133 | Yasuhiro Matsumoto |
|---|
| 134 | |
|---|
| 135 | =head1 SEE ALSO |
|---|
| 136 | |
|---|
| 137 | L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize> |
|---|
| 138 | |
|---|
| 139 | =cut |
|---|