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
52 if ($tweets->{$id}{type} eq 'retweet') {
53 DEBUG and warn "skipping $id that was already retweeted\n";
56 if (defined $conf->{allow}) {
58 foreach my $screen_name ( @{ $conf->{allow}{screen_name} } ) {
59 if ($tweets->{$id}{screen_name} eq $screen_name) {
60 DEBUG and warn "$id was allowed by screen_name\n";
65 foreach my $user_id ( @{ $conf->{allow}{user_id} } ) {
66 if ($tweets->{$id}{user_id} eq $user_id) {
67 DEBUG and warn "$id was allowed by user_id\n";
78 DEBUG or sleep($conf->{sleep});
80 # do retweet found tweets
83 DEBUG or $res = $bot->retweet($id);
84 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
88 warn "status_id => $id\n";
92 $stat->{$tweets->{$id}{type}} = $id;
96 # save last status to yaml file
97 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
98 DEBUG and warn "status.yml => ", Dumper($stat);
103 # load configration data from yaml formatted file
104 # param => scalar string of filename
105 # ret => hash object of yaml data
109 my $yaml = YAML::Tiny->read($file);
112 warn "$0: '$file' $!\n";
115 DEBUG and warn "'$file' => ", Dumper($yaml);
121 # make Net::Twitter::Lite object and login
122 # param => hash object of configration
123 # ret => Net::Twitter::Lite object
127 my $bot = Net::Twitter::Lite->new(
128 consumer_key => $conf->{consumer_key},
129 consumer_secret => $conf->{consumer_secret},
132 $bot->access_token($conf->{access_token});
133 $bot->access_token_secret($conf->{access_token_secret});
139 # search tweets containing keywords
140 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
141 # ret => HashRef of status_id (timeline order is destroyed)
142 # or undef (none is found)
145 my $keywords = shift @_;
146 my $since_id = shift @_ || 1;
149 foreach my $word (@$keywords) {
157 DEBUG and warn "searching '$key'";
166 since_id => $since_id,
170 VERBOSE and warn Dumper($res);
171 if ($res->{results}) {
172 foreach my $tweet (@{$res->{results}}) {
173 my $res = $bot->show_status($tweet->{id});
174 VERBOSE and warn Dumper($res);
177 date => str2time($res->{created_at}),
178 screen_name => $res->{user}{screen_name},
179 status_id => $res->{id},
180 text => $res->{text},
181 user_id => $res->{user}{id},
183 if ($res->{retweeted_status}) {
184 $id->{retweet_of} = $res->{retweeted_status}{id};
185 $id->{type} = 'retweet';
188 $id->{type} = 'search';
190 $ids->{$tweet->{id}} = $id;
198 DEBUG and warn "search result => ", Dumper($ids);
203 # return status_ids mentioned to me
204 # param => Net::Twitter::Lite object, since_id
205 # ret => HashRef of status_id (timeline order is destroyed)
206 # or undef (none is found)
209 my $since_id = shift @_ || 1;
213 $res = $bot->mentions(
215 since_id => $since_id,
218 VERBOSE and warn Dumper($res);
225 if ($res && @{$res}) {
229 date => str2time($_->{created_at}),
230 screen_name => $_->{user}{screen_name},
231 status_id => $_->{id},
234 user_id => $_->{user}{id},
240 DEBUG and warn "mentions result => ", Dumper($ids);
245 # output error message at eval error
247 use Scalar::Util qw(blessed);
249 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
251 if ($@->twitter_error) {
252 my %twitter_error = %{$@->twitter_error};
254 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
255 delete $twitter_error{$_}
256 } keys %twitter_error;
257 warn join("", %twitter_error);