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

Revision 2040 (checked in by hsbt, 12 years ago)

add flag to use Template-Toolkit.

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