X-Git-Url: http://lab.mitty.jp/git/?a=blobdiff_plain;f=twitter%2Ftwitterbot.pl;h=249c1bc58e0b85ad5e0ef0add7166293d828a626;hb=d732ebcd10157829c6bf19307369cb1b69b6665c;hp=e4d1b62f29690a0feacffc3dd3ca87d254af0924;hpb=5ce2dafff83681702c4c8b2cdd5d1264668aab2b;p=lab.git diff --git a/twitter/twitterbot.pl b/twitter/twitterbot.pl old mode 100644 new mode 100755 index e4d1b62..249c1bc --- a/twitter/twitterbot.pl +++ b/twitter/twitterbot.pl @@ -13,16 +13,19 @@ use Net::Twitter::Lite; use FindBin qw($Bin); use YAML::Tiny; +sub VERBOSE () { $ARGV[0] eq 'verbose' }; +sub DEBUG () { VERBOSE or $ARGV[0] eq 'debug' }; +use Data::Dumper; + +DEBUG and warn "$0: debug mode"; + my $conf = loadconf("$Bin/config.yml"); if (! defined $conf) { die "$0: cannot parse config file.\n"; } my $stat = loadconf("$Bin/status.yml"); if (! defined $stat) { - $stat = { - # do not set to 0 - since_id => 1, - }; + $stat = {}; } my $bot = login($conf); @@ -30,30 +33,32 @@ if (! $bot->authorized) { die "$0: this client is not yet authorized.\n"; } -my %tweets; +my $tweets = {}; my $tweet; -$tweet = or_search($bot, $conf->{hashtag}, $stat->{since_id}); +$tweet = or_search($bot, $conf->{hashtag}, $stat->{search}); if ($tweet) { - %tweets = (%tweets, %$tweet); + %$tweets = (%$tweets, %$tweet); } -$tweet = mentions_ids($bot, $stat->{since_id}); +$tweet = mentions_ids($bot, $stat->{mention}); if ($tweet) { - %tweets = (%tweets, %$tweet); + %$tweets = (%$tweets, %$tweet); } -foreach my $id (sort keys %tweets) { - if ($tweets{$id} eq 'retweet') { +foreach my $id (sort keys %$tweets) { + # $tweets->{$id}{type} eq 'search' => found by search API + # eq 'mention' => found by mention API + if ($tweets->{$id}{type} eq 'retweet') { next; } - sleep($conf->{sleep}); - # retweet found tweet - # $tweets->{$id} eq 'search' => found by search API - # eq 'mention' => found by mention API + DEBUG or sleep($conf->{sleep}); + + # do retweet found tweets my $res; eval { - $res = $bot->retweet($id); + DEBUG or $res = $bot->retweet($id); + DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id}); }; if ($@) { evalrescue($@); @@ -61,12 +66,13 @@ foreach my $id (sort keys %tweets) { next; } - $stat->{since_id} = $id; + $stat->{$tweets->{$id}{type}} = $id; } -if (%tweets) { +if ($tweets) { # save last status to yaml file - YAML::Tiny::DumpFile("$Bin/status.yml", $stat); + DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat); + DEBUG and warn "status.yml => ", Dumper($stat); } @@ -112,7 +118,7 @@ sub or_search { my $bot = shift @_; my $keywords = shift @_; - my $since_id = shift @_; + my $since_id = shift @_ || 1; my $key = ""; foreach my $word (@$keywords) { @@ -123,6 +129,7 @@ sub or_search { $key = $word; } } + DEBUG and warn "searching '$key'"; my $res; my $ids = {}; @@ -136,14 +143,16 @@ sub or_search { ); } if ($res->{results}) { + VERBOSE and warn Dumper($res); foreach my $tweet (@{$res->{results}}) { my $res = $bot->show_status($tweet->{id}); if ($res->{retweeted_status}) { - $ids->{$tweet->{id}} = 'retweet'; + $ids->{$tweet->{id}}{type} = 'retweet'; } else { - $ids->{$tweet->{id}} = 'search'; + $ids->{$tweet->{id}}{type} = 'search'; } + VERBOSE and warn Dumper($res); } } }; @@ -151,6 +160,7 @@ sub or_search { evalrescue($@); } + DEBUG and warn "search result => ", Dumper($ids); return $ids; } @@ -161,7 +171,7 @@ sub mentions_ids { # or undef (none is found) my $bot = shift @_; - my $since_id = shift @_; + my $since_id = shift @_ || 1; my $res; eval { @@ -170,18 +180,24 @@ sub mentions_ids { since_id => $since_id, } ); + VERBOSE and warn Dumper($res); }; if ($@) { evalrescue($@); } - my $ids; + my $ids = {}; if ($res && @{$res}) { $ids = { - map { $_->{id} => 'mention' } @{$res} + map { + $_->{id} => { + type => 'mention', + } + } @{$res} }; } + DEBUG and warn "mentions result => ", Dumper($ids); return $ids; }