« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Rakuten.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App/DB/Schema/Dumper.pm »
| 1 | package Dwarf::Module::SocialMedia::Twitter; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::HTTP::Async; | ||
| 5 | use Data::Dumper; | ||
| 6 | use DateTime; | ||
| 7 | use DateTime::Format::HTTP; | ||
| 8 | use Digest::SHA qw//; | ||
| 9 | use Encode qw/encode_utf8/; | ||
| 10 | use HTTP::Request::Common; | ||
| 11 | use HTTP::Response; | ||
| 12 | use JSON; | ||
| 13 | use LWP::UserAgent; | ||
| 14 | use Net::OAuth; | ||
| 15 | |||
| 16 | use Dwarf::Accessor qw/ | ||
| 17 | ua ua_async urls | ||
| 18 | key secret | ||
| 19 | request_token request_token_secret | ||
| 20 | access_token access_token_secret | ||
| 21 | user_id screen_name name profile_image | ||
| 22 | on_error | ||
| 23 | /; | ||
| 24 | |||
| 25 | $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A; | ||
| 26 | |||
| 27 | sub init { | ||
| 28 | my $self = shift; | ||
| 29 | my $c = $self->c; | ||
| 30 | |||
| 31 | $self->{ua} ||= LWP::UserAgent->new( | ||
| 32 | timeout => 120 | ||
| 33 | ); | ||
| 34 | |||
| 35 | $self->{ua_async} ||= Dwarf::HTTP::Async->new; | ||
| 36 | |||
| 37 | $self->{urls} ||= { | ||
| 38 | api => 'https://api.twitter.com/1.1', | ||
| 39 | request_token => 'https://api.twitter.com/oauth/request_token', | ||
| 40 | authentication => 'https://api.twitter.com/oauth/authenticate', | ||
| 41 | authorization => 'https://api.twitter.com/oauth/authorize', | ||
| 42 | access_token => 'https://api.twitter.com/oauth/access_token', | ||
| 43 | }; | ||
| 44 | |||
| 45 | $self->{on_error} ||= sub { die @_ }; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _build_user_id { | ||
| 49 | my $self = shift; | ||
| 50 | $self->init_user unless defined $self->{user_id}; | ||
| 51 | return $self->{user_id}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _build_screen_name { | ||
| 55 | my $self = shift; | ||
| 56 | $self->init_user unless defined $self->{screen_name}; | ||
| 57 | return $self->{screen_name}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _build_name { | ||
| 61 | my $self = shift; | ||
| 62 | $self->init_user unless defined $self->{name}; | ||
| 63 | return $self->{name}; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _build_profile_image { | ||
| 67 | my $self = shift; | ||
| 68 | $self->init_user unless defined $self->{profile_image}; | ||
| 69 | return $self->{profile_image}; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub init_user { | ||
| 73 | my $self = shift; | ||
| 74 | my $user = $self->show_user; | ||
| 75 | $self->{user_id} = $user->{id}; | ||
| 76 | $self->{screen_name} = $user->{screen_name}; | ||
| 77 | $self->{name} = encode_utf8($user->{name}); | ||
| 78 | $self->{profile_image} = encode_utf8($user->{profile_image_url}); | ||
| 79 | } | ||
| 80 | |||
| 81 | sub authorized { | ||
| 82 | my ($self, $will_die) = @_; | ||
| 83 | $will_die ||= 1; | ||
| 84 | my $authorized = defined $self->access_token && defined $self->access_token_secret; | ||
| 85 | if ($will_die && !$authorized) { | ||
| 86 | $self->on_error("Unauthorized"); | ||
| 87 | } | ||
| 88 | return $authorized; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub is_login { | ||
| 92 | my ($self, $check_connection) = @_; | ||
| 93 | |||
| 94 | return 0 unless $self->authorized; | ||
| 95 | return 1 unless $check_connection; | ||
| 96 | |||
| 97 | my $data; | ||
| 98 | eval { | ||
| 99 | $data = $self->show_user; | ||
| 100 | }; | ||
| 101 | if ($@) { | ||
| 102 | warn $@; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $is_login = 0; | ||
| 106 | if (ref $data eq 'HASH') { | ||
| 107 | $is_login = 1; | ||
| 108 | $self->{user_id} = $data->{id}; | ||
| 109 | $self->{screen_name} = $data->{screen_name}; | ||
| 110 | $self->{name} = encode_utf8($data->{name}); | ||
| 111 | $self->{profile_image} = encode_utf8($data->{profile_image_url}); | ||
| 112 | } | ||
| 113 | |||
| 114 | return $is_login; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub show_user { | ||
| 118 | my ($self, $id) = @_; | ||
| 119 | $id ||= $self->{user_id}; | ||
| 120 | |||
| 121 | my $data; | ||
| 122 | unless ($self->{user_id}) { | ||
| 123 | $data = $self->call( | ||
| 124 | 'account/verify_credentials', | ||
| 125 | 'GET' | ||
| 126 | ); | ||
| 127 | } else { | ||
| 128 | # accout/verify_credentials を節約するために | ||
| 129 | # users/lookup で代替出来るケースでは代替する | ||
| 130 | $data = $self->call('users/lookup', 'POST', { user_id => $id }); | ||
| 131 | if (ref $data eq 'ARRAY') { | ||
| 132 | $data = $data->[0]; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | return $data; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub publish { | ||
| 140 | my ($self, $message) = @_; | ||
| 141 | $self->call('statuses/update', 'POST', { status => $message }); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub reply { | ||
| 145 | my ($self, $in_reply_to_status_id, $message, $screen_name) = @_; | ||
| 146 | $message = "@" . $screen_name . " " . $message if defined $screen_name; | ||
| 147 | $self->call('statuses/update', 'POST', { | ||
| 148 | status => $message, | ||
| 149 | in_reply_to_status_id => $in_reply_to_status_id, | ||
| 150 | }); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub upload { | ||
| 154 | my ($self, $src, $message) = @_; | ||
| 155 | |||
| 156 | my $url = $self->urls->{api} . '/statuses/update_with_media.json'; | ||
| 157 | |||
| 158 | my $oauth = Net::OAuth->request('protected resource')->new( | ||
| 159 | version => '1.0', | ||
| 160 | request_url => $url, | ||
| 161 | request_method => 'POST', | ||
| 162 | token => $self->access_token, | ||
| 163 | token_secret => $self->access_token_secret, | ||
| 164 | consumer_key => $self->key, | ||
| 165 | consumer_secret => $self->secret, | ||
| 166 | signature_method => 'HMAC-SHA1', | ||
| 167 | timestamp => time, | ||
| 168 | nonce => Digest::SHA::sha1_base64(time . $$ . rand), | ||
| 169 | ); | ||
| 170 | $oauth->sign; | ||
| 171 | |||
| 172 | my $req = POST($url, | ||
| 173 | Content_type => 'multipart/form-data', | ||
| 174 | Authorization => $oauth->to_authorization_header, | ||
| 175 | Content => [ | ||
| 176 | status => $message, | ||
| 177 | 'media[]' => [ $src ] | ||
| 178 | ], | ||
| 179 | ); | ||
| 180 | my $res = $self->ua->request($req); | ||
| 181 | |||
| 182 | return $self->validate($res); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub send_dm { | ||
| 186 | my ($self, $id, $text) = @_; | ||
| 187 | $self->call('direct_messages/new', 'POST', { | ||
| 188 | user_id => $id, | ||
| 189 | text => $text, | ||
| 190 | }); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub follow { | ||
| 194 | my ($self, $target_screen_name) = @_; | ||
| 195 | return $self->call('friendships/create', 'POST', { | ||
| 196 | screen_name => $target_screen_name | ||
| 197 | }); | ||
| 198 | } | ||
| 199 | |||
| 200 | sub is_following { | ||
| 201 | my ($self, $target_screen_name) = @_; | ||
| 202 | my $data = $self->call('friendships/show', 'GET', { | ||
| 203 | source_id => $self->user_id, | ||
| 204 | target_screen_name => $target_screen_name, | ||
| 205 | }); | ||
| 206 | return $data->{relationship}->{source}->{following} ? 1 : 0; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub get_rate_limit_status { | ||
| 210 | my ($self) = @_; | ||
| 211 | return $self->call('account/rate_limit_status', 'GET'); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub get_timeline { | ||
| 215 | my ($self, $id, $data) = @_; | ||
| 216 | $id ||= $self->user_id; | ||
| 217 | $data ||= {}; | ||
| 218 | $data->{uid} = $id; | ||
| 219 | return $self->call('statuses/user_timeline', 'GET', $data); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub get_mentions { | ||
| 223 | my ($self, $id, $data) = @_; | ||
| 224 | $id ||= $self->user_id; | ||
| 225 | $data ||= {}; | ||
| 226 | $data->{uid} = $id; | ||
| 227 | my $res = $self->call('statuses/mentions', 'GET', $data); | ||
| 228 | return $res; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub get_sent_messages { | ||
| 232 | my ($self) = @_; | ||
| 233 | return $self->call('direct_messages/sent', 'GET'); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub get_friends_ids { | ||
| 237 | my ($self, $id) = @_; | ||
| 238 | $id ||= $self->user_id; | ||
| 239 | |||
| 240 | my $cursor = -1; | ||
| 241 | my @ids = (); | ||
| 242 | |||
| 243 | while ($cursor != 0) { | ||
| 244 | my $result = $self->call('friends/ids', 'GET', { | ||
| 245 | user_id => $id, | ||
| 246 | cursor => $cursor, | ||
| 247 | }); | ||
| 248 | |||
| 249 | $cursor = $result->{next_cursor_str}; | ||
| 250 | push @ids, @{ $result->{ids} }; | ||
| 251 | } | ||
| 252 | |||
| 253 | return \@ids; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub get_followers_ids { | ||
| 257 | my ($self, $id) = @_; | ||
| 258 | $id ||= $self->user_id; | ||
| 259 | |||
| 260 | my $cursor = -1; | ||
| 261 | my @ids; | ||
| 262 | |||
| 263 | while ($cursor != 0) { | ||
| 264 | my $result = $self->call('followers/ids', 'GET', { | ||
| 265 | user_id => $id, | ||
| 266 | cursor => $cursor, | ||
| 267 | }); | ||
| 268 | |||
| 269 | $cursor = $result->{next_cursor_str}; | ||
| 270 | push @ids, @{ $result->{ids} }; | ||
| 271 | } | ||
| 272 | |||
| 273 | return @ids; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub lookup_users { | ||
| 277 | my ($self, $ids, $rows, $offset) = @_; | ||
| 278 | $offset ||= 0; | ||
| 279 | |||
| 280 | my @ids = @$ids; | ||
| 281 | @ids = grep { defined $_ } @ids[$offset .. $offset + $rows - 1]; | ||
| 282 | return () if @ids == 0; | ||
| 283 | |||
| 284 | my $rpp = 100; | ||
| 285 | my $len = int(@ids / $rpp); | ||
| 286 | |||
| 287 | my $users; | ||
| 288 | my @requests; | ||
| 289 | |||
| 290 | for my $i (0 .. $len) { | ||
| 291 | my @a = @ids; | ||
| 292 | @a = grep { defined $_ } @a[$i * $rpp .. ($i + 1) * $rpp - 1]; | ||
| 293 | next if @a == 0; | ||
| 294 | |||
| 295 | push @requests, [ | ||
| 296 | 'users/lookup', | ||
| 297 | 'POST', | ||
| 298 | { user_id => join ',', @a } | ||
| 299 | ]; | ||
| 300 | } | ||
| 301 | |||
| 302 | my @contents = $self->call_async(@requests); | ||
| 303 | for my $content (@contents) { | ||
| 304 | for my $user (@$content) { | ||
| 305 | $users->{ $user->{id} } = $user; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | return map { $users->{$_} } grep { exists $users->{$_} } @ids; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub make_oauth_request { | ||
| 312 | my ($self, $type, %params) = @_; | ||
| 313 | |||
| 314 | die 'key must be specified.' unless defined $self->key; | ||
| 315 | die 'secret must be specified.' unless defined $self->secret; | ||
| 316 | |||
| 317 | local $Net::OAuth::SKIP_UTF8_DOUBLE_ENCODE_CHECK = 1; | ||
| 318 | |||
| 319 | my $req = Net::OAuth->request($type)->new( | ||
| 320 | version => '1.0', | ||
| 321 | consumer_key => $self->key, | ||
| 322 | consumer_secret => $self->secret, | ||
| 323 | signature_method => 'HMAC-SHA1', | ||
| 324 | timestamp => time, | ||
| 325 | nonce => Digest::SHA::sha1_base64(time . $$ . rand), | ||
| 326 | %params, | ||
| 327 | ); | ||
| 328 | $req->sign; | ||
| 329 | |||
| 330 | if ($req->request_method eq 'POST') { | ||
| 331 | return POST $req->normalized_request_url, $req->to_hash; | ||
| 332 | } | ||
| 333 | |||
| 334 | return GET $req->to_url; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub get_authorization_url { | ||
| 338 | my ($self, %params) = @_; | ||
| 339 | |||
| 340 | die "callback must be specified." unless defined $params{callback}; | ||
| 341 | |||
| 342 | $params{request_url} ||= $self->urls->{request_token}; | ||
| 343 | $params{request_method} ||= 'GET'; | ||
| 344 | |||
| 345 | my $req = $self->make_oauth_request('request token', %params); | ||
| 346 | my $res = $self->ua->request($req); | ||
| 347 | |||
| 348 | # Twitter が落ちている | ||
| 349 | if ($res->code =~ /^5/) { | ||
| 350 | $self->on_error->('Twitter OAuth Error: Could not get authorization url.'); | ||
| 351 | return; | ||
| 352 | } | ||
| 353 | |||
| 354 | my $uri = URI->new; | ||
| 355 | $uri->query($res->content); | ||
| 356 | my %res_param = $uri->query_form; | ||
| 357 | |||
| 358 | $self->request_token($res_param{oauth_token}); | ||
| 359 | $self->request_token_secret($res_param{oauth_token_secret}); | ||
| 360 | |||
| 361 | $uri = URI->new($self->urls->{authentication}); | ||
| 362 | $uri->query_form(oauth_token => $self->request_token); | ||
| 363 | |||
| 364 | return $uri; | ||
| 365 | } | ||
| 366 | |||
| 367 | sub request_access_token { | ||
| 368 | my ($self, %params) = @_; | ||
| 369 | |||
| 370 | die "verifier must be specified." unless defined $params{verifier}; | ||
| 371 | |||
| 372 | $params{request_url} ||= $self->urls->{access_token}; | ||
| 373 | $params{request_method} ||= 'GET'; | ||
| 374 | $params{token} ||= $self->request_token; | ||
| 375 | $params{token_secret} ||= $self->request_token_secret; | ||
| 376 | |||
| 377 | my $req = $self->make_oauth_request('access token', %params); | ||
| 378 | my $res = $self->ua->request($req); | ||
| 379 | |||
| 380 | # Twitter が落ちている | ||
| 381 | if ($res->code !~ /^2/) { | ||
| 382 | $self->on_error->('Twitter OAuth Error: Could not get access token.'); | ||
| 383 | return; | ||
| 384 | } | ||
| 385 | |||
| 386 | delete $self->{request_token}; | ||
| 387 | delete $self->{request_token_secret}; | ||
| 388 | |||
| 389 | my $uri = URI->new; | ||
| 390 | $uri->query($res->content); | ||
| 391 | my %res_param = $uri->query_form; | ||
| 392 | |||
| 393 | $self->user_id($res_param{user_id}); | ||
| 394 | $self->screen_name($res_param{screen_name}); | ||
| 395 | $self->access_token($res_param{oauth_token}); | ||
| 396 | $self->access_token_secret($res_param{oauth_token_secret}); | ||
| 397 | } | ||
| 398 | |||
| 399 | sub _make_request { | ||
| 400 | my ($self, $command, $method, $params) = @_; | ||
| 401 | my $req = $self->make_oauth_request( | ||
| 402 | 'protected resource', | ||
| 403 | request_url => $self->urls->{api} . '/' . $command . '.json', | ||
| 404 | request_method => $method, | ||
| 405 | extra_params => $params, | ||
| 406 | token => $self->access_token, | ||
| 407 | token_secret => $self->access_token_secret | ||
| 408 | ); | ||
| 409 | |||
| 410 | return $req; | ||
| 411 | } | ||
| 412 | |||
| 413 | sub call { | ||
| 414 | my ($self, $command, $method, $params) = @_; | ||
| 415 | $self->authorized; | ||
| 416 | my $req = $self->_make_request($command, $method, $params); | ||
| 417 | my $res = $self->ua->request($req); | ||
| 418 | return $self->validate($res); | ||
| 419 | } | ||
| 420 | |||
| 421 | sub call_async { | ||
| 422 | my $self = shift; | ||
| 423 | return if @_ == 0; | ||
| 424 | |||
| 425 | $self->authorized; | ||
| 426 | |||
| 427 | my @requests; | ||
| 428 | for my $row (@_) { | ||
| 429 | push @requests, $self->_make_request(@{ $row }); | ||
| 430 | } | ||
| 431 | |||
| 432 | my @responses = $self->ua_async->request_in_parallel(@requests); | ||
| 433 | |||
| 434 | my @contents; | ||
| 435 | for my $res (@responses) { | ||
| 436 | push @contents, $self->validate($res); | ||
| 437 | } | ||
| 438 | |||
| 439 | return @contents; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub validate { | ||
| 443 | my ($self, $res) = @_; | ||
| 444 | my $c = $self->c; | ||
| 445 | |||
| 446 | my $content = eval { decode_json($res->content) }; | ||
| 447 | if ($@) { | ||
| 448 | warn "Couldn't decode JSON: $@"; | ||
| 449 | warn $res->content; | ||
| 450 | $content = $res->content; | ||
| 451 | } | ||
| 452 | |||
| 453 | my $hdr = $res->headers; | ||
| 454 | my $code = $res->code; | ||
| 455 | |||
| 456 | if ($c->config_name ne 'production' and defined $hdr->{"x-ratelimit-remaining"}) { | ||
| 457 | warn "Ratelimit: " . $hdr->{"x-ratelimit-remaining"} . "/" . $hdr->{"x-ratelimit-limit"}; | ||
| 458 | } | ||
| 459 | |||
| 460 | unless ($code =~ /^2/) { | ||
| 461 | # 400 系 | ||
| 462 | if ($code =~ /^4/) { | ||
| 463 | unless (ref $content) { | ||
| 464 | warn Dumper $res; | ||
| 465 | $content ||= $res->code; | ||
| 466 | $self->on_error->('Twitter API Error: ' . $content); | ||
| 467 | return; | ||
| 468 | } | ||
| 469 | |||
| 470 | my $error_code = $content->{errors}->[0]->{code} // ''; | ||
| 471 | # 89 = トークン切れ | ||
| 472 | if ($error_code eq '89') { | ||
| 473 | $self->on_error->('Twitter API Error: ' . $content->{errors}->[0]->{message}); | ||
| 474 | return; | ||
| 475 | } | ||
| 476 | # 64 = アカウント凍結 | ||
| 477 | elsif ($error_code eq '64') { | ||
| 478 | $self->on_error->('Twitter API Error: ' . $content->{errors}->[0]->{message}); | ||
| 479 | return; | ||
| 480 | } | ||
| 481 | # 187 = 二重投稿 | ||
| 482 | elsif ($error_code eq '187') { | ||
| 483 | warn "Twitter API Error: ", $content->{errors}->[0]->{message}; | ||
| 484 | return $content; | ||
| 485 | } | ||
| 486 | # 88 = Rate Limit オーバー | ||
| 487 | elsif ($error_code eq '88') { | ||
| 488 | $self->on_error->('Twitter API Error: ' . $content->{errors}->[0]->{message}); | ||
| 489 | return; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | # 500 系 | ||
| 493 | else { | ||
| 494 | # LWP::UserAgent 内部エラー | ||
| 495 | if ($hdr->{'client-warning'}) { | ||
| 496 | # タイムアウト | ||
| 497 | if ($content =~ /timeout/) { | ||
| 498 | $self->on_error->('Twitter API Internal Error: Request Timeout.'); | ||
| 499 | return; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | else { | ||
| 503 | my $error_code = $content->{errors}->[0]->{code} // ''; | ||
| 504 | # 130 = Over Capacity | ||
| 505 | if ($error_code eq '130') { | ||
| 506 | $self->on_error->('Twitter API Internal Error: ' . $content->{errors}->[0]->{message}); | ||
| 507 | return; | ||
| 508 | } | ||
| 509 | # 131 = Internal Error | ||
| 510 | elsif ($error_code eq '131') { | ||
| 511 | $self->on_error->('Twitter API Internal Error: ' . $content->{errors}->[0]->{message}); | ||
| 512 | return; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | |||
| 516 | use Data::Dumper; | ||
| 517 | warn Dumper $res; | ||
| 518 | |||
| 519 | $self->on_error->('Twitter API Unknown Error: ' . $res->content); | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | return $content; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub parse_date { | ||
| 527 | my ($self, $value) = @_; | ||
| 528 | $value =~ s/\+\d{4} //; | ||
| 529 | return DateTime::Format::HTTP | ||
| 530 | ->parse_datetime($value) | ||
| 531 | ->add(hours => 9) | ||
| 532 | ->set_time_zone('Asia/Tokyo'); | ||
| 533 | } | ||
| 534 | |||
| 535 | 1; |