Changeset 68 in lab


Ignore:
Timestamp:
Oct 5, 2010 9:54:08 PM (14 years ago)
Author:
mitty
Message:
  • remove needless hash check
  • search keywords from follower's tweets and reply to them
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/twitter-0.1/twitterbot.pl

    r65 r68  
    3737 
    3838my $tweets = {}; 
    39 my $tweet; 
    40  
    41 $tweet = or_search($bot, $conf->{hashtag}, $stat->{search}); 
    42 if ($tweet) { 
    43     %$tweets = (%$tweets, %$tweet); 
    44 } 
    45  
    46 $tweet = mentions_ids($bot, $stat->{mention}); 
    47 if ($tweet) { 
    48     %$tweets = (%$tweets, %$tweet); 
    49 } 
     39%$tweets = ( 
     40    %$tweets, 
     41    %{ or_search($bot, $conf->{hashtag}, $stat->{search}) } 
     42); 
     43%$tweets = ( 
     44    %$tweets, 
     45    %{ mentions_ids($bot, $stat->{mention}) } 
     46); 
    5047 
    5148foreach my $id (sort keys %$tweets) { 
     
    7067     
    7168    $stat->{$tweets->{$id}{type}} = $id; 
     69} 
     70 
     71foreach my $word (keys %{$conf->{response}}) { 
     72    # reply to follower's tweets containing keywords 
     73    $tweets = follower_search($bot, $word); 
     74    foreach my $id (sort keys %$tweets) { 
     75        if ($tweets->{$id}{type} eq 'retweet') { 
     76            next; 
     77        } 
     78        DEBUG or sleep($conf->{sleep}); 
     79         
     80        my $text = '@' . $tweets->{$id}{screen_name} 
     81            . " " . $conf->{response}{$word}; 
     82        my $res; 
     83        eval { 
     84            DEBUG  or $res = $bot->update( 
     85                { 
     86                    in_reply_to_status_id   => $id, 
     87                    status                  => $text, 
     88                } 
     89            ); 
     90            DEBUG and warn "update(", $text, ") => ", 
     91                Dumper($tweets->{$id}); 
     92        }; 
     93        if ($@) { 
     94            evalrescue($@); 
     95        } 
     96    } 
    7297} 
    7398 
     
    219244} 
    220245 
     246sub follower_search { 
     247    # search follower's tweets containing keywords 
     248    #   param   => Net::Twitter::Lite object, keyword string, since_id 
     249    #   ret     => HashRef of status_id (timeline order is destroyed) 
     250    #               or undef (none is found) 
     251     
     252    my $bot      = shift @_; 
     253    my $keyword  = shift @_; 
     254    my $since_id = shift @_ || 1; 
     255     
     256    my $tweets = or_search($bot, [ $keyword ], $since_id); 
     257    if (! $tweets) { 
     258        return {}; 
     259    } 
     260     
     261    my $followers; 
     262    eval { 
     263        $followers = $bot->followers_ids(); 
     264        VERBOSE and warn Dumper($followers); 
     265    }; 
     266    if ($@) { 
     267        evalrescue($@); 
     268    } 
     269     
     270    my $ids = {}; 
     271    foreach my $status_id (keys %$tweets) { 
     272        foreach my $user_id (@$followers) { 
     273            if ($tweets->{$status_id}{user_id} == $user_id) { 
     274                $ids->{$status_id} = $tweets->{$status_id}; 
     275                last; 
     276            } 
     277        } 
     278    } 
     279     
     280    DEBUG and warn "search result in followers => ", Dumper($ids); 
     281    return $ids; 
     282} 
     283 
    221284sub evalrescue { 
    222285    # output error message at eval error 
Note: See TracChangeset for help on using the changeset viewer.