« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Validator.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/APIBase.pm »
| 1 | package Dwarf::Module::SocialMedia::Weibo; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::HTTP::Async; | ||
| 5 | use DateTime; | ||
| 6 | use DateTime::Format::HTTP; | ||
| 7 | use HTTP::Request::Common (); | ||
| 8 | use JSON; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | |||
| 11 | use Dwarf::Accessor qw/ | ||
| 12 | ua ua_async urls | ||
| 13 | key secret | ||
| 14 | access_token access_token_secret expires_in | ||
| 15 | user_id screen_name name profile_image | ||
| 16 | on_error | ||
| 17 | /; | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | $self->{ua} ||= LWP::UserAgent->new; | ||
| 23 | $self->{ua_async} ||= Dwarf::HTTP::Async->new; | ||
| 24 | |||
| 25 | $self->{urls} ||= { | ||
| 26 | api => 'https://api.weibo.com/2', | ||
| 27 | authorization => 'https://api.weibo.com/oauth2/authorize', | ||
| 28 | access_token => 'https://api.weibo.com/oauth2/access_token', | ||
| 29 | }; | ||
| 30 | |||
| 31 | $self->{on_error} ||= sub { die @_ }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _build_screen_name { | ||
| 35 | my $self = shift; | ||
| 36 | $self->init_user; | ||
| 37 | return $self->{screen_name}; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _build_name { | ||
| 41 | my $self = shift; | ||
| 42 | $self->init_user; | ||
| 43 | return $self->{name}; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _build_profile_image { | ||
| 47 | my $self = shift; | ||
| 48 | $self->init_user; | ||
| 49 | return $self->{profile_image}; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub init_user { | ||
| 53 | my $self = shift; | ||
| 54 | my $user = $self->show_user; | ||
| 55 | $self->{screen_name} = $user->{screen_name}; | ||
| 56 | $self->{name} = $user->{name}; | ||
| 57 | $self->{profile_image} = $user->{profile_image_url}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub authorized { | ||
| 61 | my ($self, $will_die) = @_; | ||
| 62 | $will_die ||= 1; | ||
| 63 | my $authorized = defined $self->access_token; | ||
| 64 | if ($will_die && !$authorized) { | ||
| 65 | $self->on_error("Unauthorized"); | ||
| 66 | } | ||
| 67 | return $authorized; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub is_login { | ||
| 71 | my ($self, $check_connection) = @_; | ||
| 72 | $check_connection //= 1; | ||
| 73 | |||
| 74 | return 0 unless $self->authorized(0); | ||
| 75 | return 1 unless $check_connection; | ||
| 76 | |||
| 77 | my $data = eval { | ||
| 78 | $self->call( | ||
| 79 | 'account/get_uid', | ||
| 80 | 'GET' | ||
| 81 | ) | ||
| 82 | }; | ||
| 83 | |||
| 84 | my $is_login = 0; | ||
| 85 | |||
| 86 | if (ref $data eq 'HASH') { | ||
| 87 | $is_login = 1; | ||
| 88 | $self->user_id($data->{uid}); | ||
| 89 | } | ||
| 90 | |||
| 91 | return $is_login; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub publish { | ||
| 95 | my ($self, $message) = @_; | ||
| 96 | $self->call('statuses/update', 'POST', { status => $message }); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub reply { | ||
| 100 | my ($self, $in_reply_to_status_id, $message, $screen_name) = @_; | ||
| 101 | $message = "@" . $screen_name . " " . $message if defined $screen_name; | ||
| 102 | $self->call('statuses/update', 'POST', { | ||
| 103 | status => $message, | ||
| 104 | in_reply_to_status_id => $in_reply_to_status_id, | ||
| 105 | }); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub upload { | ||
| 109 | my ($self, $src, $message) = @_; | ||
| 110 | my $data = { | ||
| 111 | status => $message, | ||
| 112 | pic => [ $src ], | ||
| 113 | }; | ||
| 114 | $self->call("statuses/upload", 'MULTIPART_POST', $data); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub send_dm { | ||
| 118 | my ($self, $id, $text) = @_; | ||
| 119 | $self->call('direct_messages/new', 'POST', { | ||
| 120 | user_id => $id, | ||
| 121 | text => $text, | ||
| 122 | }); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub follow { | ||
| 126 | my ($self, $target_screen_name) = @_; | ||
| 127 | return $self->call('friendships/create', 'POST', { | ||
| 128 | screen_name => $target_screen_name | ||
| 129 | }); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub is_following { | ||
| 133 | my ($self, $target_screen_name) = @_; | ||
| 134 | my $data = $self->call('friendships/show', 'GET', { | ||
| 135 | source_id => $self->user_id, | ||
| 136 | target_screen_name => $target_screen_name, | ||
| 137 | }); | ||
| 138 | return $data->{source}->{following} ? 1 : 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub get_rate_limit_status { | ||
| 142 | my ($self) = @_; | ||
| 143 | return $self->call('account/rate_limit_status', 'GET'); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub show_user { | ||
| 147 | my ($self, $id) = @_; | ||
| 148 | $id ||= $self->user_id; | ||
| 149 | return $self->call('users/show', 'GET', { uid => $id }); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_timeline { | ||
| 153 | my ($self, $id, $data) = @_; | ||
| 154 | $id ||= $self->user_id; | ||
| 155 | $data ||= {}; | ||
| 156 | $data->{uid} = $id; | ||
| 157 | my $res = $self->call('statuses/user_timeline', 'GET', $data); | ||
| 158 | if (ref $res eq 'HASH') { | ||
| 159 | return $res->{statuses}; | ||
| 160 | } | ||
| 161 | return []; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub get_mentions { | ||
| 165 | my ($self, $id, $data) = @_; | ||
| 166 | $id ||= $self->user_id; | ||
| 167 | $data ||= {}; | ||
| 168 | $data->{uid} = $id; | ||
| 169 | my $res = $self->call('statuses/mentions', 'GET', $data); | ||
| 170 | if (ref $res eq 'HASH') { | ||
| 171 | return $res->{statuses}; | ||
| 172 | } | ||
| 173 | return []; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub get_direct_messages { | ||
| 177 | my ($self, $id) = @_; | ||
| 178 | $id ||= $self->user_id; | ||
| 179 | die "This method has not implemented yet."; | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub get_friends_ids { | ||
| 184 | my ($self, $id) = @_; | ||
| 185 | $id ||= $self->user_id; | ||
| 186 | |||
| 187 | my $cursor = -1; | ||
| 188 | my @ids; | ||
| 189 | |||
| 190 | while ($cursor != 0) { | ||
| 191 | my $result = $self->call('friendships/friends/ids', 'GET', { | ||
| 192 | uid => $id, | ||
| 193 | cursor => $cursor, | ||
| 194 | }); | ||
| 195 | |||
| 196 | $cursor = $result->{next_cursor}; | ||
| 197 | push @ids, @{ $result->{ids} }; | ||
| 198 | } | ||
| 199 | |||
| 200 | return \@ids; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub get_followers_ids { | ||
| 204 | my $self = shift; | ||
| 205 | die "This method has not implemented yet."; | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub lookup_users { | ||
| 210 | my ($self, $ids, $rows, $offset) = @_; | ||
| 211 | $offset ||= 0; | ||
| 212 | die "This method has not implemented yet."; | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub get_authorization_url { | ||
| 217 | my ($self, %params) = @_; | ||
| 218 | |||
| 219 | die 'key must be specified.' unless defined $self->key; | ||
| 220 | die 'secret must be specified.' unless defined $self->secret; | ||
| 221 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 222 | |||
| 223 | $params{client_id} ||= $self->key; | ||
| 224 | $params{response_type} ||= 'code'; | ||
| 225 | |||
| 226 | my $uri = URI->new($self->urls->{authorization}); | ||
| 227 | $uri->query_form(%params); | ||
| 228 | return $uri; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub request_access_token { | ||
| 232 | my ($self, %params) = @_; | ||
| 233 | |||
| 234 | die 'key must be specified.' unless defined $self->key; | ||
| 235 | die 'secret must be specified.' unless defined $self->secret; | ||
| 236 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 237 | die "code must be specified." unless defined $params{code}; | ||
| 238 | |||
| 239 | $params{client_id} ||= $self->key; | ||
| 240 | $params{client_secret} ||= $self->secret; | ||
| 241 | $params{grant_type} ||= 'authorization_code'; | ||
| 242 | |||
| 243 | my $uri = URI->new($self->urls->{access_token}); | ||
| 244 | $uri->query_form(%params); | ||
| 245 | |||
| 246 | my $res = $self->ua->post($uri); | ||
| 247 | $self->validate($res); | ||
| 248 | |||
| 249 | my $json = $res->content; | ||
| 250 | my $data = eval { decode_json($json) }; | ||
| 251 | if ($@) { | ||
| 252 | warn $data; | ||
| 253 | } | ||
| 254 | |||
| 255 | $self->access_token($data->{access_token}); | ||
| 256 | $self->expires_in($data->{expires_in}); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub _make_request { | ||
| 260 | my ($self, $command, $method, $params) = @_; | ||
| 261 | |||
| 262 | $method = uc $method; | ||
| 263 | $params->{source} ||= $self->key; | ||
| 264 | $params->{access_token} ||= $self->access_token; | ||
| 265 | |||
| 266 | my $uri = URI->new($self->urls->{'api'} . '/' . $command . '.json'); | ||
| 267 | $uri->query_form(%{ $params }) if $method =~ /^(GET|DELETE)$/; | ||
| 268 | |||
| 269 | my %data = %{ $params }; | ||
| 270 | |||
| 271 | if ($method eq 'MULTIPART_POST') { | ||
| 272 | $method = 'POST'; | ||
| 273 | %data = ( | ||
| 274 | Content_Type => 'multipart/form-data', | ||
| 275 | Content => [ %{ $params} ], | ||
| 276 | ); | ||
| 277 | } elsif ($method eq 'POST') { | ||
| 278 | %data = (Content => $params) | ||
| 279 | } | ||
| 280 | |||
| 281 | no strict 'refs'; | ||
| 282 | my $req = &{"HTTP::Request::Common::$method"}($uri, %data); | ||
| 283 | $req->header("Content-Length", 0) if $method eq 'DELETE'; | ||
| 284 | |||
| 285 | return $req; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub call { | ||
| 289 | my ($self, $command, $method, $params) = @_; | ||
| 290 | $self->authorized; | ||
| 291 | my $req = $self->_make_request($command, $method, $params); | ||
| 292 | my $res = $self->ua->request($req); | ||
| 293 | return $self->validate($res); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub call_async { | ||
| 297 | my $self = shift; | ||
| 298 | return if @_ == 0; | ||
| 299 | |||
| 300 | $self->authorized; | ||
| 301 | |||
| 302 | my @requests; | ||
| 303 | for my $row (@_) { | ||
| 304 | push @requests, $self->_make_request(@{ $row }); | ||
| 305 | } | ||
| 306 | |||
| 307 | my @responses = $self->ua_async->request_in_parallel(@requests); | ||
| 308 | |||
| 309 | my @contents; | ||
| 310 | for my $res (@responses) { | ||
| 311 | push @contents, $self->validate($res); | ||
| 312 | } | ||
| 313 | |||
| 314 | return @contents; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub validate { | ||
| 318 | my ($self, $res) = @_; | ||
| 319 | my $content = eval { | ||
| 320 | my $ct = $res->content; | ||
| 321 | $ct =~ s/0\r\n\r\n$//; | ||
| 322 | decode_json($ct); | ||
| 323 | }; | ||
| 324 | if ($@) { | ||
| 325 | warn "Couldn't decode JSON: $@"; | ||
| 326 | $content = $res->content; | ||
| 327 | } | ||
| 328 | |||
| 329 | if ($res->code !~ /^2/) { | ||
| 330 | if ($content) { | ||
| 331 | if (ref $content) { | ||
| 332 | if ($content->{error} =~ /rate limit/) { | ||
| 333 | my $limit = $self->get_rate_limit_status; | ||
| 334 | my $reset = DateTime | ||
| 335 | ->from_epoch(epoch => time + $limit->{reset_time_in_seconds}) | ||
| 336 | ->set_time_zone('Asia/Tokyo'); | ||
| 337 | $self->on_error->($reset); | ||
| 338 | } | ||
| 339 | |||
| 340 | $self->on_error->($content->{error}, $content->{error_code}); | ||
| 341 | } else { | ||
| 342 | $self->on_error->($content); | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | $self->on_error->("Invalid Response Header"); | ||
| 347 | } | ||
| 348 | |||
| 349 | return $content; | ||
| 350 | } | ||
| 351 | |||
| 352 | sub parse_date { | ||
| 353 | my ($self, $value) = @_; | ||
| 354 | $value =~ s/\+\d{4} //; | ||
| 355 | return DateTime::Format::HTTP | ||
| 356 | ->parse_datetime($value) | ||
| 357 | ->add(hours => 1) | ||
| 358 | ->set_time_zone('Asia/Tokyo'); | ||
| 359 | } | ||
| 360 | |||
| 361 | 1; |