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

Revision 1944 (checked in by otsune, 1 year ago)

use template. add interval.

Line 
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
33     my $body = $self->templatize('twitter.tt', $args);
34     # TODO: FIX when Summary configurable.
35     if ( length($body) > 159 ) {
36         $body = substr($body, 0, 159);
37     }
38     $context->log(info => "Updating Twitter status to '$body'");
39     $self->{twitter}->update( encode_utf8($body) ) or $context->error("Can't update twitter status");
40
41     my $sleeping_time = $self->conf->{interval} || 15;
42     $context->log(info => "sleep $sleeping_time.");
43     sleep( $sleeping_time );
44 }
45
46 1;
47 __END__
48
49 =head1 NAME
50
51 Plagger::Plugin::Publish::Twitter - Update your status with feeds
52
53 =head1 SYNOPSIS
54
55   - module: Publish::Twitter
56     config:
57       username: twitter-id
58       password: twitter-password
59
60 =head1 DESCRIPTION
61
62 This plugin sends feed entries summary to your Twitter account status.
63
64 =head1 CONFIG
65
66 =over 4
67
68 =item username
69
70 Twitter username. Required.
71
72 =item password
73
74 Twitter password. Required.
75
76 =item interval
77
78 Optional.
79
80 =item apiurl
81
82 OPTIONAL. The URL of the API for twitter.com. This defaults to "http://twitter.com/statuses" if not set.
83
84 =item apihost
85
86 =item apirealm
87
88 Optional.
89 If you do point to a different URL, you will also need to set "apihost" and "apirealm" so that the internal LWP can authenticate.
90
91     "apihost" defaults to "www.twitter.com:80".
92     "apirealm" defaults to "Twitter API".
93
94 =back
95
96 =head1 AUTHOR
97
98 Tatsuhiko Miyagawa
99
100 =head1 SEE ALSO
101
102 L<Plagger>, L<Net::Twitter>
103
104 =cut
Note: See TracBrowser for help on using the browser.