| 1 | package Plagger::Plugin::Publish::HatenaBookmark; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Plugin ); |
|---|
| 4 | |
|---|
| 5 | use Encode; |
|---|
| 6 | use Time::HiRes qw(sleep); |
|---|
| 7 | use XML::Atom::Client; |
|---|
| 8 | use XML::Atom::Entry; |
|---|
| 9 | |
|---|
| 10 | sub register { |
|---|
| 11 | my($self, $context) = @_; |
|---|
| 12 | |
|---|
| 13 | if ($context->is_loaded('Filter::HatenaBookmarkUsersCount')) { |
|---|
| 14 | $self->conf->{use_filter} = 1; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | $context->register_hook( |
|---|
| 18 | $self, |
|---|
| 19 | 'plugin.init' => \&initialize, |
|---|
| 20 | 'publish.entry' => \&add_entry, |
|---|
| 21 | ); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub rule_hook { 'publish.entry' } |
|---|
| 25 | |
|---|
| 26 | sub initialize { |
|---|
| 27 | my ($self, $context, $args) = @_; |
|---|
| 28 | $self->{client} = XML::Atom::Client->new; |
|---|
| 29 | $self->{client}->username($self->conf->{username}); |
|---|
| 30 | $self->{client}->password($self->conf->{password}); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | sub add_entry { |
|---|
| 34 | my ($self, $context, $args) = @_; |
|---|
| 35 | |
|---|
| 36 | my @tags = @{$args->{entry}->tags}; |
|---|
| 37 | my $tag_string = @tags ? join('', map "[$_]", @tags) : ''; |
|---|
| 38 | |
|---|
| 39 | my $entry = XML::Atom::Entry->new; |
|---|
| 40 | $entry->title('dummy'); |
|---|
| 41 | |
|---|
| 42 | my $link = XML::Atom::Link->new; |
|---|
| 43 | $link->rel('related'); |
|---|
| 44 | $link->type('text/html'); |
|---|
| 45 | $link->href($args->{entry}->link); |
|---|
| 46 | $entry->add_link($link); |
|---|
| 47 | |
|---|
| 48 | if ($self->conf->{post_body}) { |
|---|
| 49 | $entry->summary( encode('utf-8', $tag_string . $args->{entry}->body_text) ); # xxx should be summary |
|---|
| 50 | } else { |
|---|
| 51 | $entry->summary( encode('utf-8', $tag_string) ); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | my $loc = $self->{client}->createEntry('http://b.hatena.ne.jp/atom/post', $entry); |
|---|
| 55 | unless ($loc) { |
|---|
| 56 | $context->log(error => $self->{client}->errstr); |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | my $sleeping_time = $self->conf->{interval} || 3; |
|---|
| 61 | $context->log(info => "Post entry success. sleep $sleeping_time."); |
|---|
| 62 | sleep( $sleeping_time ); |
|---|
| 63 | |
|---|
| 64 | if ($self->conf->{post_title} && $self->conf->{use_filter}) { |
|---|
| 65 | if (! $args->{entry}->meta->{hatenabookmark_users}) { |
|---|
| 66 | my $entrytitle = XML::Atom::Entry->new; |
|---|
| 67 | $entrytitle->title(encode('utf-8', $args->{entry}->title)); |
|---|
| 68 | $entrytitle->summary( encode('utf-8', $tag_string . $args->{entry}->body_text) ); # xxx should be summary |
|---|
| 69 | unless ($self->{client}->updateEntry($loc, $entrytitle)) { |
|---|
| 70 | $context->log(error => $self->{client}->errstr); |
|---|
| 71 | } |
|---|
| 72 | $context->log(info => "Edit entry title success."); |
|---|
| 73 | } else { |
|---|
| 74 | $context->log(info => "Edit entry title skip."); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | 1; |
|---|
| 80 | |
|---|
| 81 | __END__ |
|---|
| 82 | |
|---|
| 83 | =head1 NAME |
|---|
| 84 | |
|---|
| 85 | Plagger::Plugin::Publish::HatenaBookmark - Post to Hatena::Bookmark automatically |
|---|
| 86 | |
|---|
| 87 | =head1 SYNOPSIS |
|---|
| 88 | |
|---|
| 89 | - module: Publish::HatenaBookmark |
|---|
| 90 | config: |
|---|
| 91 | username: your-username |
|---|
| 92 | password: your-password |
|---|
| 93 | interval: 2 |
|---|
| 94 | post_body: 1 |
|---|
| 95 | post_title: 0 |
|---|
| 96 | |
|---|
| 97 | =head1 DESCRIPTION |
|---|
| 98 | |
|---|
| 99 | This plugin automatically posts feed updates to Hatena Bookmark |
|---|
| 100 | L<http://b.hatena.ne.jp/>. It supports automatic tagging as well. It |
|---|
| 101 | might be handy for synchronizing delicious feeds into Hatena Bookmark. |
|---|
| 102 | |
|---|
| 103 | =head1 CONFIG |
|---|
| 104 | |
|---|
| 105 | =over 4 |
|---|
| 106 | |
|---|
| 107 | =item post_title |
|---|
| 108 | |
|---|
| 109 | When you set to 1, title is modified. |
|---|
| 110 | By specification, It does not modify URI which is registered already. |
|---|
| 111 | Filter::HatenaBookmarkUsersCount is also used simultaneously. |
|---|
| 112 | |
|---|
| 113 | - module: Filter::HatenaBookmarkUsersCount |
|---|
| 114 | |
|---|
| 115 | Optional and defaults to 0 |
|---|
| 116 | |
|---|
| 117 | =back |
|---|
| 118 | |
|---|
| 119 | =head1 AUTHOR |
|---|
| 120 | |
|---|
| 121 | fuba |
|---|
| 122 | |
|---|
| 123 | =head1 SEE ALSO |
|---|
| 124 | |
|---|
| 125 | L<Plagger>, L<Plagger::Plugin::Publish::Delicious>, L<XML::Atom>, L<Plagger::Plugin::Filter::HatenaBookmarkUsersCount> |
|---|
| 126 | |
|---|
| 127 | =cut |
|---|