root/trunk/plagger/lib/Plagger/Plugin/Publish/HatenaBookmark.pm

Revision 746 (checked in by miyagawa, 14 years ago)

apply misc match to Hatena plugins from http://d.hatena.ne.jp/j708/20060515#1147682524

  • Property svn:keywords set to Id Revision
Line 
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     $context->register_hook(
13         $self,
14         'publish.init'        => \&initialize,
15         'publish.entry.fixup' => \&add_entry,
16     );
17 }
18
19 sub rule_hook { 'publish.entry.fixup' }
20
21 sub initialize {
22     my ($self, $context, $args) = @_;
23     $self->{client} = XML::Atom::Client->new;
24     $self->{client}->username($self->conf->{username});
25     $self->{client}->password($self->conf->{password});
26 }
27
28 sub add_entry {
29     my ($self, $context, $args) = @_;
30
31     my @tags = @{$args->{entry}->tags};
32     my $tag_string = @tags ? join('', map "[$_]", @tags) : '';
33
34     my $entry = XML::Atom::Entry->new;
35     $entry->title(encode('utf-8', $args->{entry}->title));
36
37     my $link  = XML::Atom::Link->new;
38     $link->rel('related');
39     $link->type('text/html');
40     $link->href($args->{entry}->link);
41     $entry->add_link($link);
42
43     if ($self->conf->{post_body}) {
44         $entry->summary( encode('utf-8', $tag_string . $args->{entry}->body_text) ); # xxx should be summary
45     } else {
46         $entry->summary( encode('utf-8', $tag_string) );
47     }
48
49     my $loc = $self->{client}->createEntry('http://b.hatena.ne.jp/atom/post', $entry);
50     unless ($loc) {
51         $context->log(error => $self->{client}->errstr);
52         return;
53     }
54
55     my $sleeping_time = $self->conf->{interval} || 3;
56     $context->log(info => "Post entry success. sleep $sleeping_time.");
57     sleep( $sleeping_time );
58 }
59
60 1;
61
62 __END__
63
64 =head1 NAME
65
66 Plagger::Plugin::Publish::HatenaBookmark - Post to Hatena::Bookmark automatically
67
68 =head1 SYNOPSIS
69
70   - module: Publish::HatenaBookmark
71     config:
72       username: your-username
73       password: your-password
74       interval: 2
75       post_body: 1
76
77 =head1 DESCRIPTION
78
79 This plugin automatically posts feed updates to Hatena Bookmark
80 L<http://b.hatena.ne.jp/>. It supports automatic tagging as well. It
81 might be handy for syncronizing delicious feeds into Hatena Bookmark.
82
83 =head1 AUTHOR
84
85 fuba
86
87 =head1 SEE ALSO
88
89 L<Plagger>, L<Plagger::Plugin::Publish::Delicious>, L<XML::Atom>
90
91 =cut
Note: See TracBrowser for help on using the browser.