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;
15 use Date::Parse qw(str2time);
17 my $_execmode = $ARGV[0] || 0;
18 sub VERBOSE () { $_execmode eq 'verbose' };
19 sub DEBUG () { VERBOSE or $_execmode eq 'debug' };
22 DEBUG and warn "$0: debug mode";
24 my $conf = loadconf("$Bin/config.yml");
25 if (! defined $conf) {
26 die "$0: cannot parse config file.\n";
28 my $stat = loadconf("$Bin/status.yml");
29 if (! defined $stat) {
33 my $bot = login($conf);
34 if (! $bot->authorized) {
35 die "$0: this client is not yet authorized.\n";
41 %{ or_search($bot, $conf->{hashtag}, $stat->{search}) }
45 %{ mentions_ids($bot, $stat->{mention}) }
48 foreach my $id (sort keys %$tweets) {
49 # $tweets->{$id}{type} eq 'search' => found by search API
50 # eq 'mention' => found by mention API
51 if ($tweets->{$id}{type} eq 'retweet') {
54 DEBUG or sleep($conf->{sleep});
56 # do retweet found tweets
59 DEBUG or $res = $bot->retweet($id);
60 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
64 warn "status_id => $id\n";
68 $stat->{$tweets->{$id}{type}} = $id;
71 foreach my $word (keys %{$conf->{response}}) {
72 # reply to follower's tweets containing keywords
73 $tweets = follower_search($bot, $word);
74 foreach my $id (sort keys %$tweets) {
75 if ($tweets->{$id}{type} eq 'retweet') {
78 DEBUG or sleep($conf->{sleep});
80 my $text = '@' . $tweets->{$id}{screen_name}
81 . " " . $conf->{response}{$word};
84 DEBUG or $res = $bot->update(
86 in_reply_to_status_id => $id,
90 DEBUG and warn "update(", $text, ") => ",
91 Dumper($tweets->{$id});
100 # save last status to yaml file
101 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
102 DEBUG and warn "status.yml => ", Dumper($stat);
107 # load configration data from yaml formatted file
108 # param => scalar string of filename
109 # ret => hash object of yaml data
113 my $yaml = YAML::Tiny->read($file);
116 warn "$0: '$file' $!\n";
123 # make Net::Twitter::Lite object and login
124 # param => hash object of configration
125 # ret => Net::Twitter::Lite object
129 my $bot = Net::Twitter::Lite->new(
130 consumer_key => $conf->{consumer_key},
131 consumer_secret => $conf->{consumer_secret},
134 $bot->access_token($conf->{access_token});
135 $bot->access_token_secret($conf->{access_token_secret});
141 # search tweets containing keywords
142 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
143 # ret => HashRef of status_id (timeline order is destroyed)
144 # or undef (none is found)
147 my $keywords = shift @_;
148 my $since_id = shift @_ || 1;
151 foreach my $word (@$keywords) {
159 DEBUG and warn "searching '$key'";
168 since_id => $since_id,
172 VERBOSE and warn Dumper($res);
173 if ($res->{results}) {
174 foreach my $tweet (@{$res->{results}}) {
175 my $res = $bot->show_status($tweet->{id});
176 VERBOSE and warn Dumper($res);
179 date => str2time($res->{created_at}),
180 screen_name => $res->{user}{screen_name},
181 status_id => $res->{id},
182 text => $res->{text},
183 user_id => $res->{user}{id},
185 if ($res->{retweeted_status}) {
186 $id->{retweet_of} = $res->{retweeted_status}{id};
187 $id->{type} = 'retweet';
190 $id->{type} = 'search';
192 $ids->{$tweet->{id}} = $id;
200 DEBUG and warn "search result => ", Dumper($ids);
205 # return status_ids mentioned to me
206 # param => Net::Twitter::Lite object, since_id
207 # ret => HashRef of status_id (timeline order is destroyed)
208 # or undef (none is found)
211 my $since_id = shift @_ || 1;
215 $res = $bot->mentions(
217 since_id => $since_id,
220 VERBOSE and warn Dumper($res);
227 if ($res && @{$res}) {
231 date => str2time($_->{created_at}),
232 screen_name => $_->{user}{screen_name},
233 status_id => $_->{id},
236 user_id => $_->{user}{id},
242 DEBUG and warn "mentions result => ", Dumper($ids);
246 sub follower_search {
247 # search follower's tweets containing keywords
248 # param => Net::Twitter::Lite object, keyword string, since_id
249 # ret => HashRef of status_id (timeline order is destroyed)
250 # or undef (none is found)
253 my $keyword = shift @_;
254 my $since_id = shift @_ || 1;
256 my $tweets = or_search($bot, [ $keyword ], $since_id);
263 $followers = $bot->followers_ids();
264 VERBOSE and warn Dumper($followers);
271 foreach my $status_id (keys %$tweets) {
272 foreach my $user_id (@$followers) {
273 if ($tweets->{$status_id}{user_id} == $user_id) {
274 $ids->{$status_id} = $tweets->{$status_id};
280 DEBUG and warn "search result in followers => ", Dumper($ids);
285 # output error message at eval error
287 use Scalar::Util qw(blessed);
289 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
291 if ($@->twitter_error) {
292 my %twitter_error = %{$@->twitter_error};
294 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
295 delete $twitter_error{$_}
296 } keys %twitter_error;
297 warn join("", %twitter_error);