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;
72 # save last status to yaml file
73 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
74 DEBUG and warn "status.yml => ", Dumper($stat);
79 # load configration data from yaml formatted file
80 # param => scalar string of filename
81 # ret => hash object of yaml data
85 my $yaml = YAML::Tiny->read($file);
88 warn "$0: '$file' $!\n";
95 # make Net::Twitter::Lite object and login
96 # param => hash object of configration
97 # ret => Net::Twitter::Lite object
101 my $bot = Net::Twitter::Lite->new(
102 consumer_key => $conf->{consumer_key},
103 consumer_secret => $conf->{consumer_secret},
106 $bot->access_token($conf->{access_token});
107 $bot->access_token_secret($conf->{access_token_secret});
113 # search tweets containing keywords
114 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
115 # ret => HashRef of status_id (timeline order is destroyed)
116 # or undef (none is found)
119 my $keywords = shift @_;
120 my $since_id = shift @_ || 1;
123 foreach my $word (@$keywords) {
131 DEBUG and warn "searching '$key'";
140 since_id => $since_id,
144 VERBOSE and warn Dumper($res);
145 if ($res->{results}) {
146 foreach my $tweet (@{$res->{results}}) {
147 my $res = $bot->show_status($tweet->{id});
148 VERBOSE and warn Dumper($res);
151 date => str2time($res->{created_at}),
152 screen_name => $res->{user}{screen_name},
153 status_id => $res->{id},
154 text => $res->{text},
155 user_id => $res->{user}{id},
157 if ($res->{retweeted_status}) {
158 $id->{retweet_of} = $res->{retweeted_status}{id};
159 $id->{type} = 'retweet';
162 $id->{type} = 'search';
164 $ids->{$tweet->{id}} = $id;
172 DEBUG and warn "search result => ", Dumper($ids);
177 # return status_ids mentioned to me
178 # param => Net::Twitter::Lite object, since_id
179 # ret => HashRef of status_id (timeline order is destroyed)
180 # or undef (none is found)
183 my $since_id = shift @_ || 1;
187 $res = $bot->mentions(
189 since_id => $since_id,
192 VERBOSE and warn Dumper($res);
199 if ($res && @{$res}) {
203 date => str2time($_->{created_at}),
204 screen_name => $_->{user}{screen_name},
205 status_id => $_->{id},
208 user_id => $_->{user}{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);