[5ce2daf] | 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 |
---|
[e960958] | 12 | use Net::Twitter::Lite::WithAPIv1_1; |
---|
[5ce2daf] | 13 | use FindBin qw($Bin); |
---|
| 14 | use YAML::Tiny; |
---|
[139622c] | 15 | use Date::Parse qw(str2time); |
---|
[45a20c3] | 16 | use Mail::Sendmail; |
---|
| 17 | use Encode; |
---|
[5ce2daf] | 18 | |
---|
[f3ef4f6] | 19 | my $_execmode = $ARGV[0] || 0; |
---|
| 20 | sub VERBOSE () { $_execmode eq 'verbose' }; |
---|
| 21 | sub DEBUG () { VERBOSE or $_execmode eq 'debug' }; |
---|
[f72bb76] | 22 | use Data::Dumper; |
---|
| 23 | |
---|
| 24 | DEBUG and warn "$0: debug mode"; |
---|
| 25 | |
---|
[5ce2daf] | 26 | my $conf = loadconf("$Bin/config.yml"); |
---|
| 27 | if (! defined $conf) { |
---|
| 28 | die "$0: cannot parse config file.\n"; |
---|
| 29 | } |
---|
| 30 | my $stat = loadconf("$Bin/status.yml"); |
---|
| 31 | if (! defined $stat) { |
---|
[35889d4] | 32 | $stat = {}; |
---|
[5ce2daf] | 33 | } |
---|
| 34 | |
---|
| 35 | my $bot = login($conf); |
---|
| 36 | if (! $bot->authorized) { |
---|
| 37 | die "$0: this client is not yet authorized.\n"; |
---|
| 38 | } |
---|
| 39 | |
---|
[ac68e58] | 40 | my $tweets = {}; |
---|
[e0c0679] | 41 | %$tweets = ( |
---|
| 42 | %$tweets, |
---|
| 43 | %{ or_search($bot, $conf->{hashtag}, $stat->{search}) } |
---|
| 44 | ); |
---|
| 45 | %$tweets = ( |
---|
| 46 | %$tweets, |
---|
| 47 | %{ mentions_ids($bot, $stat->{mention}) } |
---|
| 48 | ); |
---|
[5ce2daf] | 49 | |
---|
[ac68e58] | 50 | foreach my $id (sort keys %$tweets) { |
---|
| 51 | # $tweets->{$id}{type} eq 'search' => found by search API |
---|
| 52 | # eq 'mention' => found by mention API |
---|
[702399e] | 53 | my $skip; |
---|
[ac68e58] | 54 | if ($tweets->{$id}{type} eq 'retweet') { |
---|
[641934f] | 55 | DEBUG and warn "skipping $id that was already retweeted\n"; |
---|
[45a20c3] | 56 | $skip = 'retweet'; |
---|
[5ce2daf] | 57 | } |
---|
[641934f] | 58 | if (defined $conf->{allow}) { |
---|
[45a20c3] | 59 | $skip = 'allow'; |
---|
[641934f] | 60 | foreach my $screen_name ( @{ $conf->{allow}{screen_name} } ) { |
---|
| 61 | if ($tweets->{$id}{screen_name} eq $screen_name) { |
---|
| 62 | DEBUG and warn "$id was allowed by screen_name\n"; |
---|
[702399e] | 63 | undef $skip; |
---|
[641934f] | 64 | last; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | foreach my $user_id ( @{ $conf->{allow}{user_id} } ) { |
---|
| 68 | if ($tweets->{$id}{user_id} eq $user_id) { |
---|
| 69 | DEBUG and warn "$id was allowed by user_id\n"; |
---|
[702399e] | 70 | undef $skip; |
---|
[641934f] | 71 | last; |
---|
| 72 | } |
---|
| 73 | } |
---|
[702399e] | 74 | } |
---|
| 75 | |
---|
[45a20c3] | 76 | if (defined $conf->{mail}) { |
---|
| 77 | my $send; |
---|
| 78 | foreach my $pickup ( @{ $conf->{mail}{pickup} } ) { |
---|
| 79 | if ($tweets->{$id}{type} eq $pickup) { |
---|
| 80 | $send = 1; |
---|
| 81 | last; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | if ($conf->{mail}{ignore_allowed}) { |
---|
| 85 | if (defined $conf->{allow} and ! defined $skip) { |
---|
| 86 | # this tweet was allowed to retweet, so that be ignored on mail |
---|
| 87 | undef $send; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if ($send) { |
---|
| 92 | if (! exists($conf->{mail}{body}) ) { |
---|
| 93 | $conf->{mail}{body} = |
---|
| 94 | "[localtime(date)] http://twitter.com/<screen_name>/status/<status_id> text\n" . |
---|
| 95 | "----------------------------------------\n" |
---|
| 96 | ; |
---|
| 97 | } |
---|
| 98 | $conf->{mail}{body} .= |
---|
| 99 | "[" . localtime($tweets->{$id}{date}) . "] " . |
---|
| 100 | "http://twitter.com/" . |
---|
| 101 | $tweets->{$id}{screen_name} . "/status/" . |
---|
| 102 | $tweets->{$id}{status_id} . " " . |
---|
| 103 | $tweets->{$id}{text} . "\n" |
---|
| 104 | ; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
[702399e] | 108 | if ($skip) { |
---|
[24fa584] | 109 | $stat->{$tweets->{$id}{type}} = $id; |
---|
[702399e] | 110 | next; |
---|
[641934f] | 111 | } |
---|
| 112 | |
---|
[f72bb76] | 113 | DEBUG or sleep($conf->{sleep}); |
---|
[ac68e58] | 114 | |
---|
| 115 | # do retweet found tweets |
---|
[5ce2daf] | 116 | my $res; |
---|
| 117 | eval { |
---|
[f72bb76] | 118 | DEBUG or $res = $bot->retweet($id); |
---|
[ac68e58] | 119 | DEBUG and warn "retweet($id) => ", Dumper($tweets->{$id}); |
---|
[5ce2daf] | 120 | }; |
---|
| 121 | if ($@) { |
---|
| 122 | evalrescue($@); |
---|
| 123 | warn "status_id => $id\n"; |
---|
| 124 | next; |
---|
| 125 | } |
---|
| 126 | |
---|
[ac68e58] | 127 | $stat->{$tweets->{$id}{type}} = $id; |
---|
[5ce2daf] | 128 | } |
---|
| 129 | |
---|
[45a20c3] | 130 | if ($conf->{mail}{body}) { |
---|
| 131 | my $body = encode("iso-2022-jp", $conf->{mail}{body}); |
---|
| 132 | |
---|
| 133 | my %mail = ( |
---|
| 134 | Smtp => $conf->{mail}{server}, |
---|
| 135 | From => $conf->{mail}{from}, |
---|
| 136 | To => join(", ", @{ $conf->{mail}{to} }), |
---|
| 137 | Subject => $conf->{mail}{subject}, |
---|
| 138 | "Content-Type" => $conf->{mail}{contenttype}, |
---|
| 139 | Body => $body, |
---|
| 140 | ); |
---|
| 141 | DEBUG and warn "sending mail => ", Dumper(\%mail); |
---|
| 142 | |
---|
| 143 | DEBUG or sendmail(%mail) or warn "Error sending mail: $Mail::Sendmail::error\n"; |
---|
| 144 | } |
---|
| 145 | |
---|
[b9060c8] | 146 | if (ref $tweets and keys %{$tweets}) { |
---|
[5ce2daf] | 147 | # save last status to yaml file |
---|
[f72bb76] | 148 | DEBUG or YAML::Tiny::DumpFile("$Bin/status.yml", $stat); |
---|
| 149 | DEBUG and warn "status.yml => ", Dumper($stat); |
---|
[5ce2daf] | 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | sub loadconf { |
---|
| 154 | # load configration data from yaml formatted file |
---|
| 155 | # param => scalar string of filename |
---|
| 156 | # ret => hash object of yaml data |
---|
| 157 | |
---|
| 158 | my $file = shift @_; |
---|
| 159 | |
---|
| 160 | my $yaml = YAML::Tiny->read($file); |
---|
| 161 | |
---|
| 162 | if ($!) { |
---|
| 163 | warn "$0: '$file' $!\n"; |
---|
| 164 | } |
---|
| 165 | |
---|
[641934f] | 166 | DEBUG and warn "'$file' => ", Dumper($yaml); |
---|
| 167 | |
---|
[5ce2daf] | 168 | return $yaml->[0]; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | sub login { |
---|
[e960958] | 172 | # make Net::Twitter::Lite::WithAPIv1_1 object and login |
---|
[5ce2daf] | 173 | # param => hash object of configration |
---|
[e960958] | 174 | # ret => Net::Twitter::Lite::WithAPIv1_1 object |
---|
[5ce2daf] | 175 | |
---|
| 176 | my $conf = shift @_; |
---|
| 177 | |
---|
[e960958] | 178 | my $bot = Net::Twitter::Lite::WithAPIv1_1->new( |
---|
[5ce2daf] | 179 | consumer_key => $conf->{consumer_key}, |
---|
| 180 | consumer_secret => $conf->{consumer_secret}, |
---|
[43d739e] | 181 | legacy_lists_api => 0, |
---|
[7848b0b] | 182 | ssl => 1, |
---|
[5ce2daf] | 183 | ); |
---|
| 184 | |
---|
| 185 | $bot->access_token($conf->{access_token}); |
---|
| 186 | $bot->access_token_secret($conf->{access_token_secret}); |
---|
| 187 | |
---|
| 188 | return $bot; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | sub or_search { |
---|
| 192 | # search tweets containing keywords |
---|
[e960958] | 193 | # param => Net::Twitter::Lite::WithAPIv1_1 object, ArrayRef of keywords, since_id |
---|
[5ce2daf] | 194 | # ret => HashRef of status_id (timeline order is destroyed) |
---|
| 195 | # or undef (none is found) |
---|
| 196 | |
---|
| 197 | my $bot = shift @_; |
---|
| 198 | my $keywords = shift @_; |
---|
[35889d4] | 199 | my $since_id = shift @_ || 1; |
---|
[5ce2daf] | 200 | |
---|
| 201 | my $key = ""; |
---|
| 202 | foreach my $word (@$keywords) { |
---|
| 203 | if ($key) { |
---|
| 204 | $key .= " OR $word"; |
---|
| 205 | } |
---|
| 206 | else { |
---|
| 207 | $key = $word; |
---|
| 208 | } |
---|
| 209 | } |
---|
[f72bb76] | 210 | DEBUG and warn "searching '$key'"; |
---|
[5ce2daf] | 211 | |
---|
| 212 | my $res; |
---|
| 213 | my $ids = {}; |
---|
| 214 | eval { |
---|
| 215 | if ($key) { |
---|
| 216 | $res = $bot->search( |
---|
| 217 | { |
---|
| 218 | q => $key, |
---|
| 219 | since_id => $since_id, |
---|
| 220 | } |
---|
| 221 | ); |
---|
| 222 | } |
---|
[139622c] | 223 | VERBOSE and warn Dumper($res); |
---|
[90f8850] | 224 | if ($res->{statuses}) { |
---|
| 225 | foreach my $tweet (@{$res->{statuses}}) { |
---|
[5ce2daf] | 226 | my $res = $bot->show_status($tweet->{id}); |
---|
[139622c] | 227 | VERBOSE and warn Dumper($res); |
---|
| 228 | |
---|
| 229 | my $id = { |
---|
| 230 | date => str2time($res->{created_at}), |
---|
| 231 | screen_name => $res->{user}{screen_name}, |
---|
| 232 | status_id => $res->{id}, |
---|
| 233 | text => $res->{text}, |
---|
[567a134] | 234 | user_id => $res->{user}{id}, |
---|
[139622c] | 235 | }; |
---|
[5ce2daf] | 236 | if ($res->{retweeted_status}) { |
---|
[567a134] | 237 | $id->{retweet_of} = $res->{retweeted_status}{id}; |
---|
| 238 | $id->{type} = 'retweet'; |
---|
[5ce2daf] | 239 | } |
---|
| 240 | else { |
---|
[139622c] | 241 | $id->{type} = 'search'; |
---|
[5ce2daf] | 242 | } |
---|
[139622c] | 243 | $ids->{$tweet->{id}} = $id; |
---|
[5ce2daf] | 244 | } |
---|
| 245 | } |
---|
| 246 | }; |
---|
| 247 | if ($@) { |
---|
| 248 | evalrescue($@); |
---|
| 249 | } |
---|
| 250 | |
---|
[f72bb76] | 251 | DEBUG and warn "search result => ", Dumper($ids); |
---|
[5ce2daf] | 252 | return $ids; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | sub mentions_ids { |
---|
| 256 | # return status_ids mentioned to me |
---|
[e960958] | 257 | # param => Net::Twitter::Lite::WithAPIv1_1 object, since_id |
---|
[5ce2daf] | 258 | # ret => HashRef of status_id (timeline order is destroyed) |
---|
| 259 | # or undef (none is found) |
---|
| 260 | |
---|
| 261 | my $bot = shift @_; |
---|
[35889d4] | 262 | my $since_id = shift @_ || 1; |
---|
[5ce2daf] | 263 | |
---|
| 264 | my $res; |
---|
| 265 | eval { |
---|
| 266 | $res = $bot->mentions( |
---|
| 267 | { |
---|
| 268 | since_id => $since_id, |
---|
| 269 | } |
---|
| 270 | ); |
---|
[f72bb76] | 271 | VERBOSE and warn Dumper($res); |
---|
[5ce2daf] | 272 | }; |
---|
| 273 | if ($@) { |
---|
| 274 | evalrescue($@); |
---|
| 275 | } |
---|
| 276 | |
---|
[f72bb76] | 277 | my $ids = {}; |
---|
[5ce2daf] | 278 | if ($res && @{$res}) { |
---|
| 279 | $ids = { |
---|
[ac68e58] | 280 | map { |
---|
| 281 | $_->{id} => { |
---|
[139622c] | 282 | date => str2time($_->{created_at}), |
---|
| 283 | screen_name => $_->{user}{screen_name}, |
---|
| 284 | status_id => $_->{id}, |
---|
| 285 | text => $_->{text}, |
---|
| 286 | type => 'mention', |
---|
[567a134] | 287 | user_id => $_->{user}{id}, |
---|
[ac68e58] | 288 | } |
---|
| 289 | } @{$res} |
---|
[5ce2daf] | 290 | }; |
---|
| 291 | } |
---|
| 292 | |
---|
[f72bb76] | 293 | DEBUG and warn "mentions result => ", Dumper($ids); |
---|
[5ce2daf] | 294 | return $ids; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | sub evalrescue { |
---|
| 298 | # output error message at eval error |
---|
| 299 | |
---|
| 300 | use Scalar::Util qw(blessed); |
---|
| 301 | |
---|
| 302 | if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) { |
---|
| 303 | warn $@->error; |
---|
| 304 | if ($@->twitter_error) { |
---|
[a5b6bf1] | 305 | my $twitter_error = $@->twitter_error; |
---|
[d9a45a4] | 306 | if (defined $twitter_error->{errors} && ref($twitter_error->{errors})) { |
---|
[a5b6bf1] | 307 | foreach my $error (@{$twitter_error->{errors}}) { |
---|
| 308 | warn "code => " , $error->{code}, "\n"; |
---|
| 309 | warn "message => ", $error->{message}, "\n"; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | else { |
---|
| 313 | # unknown HASH structure |
---|
| 314 | use Data::Dumper; |
---|
| 315 | warn Dumper $twitter_error; |
---|
| 316 | } |
---|
[5ce2daf] | 317 | } |
---|
| 318 | } |
---|
| 319 | else { |
---|
| 320 | warn $@; |
---|
| 321 | } |
---|
| 322 | } |
---|