[48] | 1 | #! /usr/bin/perl -w |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use utf8; |
---|
| 6 | |
---|
| 7 | ## IMPORTANT ## |
---|
| 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; |
---|
| 13 | use FindBin qw($Bin); |
---|
| 14 | use YAML::Tiny; |
---|
| 15 | |
---|
[59] | 16 | sub VERBOSE () { $ARGV[0] eq 'verbose' }; |
---|
| 17 | sub DEBUG () { VERBOSE or $ARGV[0] eq 'debug' }; |
---|
| 18 | use Data::Dumper; |
---|
| 19 | |
---|
| 20 | DEBUG and warn "$0: debug mode"; |
---|
| 21 | |
---|
[48] | 22 | my $conf = loadconf("$Bin/config.yml"); |
---|
| 23 | if (! defined $conf) { |
---|
| 24 | die "$0: cannot parse config file.\n"; |
---|
| 25 | } |
---|
| 26 | my $stat = loadconf("$Bin/status.yml"); |
---|
| 27 | if (! defined $stat) { |
---|
[52] | 28 | $stat = {}; |
---|
[48] | 29 | } |
---|
| 30 | |
---|
| 31 | my $bot = login($conf); |
---|
| 32 | if (! $bot->authorized) { |
---|
| 33 | die "$0: this client is not yet authorized.\n"; |
---|
| 34 | } |
---|
| 35 | |
---|
[61] | 36 | my $tweets = {}; |
---|
[48] | 37 | my $tweet; |
---|
| 38 | |
---|
[52] | 39 | $tweet = or_search($bot, $conf->{hashtag}, $stat->{search}); |
---|
[48] | 40 | if ($tweet) { |
---|
[61] | 41 | %$tweets = (%$tweets, %$tweet); |
---|
[48] | 42 | } |
---|
| 43 | |
---|
[52] | 44 | $tweet = mentions_ids($bot, $stat->{mention}); |
---|
[48] | 45 | if ($tweet) { |
---|
[61] | 46 | %$tweets = (%$tweets, %$tweet); |
---|
[48] | 47 | } |
---|
| 48 | |
---|
[61] | 49 | foreach my $id (sort keys %$tweets) { |
---|
| 50 | # $tweets->{$id}{type} eq 'search' => found by search API |
---|
| 51 | # eq 'mention' => found by mention API |
---|
| 52 | if ($tweets->{$id}{type} eq 'retweet') { |
---|
[48] | 53 | next; |
---|
| 54 | } |
---|
[59] | 55 | DEBUG or sleep($conf->{sleep}); |
---|
[61] | 56 | |
---|
| 57 | # do retweet found tweets |
---|
[48] | 58 | my $res; |
---|
| 59 | eval { |
---|
[59] | 60 | DEBUG or $res = $bot->retweet($id); |
---|
[61] | 61 | DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id}); |
---|
[48] | 62 | }; |
---|
| 63 | if ($@) { |
---|
| 64 | evalrescue($@); |
---|
| 65 | warn "status_id => $id\n"; |
---|
| 66 | next; |
---|
| 67 | } |
---|
| 68 | |
---|
[61] | 69 | $stat->{$tweets->{$id}{type}} = $id; |
---|
[48] | 70 | } |
---|
| 71 | |
---|
[61] | 72 | if ($tweets) { |
---|
[48] | 73 | # save last status to yaml file |
---|
[59] | 74 | DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat); |
---|
| 75 | DEBUG and warn "status.yml => ", Dumper($stat); |
---|
[48] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | sub loadconf { |
---|
| 80 | # load configration data from yaml formatted file |
---|
| 81 | # param => scalar string of filename |
---|
| 82 | # ret => hash object of yaml data |
---|
| 83 | |
---|
| 84 | my $file = shift @_; |
---|
| 85 | |
---|
| 86 | my $yaml = YAML::Tiny->read($file); |
---|
| 87 | |
---|
| 88 | if ($!) { |
---|
| 89 | warn "$0: '$file' $!\n"; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return $yaml->[0]; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | sub login { |
---|
| 96 | # make Net::Twitter::Lite object and login |
---|
| 97 | # param => hash object of configration |
---|
| 98 | # ret => Net::Twitter::Lite object |
---|
| 99 | |
---|
| 100 | my $conf = shift @_; |
---|
| 101 | |
---|
| 102 | my $bot = Net::Twitter::Lite->new( |
---|
| 103 | consumer_key => $conf->{consumer_key}, |
---|
| 104 | consumer_secret => $conf->{consumer_secret}, |
---|
| 105 | ); |
---|
| 106 | |
---|
| 107 | $bot->access_token($conf->{access_token}); |
---|
| 108 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 109 | |
---|
| 110 | return $bot; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | sub or_search { |
---|
| 114 | # search tweets containing keywords |
---|
| 115 | # param => Net::Twitter::Lite object, ArrayRef of keywords, since_id |
---|
| 116 | # ret => HashRef of status_id (timeline order is destroyed) |
---|
| 117 | # or undef (none is found) |
---|
| 118 | |
---|
| 119 | my $bot = shift @_; |
---|
| 120 | my $keywords = shift @_; |
---|
[52] | 121 | my $since_id = shift @_ || 1; |
---|
[48] | 122 | |
---|
| 123 | my $key = ""; |
---|
| 124 | foreach my $word (@$keywords) { |
---|
| 125 | if ($key) { |
---|
| 126 | $key .= " OR $word"; |
---|
| 127 | } |
---|
| 128 | else { |
---|
| 129 | $key = $word; |
---|
| 130 | } |
---|
| 131 | } |
---|
[59] | 132 | DEBUG and warn "searching '$key'"; |
---|
[48] | 133 | |
---|
| 134 | my $res; |
---|
| 135 | my $ids = {}; |
---|
| 136 | eval { |
---|
| 137 | if ($key) { |
---|
| 138 | $res = $bot->search( |
---|
| 139 | { |
---|
| 140 | q => $key, |
---|
| 141 | since_id => $since_id, |
---|
| 142 | } |
---|
| 143 | ); |
---|
| 144 | } |
---|
| 145 | if ($res->{results}) { |
---|
[60] | 146 | VERBOSE and warn Dumper($res); |
---|
[48] | 147 | foreach my $tweet (@{$res->{results}}) { |
---|
| 148 | my $res = $bot->show_status($tweet->{id}); |
---|
| 149 | if ($res->{retweeted_status}) { |
---|
[61] | 150 | $ids->{$tweet->{id}}{type} = 'retweet'; |
---|
[48] | 151 | } |
---|
| 152 | else { |
---|
[61] | 153 | $ids->{$tweet->{id}}{type} = 'search'; |
---|
[48] | 154 | } |
---|
[59] | 155 | VERBOSE and warn Dumper($res); |
---|
[48] | 156 | } |
---|
| 157 | } |
---|
| 158 | }; |
---|
| 159 | if ($@) { |
---|
| 160 | evalrescue($@); |
---|
| 161 | } |
---|
| 162 | |
---|
[59] | 163 | DEBUG and warn "search result => ", Dumper($ids); |
---|
[48] | 164 | return $ids; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | sub mentions_ids { |
---|
| 168 | # return status_ids mentioned to me |
---|
| 169 | # param => Net::Twitter::Lite object, since_id |
---|
| 170 | # ret => HashRef of status_id (timeline order is destroyed) |
---|
| 171 | # or undef (none is found) |
---|
| 172 | |
---|
| 173 | my $bot = shift @_; |
---|
[52] | 174 | my $since_id = shift @_ || 1; |
---|
[48] | 175 | |
---|
| 176 | my $res; |
---|
| 177 | eval { |
---|
| 178 | $res = $bot->mentions( |
---|
| 179 | { |
---|
| 180 | since_id => $since_id, |
---|
| 181 | } |
---|
| 182 | ); |
---|
[59] | 183 | VERBOSE and warn Dumper($res); |
---|
[48] | 184 | }; |
---|
| 185 | if ($@) { |
---|
| 186 | evalrescue($@); |
---|
| 187 | } |
---|
| 188 | |
---|
[59] | 189 | my $ids = {}; |
---|
[48] | 190 | if ($res && @{$res}) { |
---|
| 191 | $ids = { |
---|
[61] | 192 | map { |
---|
| 193 | $_->{id} => { |
---|
| 194 | type => 'mention', |
---|
| 195 | } |
---|
| 196 | } @{$res} |
---|
[48] | 197 | }; |
---|
| 198 | } |
---|
| 199 | |
---|
[59] | 200 | DEBUG and warn "mentions result => ", Dumper($ids); |
---|
[48] | 201 | return $ids; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | sub evalrescue { |
---|
| 205 | # output error message at eval error |
---|
| 206 | |
---|
| 207 | use Scalar::Util qw(blessed); |
---|
| 208 | |
---|
| 209 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 210 | warn $@->error; |
---|
| 211 | if ($@->twitter_error) { |
---|
| 212 | my %twitter_error = %{$@->twitter_error}; |
---|
| 213 | map { |
---|
| 214 | $twitter_error{"$_ => "} = $twitter_error{$_} . "\n"; |
---|
| 215 | delete $twitter_error{$_} |
---|
| 216 | } keys %twitter_error; |
---|
| 217 | warn join("", %twitter_error); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | else { |
---|
| 221 | warn $@; |
---|
| 222 | } |
---|
| 223 | } |
---|