| 1 |
package Plagger::Plugin::Publish::Twitter; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use base qw( Plagger::Plugin ); |
|---|
| 4 |
|
|---|
| 5 |
use Encode; |
|---|
| 6 |
use Net::Twitter; |
|---|
| 7 |
use Time::HiRes qw(sleep); |
|---|
| 8 |
|
|---|
| 9 |
sub register { |
|---|
| 10 |
my($self, $context) = @_; |
|---|
| 11 |
$context->register_hook( |
|---|
| 12 |
$self, |
|---|
| 13 |
'publish.entry' => \&publish_entry, |
|---|
| 14 |
'plugin.init' => \&initialize, |
|---|
| 15 |
); |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
sub initialize { |
|---|
| 19 |
my($self, $context) = @_; |
|---|
| 20 |
my %opt = ( |
|---|
| 21 |
username => $self->conf->{username}, |
|---|
| 22 |
password => $self->conf->{password}, |
|---|
| 23 |
); |
|---|
| 24 |
for my $key (qw/ apihost apiurl apirealm/) { |
|---|
| 25 |
$opt{$key} = $self->conf->{$key} if $self->conf->{$key}; |
|---|
| 26 |
} |
|---|
| 27 |
$self->{twitter} = Net::Twitter->new(%opt); |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
sub publish_entry { |
|---|
| 31 |
my($self, $context, $args) = @_; |
|---|
| 32 |
my $body = $args->{entry}->body_text; |
|---|
| 33 |
|
|---|
| 34 |
if ($self->conf->{templatize}) { |
|---|
| 35 |
$body = $self->templatize('twitter.tt', $args); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
if ( length($body) > 159 ) { |
|---|
| 40 |
$body = substr($body, 0, 159); |
|---|
| 41 |
} |
|---|
| 42 |
$context->log(info => "Updating Twitter status to '$body'"); |
|---|
| 43 |
$self->{twitter}->update( encode_utf8($body) ) or $context->error("Can't update twitter status"); |
|---|
| 44 |
|
|---|
| 45 |
my $sleeping_time = $self->conf->{interval} || 15; |
|---|
| 46 |
$context->log(info => "sleep $sleeping_time."); |
|---|
| 47 |
sleep( $sleeping_time ); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
1; |
|---|
| 51 |
__END__ |
|---|
| 52 |
|
|---|
| 53 |
=head1 NAME |
|---|
| 54 |
|
|---|
| 55 |
Plagger::Plugin::Publish::Twitter - Update your status with feeds |
|---|
| 56 |
|
|---|
| 57 |
=head1 SYNOPSIS |
|---|
| 58 |
|
|---|
| 59 |
- module: Publish::Twitter |
|---|
| 60 |
config: |
|---|
| 61 |
username: twitter-id |
|---|
| 62 |
password: twitter-password |
|---|
| 63 |
|
|---|
| 64 |
=head1 DESCRIPTION |
|---|
| 65 |
|
|---|
| 66 |
This plugin sends feed entries summary to your Twitter account status. |
|---|
| 67 |
|
|---|
| 68 |
=head1 CONFIG |
|---|
| 69 |
|
|---|
| 70 |
=over 4 |
|---|
| 71 |
|
|---|
| 72 |
=item username |
|---|
| 73 |
|
|---|
| 74 |
Twitter username. Required. |
|---|
| 75 |
|
|---|
| 76 |
=item password |
|---|
| 77 |
|
|---|
| 78 |
Twitter password. Required. |
|---|
| 79 |
|
|---|
| 80 |
=item interval |
|---|
| 81 |
|
|---|
| 82 |
Optional. |
|---|
| 83 |
|
|---|
| 84 |
=item apiurl |
|---|
| 85 |
|
|---|
| 86 |
OPTIONAL. The URL of the API for twitter.com. This defaults to "http://twitter.com/statuses" if not set. |
|---|
| 87 |
|
|---|
| 88 |
=item apihost |
|---|
| 89 |
|
|---|
| 90 |
=item apirealm |
|---|
| 91 |
|
|---|
| 92 |
Optional. |
|---|
| 93 |
If you do point to a different URL, you will also need to set "apihost" and "apirealm" so that the internal LWP can authenticate. |
|---|
| 94 |
|
|---|
| 95 |
"apihost" defaults to "www.twitter.com:80". |
|---|
| 96 |
"apirealm" defaults to "Twitter API". |
|---|
| 97 |
|
|---|
| 98 |
=item templatize |
|---|
| 99 |
Optional. |
|---|
| 100 |
A flag to use Template-Toolkit to message formatting. Defaults to 0. |
|---|
| 101 |
|
|---|
| 102 |
=back |
|---|
| 103 |
|
|---|
| 104 |
=head1 AUTHOR |
|---|
| 105 |
|
|---|
| 106 |
Tatsuhiko Miyagawa |
|---|
| 107 |
|
|---|
| 108 |
=head1 SEE ALSO |
|---|
| 109 |
|
|---|
| 110 |
L<Plagger>, L<Net::Twitter> |
|---|
| 111 |
|
|---|
| 112 |
=cut |
|---|