X-Git-Url: http://lab.mitty.jp/git/?a=blobdiff_plain;f=twitter%2Ftwitterbot.pl;h=d93e023a697590ceaff8f88c34b271a20d1a99ea;hb=7e20eed62f390a5e39bcd48accd0e4023c0f8328;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..d93e023 --- 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); @@ -33,12 +36,12 @@ if (! $bot->authorized) { 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); } -$tweet = mentions_ids($bot, $stat->{since_id}); +$tweet = mentions_ids($bot, $stat->{mention}); if ($tweet) { %tweets = (%tweets, %$tweet); } @@ -47,13 +50,14 @@ foreach my $id (sort keys %tweets) { if ($tweets{$id} eq 'retweet') { next; } - sleep($conf->{sleep}); + DEBUG or sleep($conf->{sleep}); # retweet found tweet # $tweets->{$id} eq 'search' => found by search API # eq 'mention' => found by mention API 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 +65,13 @@ foreach my $id (sort keys %tweets) { next; } - $stat->{since_id} = $id; + $stat->{$tweets{$id}} = $id; } 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 +117,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 +128,7 @@ sub or_search { $key = $word; } } + DEBUG and warn "searching '$key'"; my $res; my $ids = {}; @@ -136,6 +142,7 @@ 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}) { @@ -144,6 +151,7 @@ sub or_search { else { $ids->{$tweet->{id}} = 'search'; } + VERBOSE and warn Dumper($res); } } }; @@ -151,6 +159,7 @@ sub or_search { evalrescue($@); } + DEBUG and warn "search result => ", Dumper($ids); return $ids; } @@ -161,7 +170,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 +179,20 @@ 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} }; } + DEBUG and warn "mentions result => ", Dumper($ids); return $ids; }