| 1 | package Plagger::Plugin::Publish::NiftyClip; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Plugin ); |
|---|
| 4 | |
|---|
| 5 | use Encode; |
|---|
| 6 | use Time::HiRes qw(sleep); |
|---|
| 7 | use Plagger::Mechanize; |
|---|
| 8 | |
|---|
| 9 | sub register { |
|---|
| 10 | my($self, $context) = @_; |
|---|
| 11 | $context->register_hook( |
|---|
| 12 | $self, |
|---|
| 13 | 'publish.entry' => \&add_entry, |
|---|
| 14 | 'publish.init' => \&initialize, |
|---|
| 15 | ); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | sub initialize { |
|---|
| 19 | my $self = shift; |
|---|
| 20 | unless ($self->{mech}) { |
|---|
| 21 | my $mech = Plagger::Mechanize->new; |
|---|
| 22 | $mech->agent_alias('Windows IE 6'); |
|---|
| 23 | $mech->quiet(1); |
|---|
| 24 | $self->{mech} = $mech; |
|---|
| 25 | } |
|---|
| 26 | $self->login_nifty_clip; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | sub add_entry { |
|---|
| 31 | my ($self, $context, $args) = @_; |
|---|
| 32 | |
|---|
| 33 | my @tags = @{$args->{entry}->tags}; |
|---|
| 34 | my $tag_string = @tags ? join(' ', @tags) : ''; |
|---|
| 35 | |
|---|
| 36 | my $summary; |
|---|
| 37 | if ($self->conf->{post_body}) { |
|---|
| 38 | $summary = encode('utf-8', $args->{entry}->body_text); # xxx should be summary |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | my $res = eval { $self->{mech}->get('http://clip.nifty.com/create') }; |
|---|
| 42 | if ($res && $res->is_success) { |
|---|
| 43 | eval { |
|---|
| 44 | $self->{mech}->submit_form( |
|---|
| 45 | form_name => 'savingForm', |
|---|
| 46 | fields => { |
|---|
| 47 | title => encode('utf-8', $args->{entry}->title), |
|---|
| 48 | url => encode('utf-8', $args->{entry}->link), |
|---|
| 49 | comment => $summary, |
|---|
| 50 | suggesttags => $tag_string, |
|---|
| 51 | public_flag => ($self->conf->{default_public} ? 'on' : 'off'), |
|---|
| 52 | } |
|---|
| 53 | ) |
|---|
| 54 | }; |
|---|
| 55 | if ($@) { |
|---|
| 56 | $context->log(info => "can't submit: " . $@); |
|---|
| 57 | } else { |
|---|
| 58 | $context->log(info => "Post entry success."); |
|---|
| 59 | } |
|---|
| 60 | } else { |
|---|
| 61 | $context->log(error => "fail to bookmark HTTP Status: " . $res->code); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 65 | $context->log(info => "sleep $sleeping_time."); |
|---|
| 66 | sleep( $sleeping_time ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | sub login_nifty_clip { |
|---|
| 70 | my $self = shift; |
|---|
| 71 | unless ($self->conf->{username} && $self->conf->{password}) { |
|---|
| 72 | Plagger->context->log(error => 'set your username and password before login.'); |
|---|
| 73 | } |
|---|
| 74 | my $res = $self->{mech}->get('https://clip.nifty.com/login'); |
|---|
| 75 | $self->{mech}->submit_form( |
|---|
| 76 | form_name => 'login-form', |
|---|
| 77 | fields => { |
|---|
| 78 | username => $self->conf->{username}, |
|---|
| 79 | password => $self->conf->{password}, |
|---|
| 80 | }, |
|---|
| 81 | ); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | 1; |
|---|
| 85 | |
|---|
| 86 | __END__ |
|---|
| 87 | |
|---|
| 88 | =head1 NAME |
|---|
| 89 | |
|---|
| 90 | Plagger::Plugin::Publish::NiftyClip - Post to nifty clip automatically |
|---|
| 91 | |
|---|
| 92 | =head1 SYNOPSIS |
|---|
| 93 | |
|---|
| 94 | - module: Publish::NiftyClip |
|---|
| 95 | config: |
|---|
| 96 | username: your-username |
|---|
| 97 | password: your-password |
|---|
| 98 | interval: 2 |
|---|
| 99 | post_body: 1 |
|---|
| 100 | default_public: 1 |
|---|
| 101 | |
|---|
| 102 | =head1 DESCRIPTION |
|---|
| 103 | |
|---|
| 104 | This plugin automatically posts feed updates to nifty clip |
|---|
| 105 | L<http://clip.nifty.com/>. It supports automatic tagging as well. It |
|---|
| 106 | might be handy for synchronizing delicious feeds into nifty clip. |
|---|
| 107 | |
|---|
| 108 | =head1 CONFIG |
|---|
| 109 | |
|---|
| 110 | =over 4 |
|---|
| 111 | |
|---|
| 112 | =item username, password |
|---|
| 113 | |
|---|
| 114 | username and password for Nifty Clip. Required. |
|---|
| 115 | |
|---|
| 116 | =item default_public |
|---|
| 117 | |
|---|
| 118 | Optional. default publish operation value is '1' as publish. |
|---|
| 119 | |
|---|
| 120 | =item interval |
|---|
| 121 | |
|---|
| 122 | Optional. |
|---|
| 123 | |
|---|
| 124 | =item timeout |
|---|
| 125 | |
|---|
| 126 | Optional. |
|---|
| 127 | |
|---|
| 128 | =back |
|---|
| 129 | |
|---|
| 130 | =head1 AUTHOR |
|---|
| 131 | |
|---|
| 132 | Yasuhiro Matsumoto |
|---|
| 133 | |
|---|
| 134 | =head1 SEE ALSO |
|---|
| 135 | |
|---|
| 136 | L<Plagger>, L<Plagger::Plugin::Publish::LivedoorClip>, L<Plagger::Mechanize> |
|---|
| 137 | |
|---|
| 138 | =cut |
|---|