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"
109 $stat->{$tweets->{$id}{type}} = $id;
113 DEBUG or sleep($conf->{sleep});
115 # do retweet found tweets
118 DEBUG or $res = $bot->retweet($id);
119 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
123 warn "status_id => $id\n";
127 $stat->{$tweets->{$id}{type}} = $id;
130 if ($conf->{mail}{body}) {
131 my $body = encode("iso-2022-jp", $conf->{mail}{body});
134 Smtp => $conf->{mail}{server},
135 From => $conf->{mail}{from},
136 To => join(", ", @{ $conf->{mail}{to} }),
137 Subject => $conf->{mail}{subject},
138 "Content-Type" => $conf->{mail}{contenttype},
141 DEBUG and warn "sending mail => ", Dumper(\%mail);
143 DEBUG or sendmail(%mail) or warn "Error sending mail: $Mail::Sendmail::error\n";
147 # save last status to yaml file
148 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
149 DEBUG and warn "status.yml => ", Dumper($stat);
154 # load configration data from yaml formatted file
155 # param => scalar string of filename
156 # ret => hash object of yaml data
160 my $yaml = YAML::Tiny->read($file);
163 warn "$0: '$file' $!\n";
166 DEBUG and warn "'$file' => ", Dumper($yaml);
172 # make Net::Twitter::Lite object and login
173 # param => hash object of configration
174 # ret => Net::Twitter::Lite object
178 my $bot = Net::Twitter::Lite->new(
179 consumer_key => $conf->{consumer_key},
180 consumer_secret => $conf->{consumer_secret},
183 $bot->access_token($conf->{access_token});
184 $bot->access_token_secret($conf->{access_token_secret});
190 # search tweets containing keywords
191 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
192 # ret => HashRef of status_id (timeline order is destroyed)
193 # or undef (none is found)
196 my $keywords = shift @_;
197 my $since_id = shift @_ || 1;
200 foreach my $word (@$keywords) {
208 DEBUG and warn "searching '$key'";
217 since_id => $since_id,
221 VERBOSE and warn Dumper($res);
222 if ($res->{results}) {
223 foreach my $tweet (@{$res->{results}}) {
224 my $res = $bot->show_status($tweet->{id});
225 VERBOSE and warn Dumper($res);
228 date => str2time($res->{created_at}),
229 screen_name => $res->{user}{screen_name},
230 status_id => $res->{id},
231 text => $res->{text},
232 user_id => $res->{user}{id},
234 if ($res->{retweeted_status}) {
235 $id->{retweet_of} = $res->{retweeted_status}{id};
236 $id->{type} = 'retweet';
239 $id->{type} = 'search';
241 $ids->{$tweet->{id}} = $id;
249 DEBUG and warn "search result => ", Dumper($ids);
254 # return status_ids mentioned to me
255 # param => Net::Twitter::Lite object, since_id
256 # ret => HashRef of status_id (timeline order is destroyed)
257 # or undef (none is found)
260 my $since_id = shift @_ || 1;
264 $res = $bot->mentions(
266 since_id => $since_id,
269 VERBOSE and warn Dumper($res);
276 if ($res && @{$res}) {
280 date => str2time($_->{created_at}),
281 screen_name => $_->{user}{screen_name},
282 status_id => $_->{id},
285 user_id => $_->{user}{id},
291 DEBUG and warn "mentions result => ", Dumper($ids);
296 # output error message at eval error
298 use Scalar::Util qw(blessed);
300 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
302 if ($@->twitter_error) {
303 my %twitter_error = %{$@->twitter_error};
305 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
306 delete $twitter_error{$_}
307 } keys %twitter_error;
308 warn join("", %twitter_error);