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);
19 my $_execmode = $ARGV[0] || 0;
20 sub VERBOSE () { $_execmode eq 'verbose' };
21 sub DEBUG () { VERBOSE or $_execmode eq 'debug' };
24 DEBUG and warn "$0: debug mode";
26 my $conf = loadconf("$Bin/config.yml");
27 if (! defined $conf) {
28 die "$0: cannot parse config file.\n";
30 my $stat = loadconf("$Bin/status.yml");
31 if (! defined $stat) {
35 my $bot = login($conf);
36 if (! $bot->authorized) {
37 die "$0: this client is not yet authorized.\n";
43 %{ or_search($bot, $conf->{hashtag}, $stat->{search}) }
47 %{ mentions_ids($bot, $stat->{mention}) }
50 foreach my $id (sort keys %$tweets) {
51 # $tweets->{$id}{type} eq 'search' => found by search API
52 # eq 'mention' => found by mention API
54 if ($tweets->{$id}{type} eq 'retweet') {
55 DEBUG and warn "skipping $id that was already retweeted\n";
58 if (defined $conf->{allow}) {
60 foreach my $screen_name ( @{ $conf->{allow}{screen_name} } ) {
61 if ($tweets->{$id}{screen_name} eq $screen_name) {
62 DEBUG and warn "$id was allowed by screen_name\n";
67 foreach my $user_id ( @{ $conf->{allow}{user_id} } ) {
68 if ($tweets->{$id}{user_id} eq $user_id) {
69 DEBUG and warn "$id was allowed by user_id\n";
76 if (defined $conf->{mail}) {
78 foreach my $pickup ( @{ $conf->{mail}{pickup} } ) {
79 if ($tweets->{$id}{type} eq $pickup) {
84 if ($conf->{mail}{ignore_allowed}) {
85 if (defined $conf->{allow} and ! defined $skip) {
86 # this tweet was allowed to retweet, so that be ignored on mail
92 if (! exists($conf->{mail}{body}) ) {
94 "[localtime(date)] http://twitter.com/<screen_name>/status/<status_id> text\n" .
95 "----------------------------------------\n"
98 $conf->{mail}{body} .=
99 "[" . localtime($tweets->{$id}{date}) . "] " .
100 "http://twitter.com/" .
101 $tweets->{$id}{screen_name} . "/status/" .
102 $tweets->{$id}{status_id} . " " .
103 $tweets->{$id}{text} . "\n"
112 DEBUG or sleep($conf->{sleep});
114 # do retweet found tweets
117 DEBUG or $res = $bot->retweet($id);
118 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
122 warn "status_id => $id\n";
126 $stat->{$tweets->{$id}{type}} = $id;
129 if ($conf->{mail}{body}) {
130 my $body = encode("iso-2022-jp", $conf->{mail}{body});
133 Smtp => $conf->{mail}{server},
134 From => $conf->{mail}{from},
135 To => join(", ", @{ $conf->{mail}{to} }),
136 Subject => $conf->{mail}{subject},
137 "Content-Type" => $conf->{mail}{contenttype},
140 DEBUG and warn "sending mail => ", Dumper(\%mail);
142 DEBUG or sendmail(%mail) or warn "Error sending mail: $Mail::Sendmail::error\n";
146 # save last status to yaml file
147 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
148 DEBUG and warn "status.yml => ", Dumper($stat);
153 # load configration data from yaml formatted file
154 # param => scalar string of filename
155 # ret => hash object of yaml data
159 my $yaml = YAML::Tiny->read($file);
162 warn "$0: '$file' $!\n";
165 DEBUG and warn "'$file' => ", Dumper($yaml);
171 # make Net::Twitter::Lite object and login
172 # param => hash object of configration
173 # ret => Net::Twitter::Lite object
177 my $bot = Net::Twitter::Lite->new(
178 consumer_key => $conf->{consumer_key},
179 consumer_secret => $conf->{consumer_secret},
182 $bot->access_token($conf->{access_token});
183 $bot->access_token_secret($conf->{access_token_secret});
189 # search tweets containing keywords
190 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
191 # ret => HashRef of status_id (timeline order is destroyed)
192 # or undef (none is found)
195 my $keywords = shift @_;
196 my $since_id = shift @_ || 1;
199 foreach my $word (@$keywords) {
207 DEBUG and warn "searching '$key'";
216 since_id => $since_id,
220 VERBOSE and warn Dumper($res);
221 if ($res->{results}) {
222 foreach my $tweet (@{$res->{results}}) {
223 my $res = $bot->show_status($tweet->{id});
224 VERBOSE and warn Dumper($res);
227 date => str2time($res->{created_at}),
228 screen_name => $res->{user}{screen_name},
229 status_id => $res->{id},
230 text => $res->{text},
231 user_id => $res->{user}{id},
233 if ($res->{retweeted_status}) {
234 $id->{retweet_of} = $res->{retweeted_status}{id};
235 $id->{type} = 'retweet';
238 $id->{type} = 'search';
240 $ids->{$tweet->{id}} = $id;
248 DEBUG and warn "search result => ", Dumper($ids);
253 # return status_ids mentioned to me
254 # param => Net::Twitter::Lite object, since_id
255 # ret => HashRef of status_id (timeline order is destroyed)
256 # or undef (none is found)
259 my $since_id = shift @_ || 1;
263 $res = $bot->mentions(
265 since_id => $since_id,
268 VERBOSE and warn Dumper($res);
275 if ($res && @{$res}) {
279 date => str2time($_->{created_at}),
280 screen_name => $_->{user}{screen_name},
281 status_id => $_->{id},
284 user_id => $_->{user}{id},
290 DEBUG and warn "mentions result => ", Dumper($ids);
295 # output error message at eval error
297 use Scalar::Util qw(blessed);
299 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
301 if ($@->twitter_error) {
302 my %twitter_error = %{$@->twitter_error};
304 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
305 delete $twitter_error{$_}
306 } keys %twitter_error;
307 warn join("", %twitter_error);