8 # When Net::Twitter::Lite encounters a Twitter API error or a network error,
9 # it throws a Net::Twitter::Lite::Error object.
10 # You can catch and process these exceptions by using eval blocks and testing $@
11 ## from http://search.cpan.org/perldoc?Net::Twitter::Lite#ERROR_HANDLING
12 use Net::Twitter::Lite;
16 sub VERBOSE () { $ARGV[0] eq 'verbose' };
17 sub DEBUG () { VERBOSE or $ARGV[0] eq 'debug' };
20 DEBUG and warn "$0: debug mode";
22 my $conf = loadconf("$Bin/config.yml");
23 if (! defined $conf) {
24 die "$0: cannot parse config file.\n";
26 my $stat = loadconf("$Bin/status.yml");
27 if (! defined $stat) {
31 my $bot = login($conf);
32 if (! $bot->authorized) {
33 die "$0: this client is not yet authorized.\n";
39 $tweet = or_search($bot, $conf->{hashtag}, $stat->{search});
41 %$tweets = (%$tweets, %$tweet);
44 $tweet = mentions_ids($bot, $stat->{mention});
46 %$tweets = (%$tweets, %$tweet);
49 foreach my $id (sort keys %$tweets) {
50 # $tweets->{$id}{type} eq 'search' => found by search API
51 # eq 'mention' => found by mention API
52 if ($tweets->{$id}{type} eq 'retweet') {
55 DEBUG or sleep($conf->{sleep});
57 # do retweet found tweets
60 DEBUG or $res = $bot->retweet($id);
61 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
65 warn "status_id => $id\n";
69 $stat->{$tweets->{$id}{type}} = $id;
73 # save last status to yaml file
74 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
75 DEBUG and warn "status.yml => ", Dumper($stat);
80 # load configration data from yaml formatted file
81 # param => scalar string of filename
82 # ret => hash object of yaml data
86 my $yaml = YAML::Tiny->read($file);
89 warn "$0: '$file' $!\n";
96 # make Net::Twitter::Lite object and login
97 # param => hash object of configration
98 # ret => Net::Twitter::Lite object
102 my $bot = Net::Twitter::Lite->new(
103 consumer_key => $conf->{consumer_key},
104 consumer_secret => $conf->{consumer_secret},
107 $bot->access_token($conf->{access_token});
108 $bot->access_token_secret($conf->{access_token_secret});
114 # search tweets containing keywords
115 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
116 # ret => HashRef of status_id (timeline order is destroyed)
117 # or undef (none is found)
120 my $keywords = shift @_;
121 my $since_id = shift @_ || 1;
124 foreach my $word (@$keywords) {
132 DEBUG and warn "searching '$key'";
141 since_id => $since_id,
145 if ($res->{results}) {
146 VERBOSE and warn Dumper($res);
147 foreach my $tweet (@{$res->{results}}) {
148 my $res = $bot->show_status($tweet->{id});
149 if ($res->{retweeted_status}) {
150 $ids->{$tweet->{id}}{type} = 'retweet';
153 $ids->{$tweet->{id}}{type} = 'search';
155 VERBOSE and warn Dumper($res);
163 DEBUG and warn "search result => ", Dumper($ids);
168 # return status_ids mentioned to me
169 # param => Net::Twitter::Lite object, since_id
170 # ret => HashRef of status_id (timeline order is destroyed)
171 # or undef (none is found)
174 my $since_id = shift @_ || 1;
178 $res = $bot->mentions(
180 since_id => $since_id,
183 VERBOSE and warn Dumper($res);
190 if ($res && @{$res}) {
200 DEBUG and warn "mentions result => ", Dumper($ids);
205 # output error message at eval error
207 use Scalar::Util qw(blessed);
209 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
211 if ($@->twitter_error) {
212 my %twitter_error = %{$@->twitter_error};
214 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
215 delete $twitter_error{$_}
216 } keys %twitter_error;
217 warn join("", %twitter_error);