Changeset e4e1dcb for lib/BarnOwl/Module/Twitter/Handle.pm
- Timestamp:
- Dec 19, 2009, 11:41:06 PM (15 years ago)
- Branches:
- master, release-1.10, release-1.7, release-1.8, release-1.9
- Children:
- 4ae10de
- Parents:
- e4951ea (diff), 0948fa0f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/BarnOwl/Module/Twitter/Handle.pm
re4951ea re4e1dcb 15 15 16 16 use Net::Twitter::Lite; 17 BEGIN { 18 # Backwards compatibility with version of Net::Twitter::Lite that 19 # lack home_timeline. 20 if(!defined(*Net::Twitter::Lite::home_timeline{CODE})) { 21 *Net::Twitter::Lite::home_timeline = 22 \&Net::Twitter::Lite::friends_timeline; 23 } 24 } 17 25 use HTML::Entities; 18 26 … … 71 79 $self->{twitter} = Net::Twitter::Lite->new(%twitter_args); 72 80 73 my $timeline = eval { $self->{twitter}-> friends_timeline({count => 1}) };74 warn "$@ " if $@;81 my $timeline = eval { $self->{twitter}->home_timeline({count => 1}) }; 82 warn "$@\n" if $@; 75 83 76 84 if(!defined($timeline)) { … … 86 94 $self->{last_direct} = $self->{twitter}->direct_messages()->[0]{id}; 87 95 }; 88 warn "$@ " if $@;96 warn "$@\n" if $@; 89 97 $self->{last_direct} = 1 unless defined($self->{last_direct}); 90 98 … … 92 100 $self->{twitter}->{ua}->timeout(1); 93 101 }; 94 warn "$@ " if $@;102 warn "$@\n" if $@; 95 103 96 104 $self->sleep(0); … … 129 137 } 130 138 139 =head2 twitter_command COMMAND ARGS... 140 141 Call the specified method on $self->{twitter} with an extended 142 timeout. This is intended for interactive commands, with the theory 143 that if the user explicitly requested an action, it is slightly more 144 acceptable to hang the UI for a second or two than to fail just 145 because Twitter is being slightly slow. Polling commands should be 146 called normally, with the default (short) timeout, to prevent 147 background Twitter suckage from hosing the UI normally. 148 149 =cut 150 151 sub twitter_command { 152 my $self = shift; 153 my $cmd = shift; 154 155 eval { $self->{twitter}->{ua}->timeout(5); }; 156 my $result = eval { 157 $self->{twitter}->$cmd(@_); 158 }; 159 my $error = $@; 160 eval { $self->{twitter}->{ua}->timeout(1); }; 161 if ($error) { 162 die($error); 163 } 164 return $result; 165 } 166 131 167 sub twitter_error { 132 168 my $self = shift; 133 169 134 170 my $ratelimit = eval { $self->{twitter}->rate_limit_status }; 135 warn "$@ " if $@;171 warn "$@\n" if $@; 136 172 unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') { 137 173 # Twitter's probably just sucking, try again later. … … 157 193 BarnOwl::debug("Polling " . $self->{cfg}->{account_nickname}); 158 194 159 my $timeline = eval { $self->{twitter}-> friends_timeline( { since_id => $self->{last_id} } ) };160 warn "$@ " if $@;195 my $timeline = eval { $self->{twitter}->home_timeline( { since_id => $self->{last_id} } ) }; 196 warn "$@\n" if $@; 161 197 unless(defined($timeline) && ref($timeline) eq 'ARRAY') { 162 198 $self->twitter_error(); … … 166 202 if ($self->{cfg}->{show_mentions}) { 167 203 my $mentions = eval { $self->{twitter}->mentions( { since_id => $self->{last_id} } ) }; 168 warn "$@ " if $@;204 warn "$@\n" if $@; 169 205 unless (defined($mentions) && ref($mentions) eq 'ARRAY') { 170 206 $self->twitter_error(); … … 211 247 212 248 my $direct = eval { $self->{twitter}->direct_messages( { since_id => $self->{last_direct} } ) }; 213 warn "$@ " if $@;249 warn "$@\n" if $@; 214 250 unless(defined($direct) && ref($direct) eq 'ARRAY') { 215 251 $self->twitter_error(); … … 228 264 location => decode_entities($tweet->{sender}{location}||""), 229 265 body => decode_entities($tweet->{text}), 230 isprivate => 'true',266 private => 'true', 231 267 service => $self->{cfg}->{service}, 232 268 account => $self->{cfg}->{account_nickname}, … … 249 285 $self->twitter_direct($1, $2); 250 286 } elsif(defined $self->{twitter}) { 251 if(defined($reply_to)) { 252 $self->{twitter}->update({ 253 status => $msg, 254 in_reply_to_status_id => $reply_to 255 }); 256 } else { 257 $self->{twitter}->update($msg); 287 if(length($msg) > 140) { 288 die("Twitter: Message over 140 characters long.\n"); 258 289 } 290 $self->twitter_command('update', { 291 status => $msg, 292 defined($reply_to) ? (in_reply_to_status_id => $reply_to) : () 293 }); 259 294 } 260 295 } … … 266 301 my $msg = shift; 267 302 if(defined $self->{twitter}) { 268 $self-> {twitter}->new_direct_message({303 $self->twitter_command('new_direct_message', { 269 304 user => $who, 270 305 text => $msg … … 277 312 direction => 'out', 278 313 body => $msg, 279 isprivate => 'true',314 private => 'true', 280 315 service => $self->{cfg}->{service}, 281 316 ); … … 298 333 } 299 334 335 sub twitter_retweet { 336 my $self = shift; 337 my $msg = shift; 338 339 if($msg->service ne $self->{cfg}->{service}) { 340 die("Cannot retweet a message from a different service.\n"); 341 } 342 $self->twitter_command(retweet => $msg->{status_id}); 343 } 344 300 345 sub twitter_follow { 301 346 my $self = shift; … … 303 348 my $who = shift; 304 349 305 my $user = $self-> {twitter}->create_friend($who);350 my $user = $self->twitter_command('create_friend', $who); 306 351 # returns a string on error 307 352 if (defined $user && !ref $user) { … … 317 362 my $who = shift; 318 363 319 my $user = $self-> {twitter}->destroy_friend($who);364 my $user = $self->twitter_command('destroy_friend', $who); 320 365 # returns a string on error 321 366 if (defined $user && !ref $user) {
Note: See TracChangeset
for help on using the changeset viewer.