| 1 | package Plagger::Plugin::Publish::MixiDiary;
|
|---|
| 2 | use strict;
|
|---|
| 3 | use base qw( Plagger::Plugin );
|
|---|
| 4 |
|
|---|
| 5 | use Encode;
|
|---|
| 6 | use Atompub::Client;
|
|---|
| 7 | use XML::Atom::Entry;
|
|---|
| 8 | use Time::HiRes qw(sleep);
|
|---|
| 9 |
|
|---|
| 10 | sub register {
|
|---|
| 11 | my($self, $context) = @_;
|
|---|
| 12 | $context->register_hook(
|
|---|
| 13 | $self,
|
|---|
| 14 | 'publish.entry' => \&publish_entry,
|
|---|
| 15 | 'plugin.init' => \&initialize,
|
|---|
| 16 | );
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | sub initialize {
|
|---|
| 20 | my($self, $context) = @_;
|
|---|
| 21 | unless ($self->{client}) {
|
|---|
| 22 | my $client = Atompub::Client->new;
|
|---|
| 23 | $client->username($self->conf->{username});
|
|---|
| 24 | $client->password($self->conf->{password});
|
|---|
| 25 | my $service = $client->getService('http://mixi.jp/atom/diary/');
|
|---|
| 26 | my @workspaces = $service->workspaces;
|
|---|
| 27 | my @collections = $workspaces[0]->collections;
|
|---|
| 28 | $self->{client} = $client;
|
|---|
| 29 | $self->{PostURI} = $collections[0]->href;
|
|---|
| 30 | }
|
|---|
| 31 | 1;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | sub publish_entry {
|
|---|
| 35 | my($self, $context, $args) = @_;
|
|---|
| 36 |
|
|---|
| 37 | $context->log(info => "updating mixi diary");
|
|---|
| 38 |
|
|---|
| 39 | my $entry = XML::Atom::Entry->new;
|
|---|
| 40 | $entry->title( encode_utf8( $args->{entry}->title ) );
|
|---|
| 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 | $entry->summary( encode_utf8( $args->{entry}->body_text ) );
|
|---|
| 48 |
|
|---|
| 49 | $self->{client}->createEntry($self->{PostURI}, $entry)
|
|---|
| 50 | or $context->log(info => "can't submit: " . $self->{client}->errstr);
|
|---|
| 51 |
|
|---|
| 52 | my $sleeping_time = $self->conf->{interval} || 3;
|
|---|
| 53 | $context->log(info => "sleep $sleeping_time.");
|
|---|
| 54 | sleep( $sleeping_time );
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | 1;
|
|---|
| 58 | __END__
|
|---|
| 59 |
|
|---|
| 60 | =head1 NAME
|
|---|
| 61 |
|
|---|
| 62 | Plagger::Plugin::Publish::MixiDiary - Update your mixi diary with feeds
|
|---|
| 63 |
|
|---|
| 64 | =head1 SYNOPSIS
|
|---|
| 65 |
|
|---|
| 66 | - module: Publish::MixiDiary
|
|---|
| 67 | config:
|
|---|
| 68 | username: mixi-id
|
|---|
| 69 | password: mixi-password
|
|---|
| 70 |
|
|---|
| 71 | =head1 DESCRIPTION
|
|---|
| 72 |
|
|---|
| 73 | This plugin sends feed entries summary to your mixi diary.
|
|---|
| 74 |
|
|---|
| 75 | =head1 CONFIG
|
|---|
| 76 |
|
|---|
| 77 | =over 4
|
|---|
| 78 |
|
|---|
| 79 | =item username
|
|---|
| 80 |
|
|---|
| 81 | mixi username. Required.
|
|---|
| 82 |
|
|---|
| 83 | =item password
|
|---|
| 84 |
|
|---|
| 85 | mixi password. Required.
|
|---|
| 86 |
|
|---|
| 87 | =item interval
|
|---|
| 88 |
|
|---|
| 89 | =back
|
|---|
| 90 |
|
|---|
| 91 | =head1 AUTHOR
|
|---|
| 92 |
|
|---|
| 93 | Yasuhiro Matsumoto
|
|---|
| 94 |
|
|---|
| 95 | =head1 SEE ALSO
|
|---|
| 96 |
|
|---|
| 97 | L<Plagger>, L<Atompub::Client>, L<XML::Atom::Entry>
|
|---|
| 98 |
|
|---|
| 99 | =cut
|
|---|