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;
16 my $conf = loadconf("$Bin/config.yml");
17 if (! defined $conf) {
18 die "$0: cannot parse config file.\n";
20 my $stat = loadconf("$Bin/status.yml");
21 if (! defined $stat) {
28 my $bot = login($conf);
29 if (! $bot->authorized) {
30 die "$0: this client is not yet authorized.\n";
36 $tweet = or_search($bot, $conf->{hashtag}, $stat->{since_id});
38 %tweets = (%tweets, %$tweet);
41 $tweet = mentions_ids($bot, $stat->{since_id});
43 %tweets = (%tweets, %$tweet);
46 foreach my $id (sort keys %tweets) {
47 if ($tweets{$id} eq 'retweet') {
50 sleep($conf->{sleep});
52 # $tweets->{$id} eq 'search' => found by search API
53 # eq 'mention' => found by mention API
56 $res = $bot->retweet($id);
60 warn "status_id => $id\n";
64 $stat->{since_id} = $id;
68 # save last status to yaml file
69 YAML::Tiny::DumpFile("$Bin/status.yml", $stat);
74 # load configration data from yaml formatted file
75 # param => scalar string of filename
76 # ret => hash object of yaml data
80 my $yaml = YAML::Tiny->read($file);
83 warn "$0: '$file' $!\n";
90 # make Net::Twitter::Lite object and login
91 # param => hash object of configration
92 # ret => Net::Twitter::Lite object
96 my $bot = Net::Twitter::Lite->new(
97 consumer_key => $conf->{consumer_key},
98 consumer_secret => $conf->{consumer_secret},
101 $bot->access_token($conf->{access_token});
102 $bot->access_token_secret($conf->{access_token_secret});
108 # search tweets containing keywords
109 # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id
110 # ret => HashRef of status_id (timeline order is destroyed)
111 # or undef (none is found)
114 my $keywords = shift @_;
115 my $since_id = shift @_;
118 foreach my $word (@$keywords) {
134 since_id => $since_id,
138 if ($res->{results}) {
139 foreach my $tweet (@{$res->{results}}) {
140 my $res = $bot->show_status($tweet->{id});
141 if ($res->{retweeted_status}) {
142 $ids->{$tweet->{id}} = 'retweet';
145 $ids->{$tweet->{id}} = 'search';
158 # return status_ids mentioned to me
159 # param => Net::Twitter::Lite object, since_id
160 # ret => HashRef of status_id (timeline order is destroyed)
161 # or undef (none is found)
164 my $since_id = shift @_;
168 $res = $bot->mentions(
170 since_id => $since_id,
179 if ($res && @{$res}) {
181 map { $_->{id} => 'mention' } @{$res}
189 # output error message at eval error
191 use Scalar::Util qw(blessed);
193 if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
195 if ($@->twitter_error) {
196 my %twitter_error = %{$@->twitter_error};
198 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
199 delete $twitter_error{$_}
200 } keys %twitter_error;
201 warn join("", %twitter_error);