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') {
52 DEBUG and warn "skipping $id that was already retweeted\n";
55 if (defined $conf->{allow}) {
57 foreach my $screen_name ( @{ $conf->{allow}{screen_name} } ) {
58 if ($tweets->{$id}{screen_name} eq $screen_name) {
59 DEBUG and warn "$id was allowed by screen_name\n";
64 foreach my $user_id ( @{ $conf->{allow}{user_id} } ) {
65 if ($tweets->{$id}{user_id} eq $user_id) {
66 DEBUG and warn "$id was allowed by user_id\n";
76 DEBUG or sleep($conf->{sleep});
78 # do retweet found tweets
81 DEBUG or $res = $bot->retweet($id);
82 DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id});
86 warn "status_id => $id\n";
90 $stat->{$tweets->{$id}{type}} = $id;
94 # save last status to yaml file
95 DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
96 DEBUG and warn "status.yml => ", Dumper($stat);
101 # load configration data from yaml formatted file
102 # param => scalar string of filename
103 # ret => hash object of yaml data
107 my $yaml = YAML::Tiny->read($file);
110 warn "$0: '$file' $!\n";
113 DEBUG and warn "'$file' => ", Dumper($yaml);
119 # make Net::Twitter::Lite object and login
120 # param => hash object of configration
121 # ret => Net::Twitter::Lite object
125 my $bot = Net::Twitter::Lite->new(
126 consumer_key => $conf->{consumer_key},
127 consumer_secret => $conf->{consumer_secret},
130 $bot->access_token($conf->{access_token});
131 $bot->access_token_secret($conf->{access_token_secret});
137 # search tweets containing keywords
138 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
139 # ret => HashRef of status_id (timeline order is destroyed)
140 # or undef (none is found)
143 my $keywords = shift @_;
144 my $since_id = shift @_ || 1;
147 foreach my $word (@$keywords) {
155 DEBUG and warn "searching '$key'";
164 since_id => $since_id,
168 VERBOSE and warn Dumper($res);
169 if ($res->{results}) {
170 foreach my $tweet (@{$res->{results}}) {
171 my $res = $bot->show_status($tweet->{id});
172 VERBOSE and warn Dumper($res);
175 date => str2time($res->{created_at}),
176 screen_name => $res->{user}{screen_name},
177 status_id => $res->{id},
178 text => $res->{text},
179 user_id => $res->{user}{id},
181 if ($res->{retweeted_status}) {
182 $id->{retweet_of} = $res->{retweeted_status}{id};
183 $id->{type} = 'retweet';
186 $id->{type} = 'search';
188 $ids->{$tweet->{id}} = $id;
196 DEBUG and warn "search result => ", Dumper($ids);
201 # return status_ids mentioned to me
202 # param => Net::Twitter::Lite object, since_id
203 # ret => HashRef of status_id (timeline order is destroyed)
204 # or undef (none is found)
207 my $since_id = shift @_ || 1;
211 $res = $bot->mentions(
213 since_id => $since_id,
216 VERBOSE and warn Dumper($res);
223 if ($res && @{$res}) {
227 date => str2time($_->{created_at}),
228 screen_name => $_->{user}{screen_name},
229 status_id => $_->{id},
232 user_id => $_->{user}{id},
238 DEBUG and warn "mentions result => ", Dumper($ids);
243 # output error message at eval error
245 use Scalar::Util qw(blessed);
247 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
249 if ($@->twitter_error) {
250 my %twitter_error = %{$@->twitter_error};
252 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
253 delete $twitter_error{$_}
254 } keys %twitter_error;
255 warn join("", %twitter_error);