Changeset 927c186 for lib/BarnOwl/Module/Twitter.pm
- Timestamp:
- Jan 9, 2009, 2:58:18 PM (16 years ago)
- Branches:
- master, release-1.10, release-1.7, release-1.8, release-1.9
- Children:
- b4b5a7e
- Parents:
- da3596f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/BarnOwl/Module/Twitter.pm
r1f82df9 r927c186 67 67 } 68 68 ); 69 70 BarnOwl::new_variable_bool( 71 'twitter:poll', 72 { 73 default => 1, 74 summary => 'Poll Twitter for incoming messages', 75 description => "If set, will poll Twitter every minute for normal updates,\n" 76 . 'and every two minutes for direct message' 77 } 78 ); 69 79 70 80 my $conffile = BarnOwl::get_config_dir() . "/twitter"; … … 109 119 } 110 120 111 my $last_poll = 0; 112 my $last_id = undef; 121 our $last_poll = 0; 122 our $last_direct_poll = 0; 123 our $last_id = undef; 124 our $last_direct = undef; 125 113 126 unless(defined($last_id)) { 114 $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; 127 eval { 128 $last_id = $twitter->friends_timeline({count => 1})->[0]{id}; 129 }; 130 } 131 132 unless(defined($last_direct)) { 133 eval { 134 $last_direct = $twitter->direct_messages()->[0]{id}; 135 }; 115 136 } 116 137 117 138 sub poll_messages { 118 return unless ( time - $last_poll ) >= 45; 139 poll_twitter(); 140 poll_direct(); 141 } 142 143 sub twitter_error { 144 my $ratelimit = $twitter->rate_limit_status; 145 unless(defined($ratelimit)) { 146 # Twitter's just sucking, sleep for 5 minutes 147 $last_direct_poll = $last_poll = time + 60*5; 148 die("Twitter seems to be having problems.\n"); 149 } 150 if($ratelimit->{remaining_hits} <= 0) { 151 $last_direct_poll = $last_poll = $ratelimit->{reset_time_in_seconds}; 152 die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n"); 153 } 154 } 155 156 sub poll_twitter { 157 return unless ( time - $last_poll ) >= 60; 119 158 $last_poll = time; 159 return unless BarnOwl::getvar('twitter:poll') eq 'on'; 160 120 161 my $timeline = $twitter->friends_timeline( { since_id => $last_id } ); 121 162 unless(defined($timeline)) { 122 BarnOwl::error("Twitter returned error ... rate-limited?"); 123 # Sleep for 15 minutes 124 $last_poll = time + 60*15; 163 twitter_error(); 125 164 return; 126 165 }; … … 147 186 } 148 187 188 sub poll_direct { 189 return unless ( time - $last_direct_poll) >= 120; 190 $last_direct_poll = time; 191 return unless BarnOwl::getvar('twitter:poll') eq 'on'; 192 193 my $direct = $twitter->direct_messages( { since_id => $last_direct } ); 194 unless(defined($direct)) { 195 twitter_error(); 196 return; 197 }; 198 if ( scalar @$direct ) { 199 for my $tweet ( reverse @$direct ) { 200 if ( $tweet->{id} <= $last_direct ) { 201 next; 202 } 203 my $msg = BarnOwl::Message->new( 204 type => 'Twitter', 205 sender => $tweet->{sender}{screen_name}, 206 recipient => $cfg->{user} || $user, 207 direction => 'in', 208 location => decode_entities($tweet->{sender}{location}||""), 209 body => decode_entities($tweet->{text}), 210 isprivate => 'true' 211 ); 212 BarnOwl::queue_message($msg); 213 } 214 $last_direct = $direct->[0]{id}; 215 } else { 216 # BarnOwl::message("No new tweets..."); 217 } 218 } 219 149 220 sub twitter { 150 221 my $msg = shift; 222 if($msg =~ m{\Ad\s+([^\s])+(.*)}sm) { 223 twitter_direct($1, $2); 224 } elsif(defined $twitter) { 225 $twitter->update($msg); 226 } 227 } 228 229 sub twitter_direct { 230 my $who = shift; 231 my $msg = shift; 151 232 if(defined $twitter) { 152 $twitter->update($msg); 233 $twitter->new_direct_message({ 234 user => $who, 235 text => $msg 236 }); 153 237 } 154 238 } … … 159 243 description => 'Update Twitter. If MESSAGE is provided, use it as your status.' 160 244 . "\nOtherwise, prompt for a status message to use." 245 }); 246 247 BarnOwl::new_command('twitter-direct' => \&cmd_twitter_direct, { 248 summary => 'Send a Twitter direct message', 249 usage => 'twitter-direct USER', 250 description => 'Send a Twitter Direct Message to USER' 161 251 }); 162 252 … … 171 261 } 172 262 263 sub cmd_twitter_direct { 264 my $cmd = shift; 265 my $user = shift; 266 die("Usage: $cmd USER\n") unless $user; 267 BarnOwl::start_edit_win("$cmd $user", sub{twitter_direct($user, shift)}); 268 } 269 173 270 eval { 174 271 $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
Note: See TracChangeset
for help on using the changeset viewer.