| 1 | package Plagger::Plugin::Publish::Delicious; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | use Net::Delicious; | ||
| 7 | |||
| 8 | sub register { | ||
| 9 | my($self, $context) = @_; | ||
| 10 | $context->register_hook( | ||
| 11 | $self, | ||
| 12 | 'plugin.init' => \&initialize, | ||
| 13 | 'publish.entry' => \&add_entry, | ||
| 14 | ); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub rule_hook { 'publish.entry' } | ||
| 18 | |||
| 19 | sub initialize { | ||
| 20 | my ($self, $context, $args) = @_; | ||
| 21 | $self->{delicious} = Net::Delicious->new({ | ||
| 22 | user => $self->conf->{username}, | ||
| 23 | pswd => $self->conf->{password}, | ||
| 24 | }); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub add_entry { | ||
| 28 | my($self, $context, $args) = @_; | ||
| 29 | |||
| 30 | my @tags = @{$args->{entry}->tags}; | ||
| 31 | my $tag_string = @tags ? join(' ', @tags) : ''; | ||
| 32 | |||
| 33 | my $params = { | ||
| 34 | url => $args->{entry}->link, | ||
| 35 | description => encode('utf-8', $args->{entry}->title), | ||
| 36 | tags => encode('utf-8', $tag_string), | ||
| 37 | }; | ||
| 38 | |||
| 39 | if ($self->conf->{post_body}) { | ||
| 40 | $params->{extended} = encode('utf-8', $args->{entry}->body_text), | ||
| 41 | } | ||
| 42 | |||
| 43 | $self->{delicious}->add_post($params); | ||
| 44 | |||
| 45 | my $sleeping_time = $self->conf->{interval} || 3; | ||
| 46 | $context->log(info => "Post entry success. sleep $sleeping_time."); | ||
| 47 | sleep( $sleeping_time ); | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |