projects
/
lab.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
* change structure of return value from or_search, mentions_ids
[lab.git]
/
twitter
/
twitterbot.pl
diff --git
a/twitter/twitterbot.pl
b/twitter/twitterbot.pl
old mode 100644
(file)
new mode 100755
(executable)
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;
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) {
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 $bot = login($conf);
@@
-30,30
+33,32
@@
if (! $bot->authorized) {
die "$0: this client is not yet authorized.\n";
}
die "$0: this client is not yet authorized.\n";
}
-my %tweets;
+my $tweets = {};
my $tweet;
my $tweet;
-$tweet = or_search($bot, $conf->{hashtag}, $stat->{since_id});
+$tweet = or_search($bot, $conf->{hashtag}, $stat->{search});
if ($tweet) {
if ($tweet) {
- %tweets = (%tweets, %$tweet);
+ %$tweets = (%$tweets, %$tweet);
}
}
-$tweet = mentions_ids($bot, $stat->{since_id});
+$tweet = mentions_ids($bot, $stat->{mention});
if ($tweet) {
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;
}
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 {
my $res;
eval {
- $res = $bot->retweet($id);
+ DEBUG or $res = $bot->retweet($id);
+ DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
};
if ($@) {
evalrescue($@);
};
if ($@) {
evalrescue($@);
@@
-61,12
+66,13
@@
foreach my $id (sort keys %tweets) {
next;
}
next;
}
- $stat->{since_id} = $id;
+ $stat->{$tweets->{$id}{type}} = $id;
}
}
-if (%tweets) {
+if ($tweets) {
# save last status to yaml file
# 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 $bot = shift @_;
my $keywords = shift @_;
- my $since_id = shift @_;
+ my $since_id = shift @_ || 1;
my $key = "";
foreach my $word (@$keywords) {
my $key = "";
foreach my $word (@$keywords) {
@@
-123,6
+129,7
@@
sub or_search {
$key = $word;
}
}
$key = $word;
}
}
+ DEBUG and warn "searching '$key'";
my $res;
my $ids = {};
my $res;
my $ids = {};
@@
-136,14
+143,16
@@
sub or_search {
);
}
if ($res->{results}) {
);
}
if ($res->{results}) {
+ VERBOSE and warn Dumper($res);
foreach my $tweet (@{$res->{results}}) {
my $res = $bot->show_status($tweet->{id});
if ($res->{retweeted_status}) {
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 {
}
else {
- $ids->{$tweet->{id}} = 'search';
+ $ids->{$tweet->{id}}{type} = 'search';
}
}
+ VERBOSE and warn Dumper($res);
}
}
};
}
}
};
@@
-151,6
+160,7
@@
sub or_search {
evalrescue($@);
}
evalrescue($@);
}
+ DEBUG and warn "search result => ", Dumper($ids);
return $ids;
}
return $ids;
}
@@
-161,7
+171,7
@@
sub mentions_ids {
# or undef (none is found)
my $bot = shift @_;
# or undef (none is found)
my $bot = shift @_;
- my $since_id = shift @_;
+ my $since_id = shift @_ || 1;
my $res;
eval {
my $res;
eval {
@@
-170,18
+180,24
@@
sub mentions_ids {
since_id => $since_id,
}
);
since_id => $since_id,
}
);
+ VERBOSE and warn Dumper($res);
};
if ($@) {
evalrescue($@);
}
};
if ($@) {
evalrescue($@);
}
- my $ids;
+ my $ids = {};
if ($res && @{$res}) {
$ids = {
if ($res && @{$res}) {
$ids = {
- map { $_->{id} => 'mention' } @{$res}
+ map {
+ $_->{id} => {
+ type => 'mention',
+ }
+ } @{$res}
};
}
};
}
+ DEBUG and warn "mentions result => ", Dumper($ids);
return $ids;
}
return $ids;
}