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);
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);
}
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($@);
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);
}
my $bot = shift @_;
my $keywords = shift @_;
- my $since_id = shift @_;
+ my $since_id = shift @_ || 1;
my $key = "";
foreach my $word (@$keywords) {
$key = $word;
}
}
+ DEBUG and warn "searching '$key'";
my $res;
my $ids = {};
);
}
if ($res->{results}) {
+ VERBOSE and warn Dumper($res);
foreach my $tweet (@{$res->{results}}) {
my $res = $bot->show_status($tweet->{id});
if ($res->{retweeted_status}) {
else {
$ids->{$tweet->{id}} = 'search';
}
+ VERBOSE and warn Dumper($res);
}
}
};
evalrescue($@);
}
+ DEBUG and warn "search result => ", Dumper($ids);
return $ids;
}
# or undef (none is found)
my $bot = shift @_;
- my $since_id = shift @_;
+ my $since_id = shift @_ || 1;
my $res;
eval {
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;
}