Jaipo/Service/Twitter.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 145 CC 7 SCAR 16.7
CC
7
Cov
0%
init
12.0
send_msg
12.0
layout_message
6.0
CRAP 5.3
1 package Jaipo::Service::Twitter;
2 use warnings;
3 use strict;
4 use Net::Twitter;
5 use base qw/Jaipo::Service Class::Accessor::Fast/;
6 use Text::Table;
7 use App::Cache;
8 use Lingua::ZH::Wrap 'wrap';
9 use utf8;
10  
11 =head2 Jaipo::UI::Console
12  
13 =head3 Twitter Service Command
14  
15 =item ?
16  
17 help message
18  
19 =item rd
20  
21 read direct messages
22  
23 =item rr
24  
25 read reply messages
26  
27 =item rp
28  
29 read public messages
30  
31 =item rf
32  
33 read followed timeline messages
34  
35 =item sd [ID] [MESSAGE]
36  
37 send direct message
38  
39 =item sr [ID] [MESSAGE]
40  
41 send reply message
42  
43 =item w [ID]
44  
45 check someone's profile
46  
47 =item l [LOCATION]
48  
49 LOCATION is a string, which will be updated to your profile
50  
51 =item f [ID]
52  
53 follow someone
54  
55 =item show friends
56  
57 show friends
58  
59 =item show followers
60  
61 show followers
62  
63 =cut
64  
65 sub init {
66 my $self = shift;
67 my $caller = shift;
68 my $opt = $self->options;
69  
70 unless( $opt->{username} and $opt->{access_token} and $opt->{access_token_secret} ) {
71  
72 # request to setup parameter
73 # XXX TODO: simplify this, let it like jifty dbi schema or
74 # something
75 $caller->setup_service ( {
76 package_name => __PACKAGE__,
77 require_args => [
78 {
79 username => {
80 label => 'Username',
81 description => '',
82 type => 'text'
83 }
84 },
85 { access_token => {
86 label => 'OAuth_Token',
87 description => '',
88 type => 'text'
89 }
90 },
91 { access_token_secret => {
92 label => 'OAuth_Token_Secret',
93 description => '',
94 type => 'text'
95 }
96 },
97 ]
98 },
99 $opt
100 );
101 }
102  
103 Jaipo->config->set_service_option( 'Twitter' , $opt );
104 my %twitter_opt = %{ $opt };
105  
106 # default options
107 $twitter_opt{useragent} = 'jaipopm';
108 $twitter_opt{source} = 'jaipopm';
109 $twitter_opt{clienturl} = 'http://jaipo.org/';
110 $twitter_opt{clientver} = '0.001';
111 $twitter_opt{clientname} = 'Jaipo.pm';
112 $twitter_opt{consumer_key} = 'C5ruz1vl9H5iUYgM9ptbpg';
113 $twitter_opt{consumer_secret} = 'qEUKQJWq0OwCcXgEcu6ZC9bCbOAyPeR0pNeUihyc';
114 $twitter_opt{traits} = [qw/API::RESTv1_1/];
115
116 delete $twitter_opt{username}; # FIXME: with this, Net::Twitter will use Basic Auth
117  
118 my $twitter = Net::Twitter->new( %twitter_opt );
119  
120 unless( $twitter ) {
121 # XXX: need to implement logger: Jaipo->log->warn( );
122 print "twitter init failed\n";
123 }
124  
125 print "Twitter Service: " . $opt->{username} . ' => ' . $opt->{trigger_name} . "\n";
126 $self->core( $twitter );
127 }
128  
129  
130 =head2 PRIVATE FUNCTIONS
131  
132 =cut
133  
134  
135 sub get_table {
136 my $self = shift;
137 my @cols = @_;
138 my $tb = Text::Table->new( @cols );
139 return $tb;
140 }
141  
142  
143 sub layout_message {
144 my ( $self , $lines ) = @_;
145 local $Lingua::ZH::Wrap::columns = 60;
146 local $Lingua::ZH::Wrap::overflow = 0;
147  
148 # XXX: lingua::zh::wrap url ... orz
149 my $out = "";
150 for ( @$lines ) {
151 my $source = $_->{source};
152 # $source =~ s{<a href="(.*?)">(.*?)</a>}{$2 ($1)};
153 $source =~ s{<a href="(.*?)">(.*?)</a>}{$2};
154  
155 my $text = $_->{text} ;
156 $text =~ s|(http://\S*)|\n$1\n|g ;
157 my @text_lines = split /\n/,$text;
158  
159 my $wrap_text = '';
160 for my $l ( @text_lines ) {
161 if( $l !~ m{http://} ) {
162 $wrap_text .= wrap( ' ' x 4 , ' ' x 4 , $l ) . "\n";
163 }
164 else {
165 $wrap_text .= ' ' x 4 . $l . "\n";
166 }
167 }
168  
169 $out .= qq|
170 @{[ $_->{user}->{name} ]} said:
171 $wrap_text
172 -- from $source
173 |;
174 }
175 return $out;
176  
177 # XXX: we disable text::table currently
178 # force Message column to be 60 chars width or more.
179 # my $tb = $self->get_table( qw|User Source Message| );
180 # for ( @$lines ) {
181 # my $source = $_->{source};
182 # # $source =~ s{<a href="(.*?)">(.*?)</a>}{$2 ($1)};
183 # $source =~ s{<a href="(.*?)">(.*?)</a>}{$2};
184 # $tb->add( $_->{user}->{name} , $source , wrap( $_->{text} ) );
185 # }
186 # return $tb->table . "";
187 }
188  
189 sub get_cache {
190 my $cache = App::Cache->new({ ttl => 60*60*3 });
191 return $cache;
192 }
193  
194 sub filter_read_message {
195 my $lines = shift;
196 my $cache = get_cache;
197 my $new_lines = [];
198 for ( @$lines ) {
199 my $read = $cache->get('twitter_' . $_->{id} );
200 unless( $read ) {
201 push @$new_lines , $_;
202 $cache->set('twitter_' . $_->{id} , 1 );
203 }
204 }
205 return $new_lines;
206 }
207  
208  
209 =head2 SERVICE OVERRIDE FUNCTIONS
210  
211 =cut
212  
213 sub send_msg {
214 my ( $self , $message ) = @_;
215 $message =~ s/^s(end)? //;
216 print "Sending to => " if( Jaipo->config->app('Verbose') );
217 print $self->options->{trigger_name};
218 my $result = $self->core->update({ status => $message });
219 print " [Done]\n" if( Jaipo->config->app('Verbose') );
220 }
221  
222 # updates from user himself
223 sub read_user_timeline {
224 my $self = shift;
225 my $lines = $self->core->user_timeline; # XXX: give args to this method
226
227 $lines = filter_read_message( $lines );
228 Jaipo->logger->info( $self->layout_message( $lines ) );
229 return {
230 type => 'notification',
231 message => scalar @$lines . ' Updates',
232 updates => scalar @$lines
233 };
234 }
235  
236 # updates from user's friends or channel
237 sub read_public_timeline {
238 my $self = shift;
239 my $lines = $self->core->home_timeline; # XXX: give args to this method
240 $lines = filter_read_message( $lines );
241 Jaipo->logger->info( $self->layout_message( $lines ) );
242 return {
243 type => 'notification',
244 message => scalar @$lines . ' Updates',
245 updates => scalar @$lines
246 };
247 }
248  
249  
250 sub read_global_timeline {
251 my $self = shift;
252
253 print "Twitter stopped supporting public_timeline since 2012\n";
254
255 #~ my $lines = $self->core->public_timeline; # XXX: give args to this method
256
257 #~ $lines = filter_read_message( $lines );
258 #~ Jaipo->logger->info( $self->layout_message( $lines ) );
259 #~ return {
260 #~ type => 'notification',
261 #~ message => scalar @$lines . ' Updates',
262 #~ updates => scalar @$lines
263 #~ };
264 }
265  
266  
267  
268  
269 1;