Changeset 36546fa


Ignore:
Timestamp:
Oct 11, 2009, 10:17:51 PM (12 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.7, release-1.8, release-1.9
Children:
d69c37c
Parents:
fe184d4
git-author:
Nelson Elhage <nelhage@mit.edu> (09/16/09 23:03:38)
git-committer:
Nelson Elhage <nelhage@mit.edu> (10/11/09 22:17:51)
Message:
Replace the mainloop hook with BarnOwl's built-in timer support.
Location:
lib/BarnOwl/Module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/BarnOwl/Module/Twitter.pm

    r176434d r36546fa  
    187187}
    188188
    189 sub poll_messages {
    190     return unless @twitter_handles;
    191 
    192     my $handle = $twitter_handles[$next_service_to_poll];
    193     $next_service_to_poll = ($next_service_to_poll + 1) % scalar(@twitter_handles);
    194    
    195     $handle->poll_twitter() if $handle->{cfg}->{poll_for_tweets};
    196     $handle->poll_direct() if $handle->{cfg}->{poll_for_dms};
    197 }
    198 
    199189sub find_account {
    200190    my $name = shift;
     
    322312}
    323313
    324 eval {
    325     $BarnOwl::Hooks::receiveMessage->add("BarnOwl::Module::Twitter::handle_message");
    326     $BarnOwl::Hooks::mainLoop->add("BarnOwl::Module::Twitter::poll_messages");
    327 };
    328 if($@) {
    329     $BarnOwl::Hooks::receiveMessage->add(\&handle_message);
    330     $BarnOwl::Hooks::mainLoop->add(\&poll_messages);
    331 }
    332 
    333314BarnOwl::filter(qw{twitter type ^twitter$});
    334315
  • lib/BarnOwl/Module/Twitter/Handle.pm

    r26f9e2e r36546fa  
    5757        'cfg'  => $cfg,
    5858        'twitter' => undef,
    59         'last_poll' => 0,
    60         'last_direct_poll' => 0,
    6159        'last_id' => undef,
    6260        'last_direct' => undef,
     61        'timer'        => undef,
     62        'direct_timer' => undef
    6363    };
    6464
     
    9292    warn "$@" if $@;
    9393
     94    $self->sleep(0);
     95
    9496    return $self;
    9597}
    9698
     99=head2 sleep SECONDS
     100
     101Stop polling Twitter for SECONDS seconds.
     102
     103=cut
     104
     105sub sleep {
     106    my $self  = shift;
     107    my $delay = shift;
     108
     109    if($self->{cfg}->{poll_for_tweets}) {
     110        $self->{timer} = BarnOwl::Timer->new({
     111            after    => $delay,
     112            interval => 60,
     113            cb       => sub { $self->poll_twitter }
     114           });
     115    }
     116
     117    if($self->{cfg}->{poll_for_dms}) {
     118        $self->{direct_timer} = BarnOwl::Timer->new({
     119            after    => $delay,
     120            interval => 120,
     121            cb       => sub { $self->poll_direct }
     122           });
     123    }
     124}
     125
    97126sub twitter_error {
    98127    my $self = shift;
     
    101130    warn "$@" if $@;
    102131    unless(defined($ratelimit) && ref($ratelimit) eq 'HASH') {
    103         # Twitter's just sucking, sleep for 5 minutes
    104         $self->{last_direct_poll} = $self->{last_poll} = time + 60*5;
    105         # die("Twitter seems to be having problems.\n");
     132        # Twitter's probably just sucking, try again later.
     133        $self->sleep(5);
    106134        return;
    107135    }
     136
    108137    if(exists($ratelimit->{remaining_hits})
    109138       && $ratelimit->{remaining_hits} <= 0) {
    110         $self->{last_direct_poll} = $self->{last_poll} = $ratelimit->{reset_time_in_seconds};
     139        $self->sleep(time - $ratelimit->{reset_time_in_seconds} + 60);
    111140        die("Twitter: ratelimited until " . $ratelimit->{reset_time} . "\n");
    112141    } elsif(exists($ratelimit->{error})) {
     142        $self->sleep(60*20);
    113143        die("Twitter: ". $ratelimit->{error} . "\n");
    114         $self->{last_direct_poll} = $self->{last_poll} = time + 60*20;
    115144    }
    116145}
     
    119148    my $self = shift;
    120149
    121     return unless ( time - $self->{last_poll} ) >= 60;
    122     $self->{last_poll} = time;
    123150    return unless BarnOwl::getvar('twitter:poll') eq 'on';
     151
     152    BarnOwl::debug("Polling " . $self->{cfg}->{account_nickname});
    124153
    125154    my $timeline = eval { $self->{twitter}->friends_timeline( { since_id => $self->{last_id} } ) };
     
    172201    my $self = shift;
    173202
    174     return unless ( time - $self->{last_direct_poll}) >= 120;
    175     $self->{last_direct_poll} = time;
    176203    return unless BarnOwl::getvar('twitter:poll') eq 'on';
     204
     205    BarnOwl::debug("Polling direct for " . $self->{cfg}->{account_nickname});
    177206
    178207    my $direct = eval { $self->{twitter}->direct_messages( { since_id => $self->{last_direct} } ) };
Note: See TracChangeset for help on using the changeset viewer.