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 $tweet = or_search($bot, $conf->{hashtag}, $stat->{search});
43 %$tweets = (%$tweets, %$tweet);
46 $tweet = mentions_ids($bot, $stat->{mention});
48 %$tweets = (%$tweets, %$tweet);
51 foreach my $id (sort keys %$tweets) {
52 # $tweets->{$id}{type} eq 'search' => found by search API
53 # eq 'mention' => found by mention API
54 if ($tweets->{$id}{type} eq 'retweet') {
57 DEBUG or sleep($conf->{sleep});
59 # do retweet found tweets
62 DEBUG or $res = $bot->retweet($id);
63 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
67 warn "status_id => $id\n";
71 $stat->{$tweets->{$id}{type}} = $id;
75 # save last status to yaml file
76 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
77 DEBUG and warn "status.yml => ", Dumper($stat);
82 # load configration data from yaml formatted file
83 # param => scalar string of filename
84 # ret => hash object of yaml data
88 my $yaml = YAML::Tiny->read($file);
91 warn "$0: '$file' $!\n";
98 # make Net::Twitter::Lite object and login
99 # param => hash object of configration
100 # ret => Net::Twitter::Lite object
104 my $bot = Net::Twitter::Lite->new(
105 consumer_key => $conf->{consumer_key},
106 consumer_secret => $conf->{consumer_secret},
109 $bot->access_token($conf->{access_token});
110 $bot->access_token_secret($conf->{access_token_secret});
116 # search tweets containing keywords
117 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
118 # ret => HashRef of status_id (timeline order is destroyed)
119 # or undef (none is found)
122 my $keywords = shift @_;
123 my $since_id = shift @_ || 1;
126 foreach my $word (@$keywords) {
134 DEBUG and warn "searching '$key'";
143 since_id => $since_id,
147 VERBOSE and warn Dumper($res);
148 if ($res->{results}) {
149 foreach my $tweet (@{$res->{results}}) {
150 my $res = $bot->show_status($tweet->{id});
151 VERBOSE and warn Dumper($res);
154 date => str2time($res->{created_at}),
155 screen_name => $res->{user}{screen_name},
156 status_id => $res->{id},
157 text => $res->{text},
159 if ($res->{retweeted_status}) {
160 $id->{type} = 'retweet';
163 $id->{type} = 'search';
165 $ids->{$tweet->{id}} = $id;
173 DEBUG and warn "search result => ", Dumper($ids);
178 # return status_ids mentioned to me
179 # param => Net::Twitter::Lite object, since_id
180 # ret => HashRef of status_id (timeline order is destroyed)
181 # or undef (none is found)
184 my $since_id = shift @_ || 1;
188 $res = $bot->mentions(
190 since_id => $since_id,
193 VERBOSE and warn Dumper($res);
200 if ($res && @{$res}) {
204 date => str2time($_->{created_at}),
205 screen_name => $_->{user}{screen_name},
206 status_id => $_->{id},
214 DEBUG and warn "mentions result => ", Dumper($ids);
219 # output error message at eval error
221 use Scalar::Util qw(blessed);
223 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
225 if ($@->twitter_error) {
226 my %twitter_error = %{$@->twitter_error};
228 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
229 delete $twitter_error{$_}
230 } keys %twitter_error;
231 warn join("", %twitter_error);