From: mitty Date: Tue, 5 Oct 2010 12:54:08 +0000 (+0000) Subject: * remove needless hash check X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=830035cf10d2f42f0d678ead2cdcaccb132eea33;p=lab.git * remove needless hash check * search keywords from follower's tweets and reply to them git-svn-id: https://lab.mitty.jp/svn/lab/branches/twitter-0.1@68 7d2118f6-f56c-43e7-95a2-4bb3031d96e7 --- diff --git a/twitterbot.pl b/twitterbot.pl index d1dd229..de7a250 100755 --- a/twitterbot.pl +++ b/twitterbot.pl @@ -36,17 +36,14 @@ if (! $bot->authorized) { } my $tweets = {}; -my $tweet; - -$tweet = or_search($bot, $conf->{hashtag}, $stat->{search}); -if ($tweet) { - %$tweets = (%$tweets, %$tweet); -} - -$tweet = mentions_ids($bot, $stat->{mention}); -if ($tweet) { - %$tweets = (%$tweets, %$tweet); -} +%$tweets = ( + %$tweets, + %{ or_search($bot, $conf->{hashtag}, $stat->{search}) } +); +%$tweets = ( + %$tweets, + %{ mentions_ids($bot, $stat->{mention}) } +); foreach my $id (sort keys %$tweets) { # $tweets->{$id}{type} eq 'search' => found by search API @@ -71,6 +68,34 @@ foreach my $id (sort keys %$tweets) { $stat->{$tweets->{$id}{type}} = $id; } +foreach my $word (keys %{$conf->{response}}) { + # reply to follower's tweets containing keywords + $tweets = follower_search($bot, $word); + foreach my $id (sort keys %$tweets) { + if ($tweets->{$id}{type} eq 'retweet') { + next; + } + DEBUG or sleep($conf->{sleep}); + + my $text = '@' . $tweets->{$id}{screen_name} + . " " . $conf->{response}{$word}; + my $res; + eval { + DEBUG or $res = $bot->update( + { + in_reply_to_status_id => $id, + status => $text, + } + ); + DEBUG and warn "update(", $text, ") => ", + Dumper($tweets->{$id}); + }; + if ($@) { + evalrescue($@); + } + } +} + if ($tweets) { # save last status to yaml file DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat); @@ -218,6 +243,44 @@ sub mentions_ids { return $ids; } +sub follower_search { + # search follower's tweets containing keywords + # param => Net::Twitter::Lite object, keyword string, since_id + # ret => HashRef of status_id (timeline order is destroyed) + # or undef (none is found) + + my $bot = shift @_; + my $keyword = shift @_; + my $since_id = shift @_ || 1; + + my $tweets = or_search($bot, [ $keyword ], $since_id); + if (! $tweets) { + return {}; + } + + my $followers; + eval { + $followers = $bot->followers_ids(); + VERBOSE and warn Dumper($followers); + }; + if ($@) { + evalrescue($@); + } + + my $ids = {}; + foreach my $status_id (keys %$tweets) { + foreach my $user_id (@$followers) { + if ($tweets->{$status_id}{user_id} == $user_id) { + $ids->{$status_id} = $tweets->{$status_id}; + last; + } + } + } + + DEBUG and warn "search result in followers => ", Dumper($ids); + return $ids; +} + sub evalrescue { # output error message at eval error