« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App/Test.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Session.pm »
| 1 | package Dwarf::Module::SocialMedia::Mixi; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::HTTP::Async; | ||
| 5 | use Dwarf::Util qw/encode_utf8 decode_utf8 shuffle_array/; | ||
| 6 | use HTTP::Request::Common (); | ||
| 7 | use JSON; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | |||
| 10 | use Dwarf::Accessor qw/ | ||
| 11 | ua ua_async urls | ||
| 12 | key secret | ||
| 13 | access_token refresh_token expires_in got_access_token_at | ||
| 14 | user_id name profile_image friends | ||
| 15 | on_error | ||
| 16 | /; | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self->{ua} ||= LWP::UserAgent->new; | ||
| 22 | $self->{ua_async} ||= Dwarf::HTTP::Async->new; | ||
| 23 | |||
| 24 | $self->{urls} ||= { | ||
| 25 | api => 'http://api.mixi-platform.com/2', | ||
| 26 | authorization => 'https://mixi.jp/connect_authorize.pl', | ||
| 27 | access_token => 'https://secure.mixi-platform.com/2/token', | ||
| 28 | }; | ||
| 29 | |||
| 30 | $self->{on_error} ||= sub { die @_ }; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub _build_name { | ||
| 34 | my $self = shift; | ||
| 35 | $self->init_user unless defined $self->{name}; | ||
| 36 | return $self->{name}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _build_profile_image { | ||
| 40 | my $self = shift; | ||
| 41 | $self->init_user unless defined $self->{profile_image}; | ||
| 42 | return $self->{profile_image}; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _bulid_friends { | ||
| 46 | my $self = shift; | ||
| 47 | my $data = $self->call('people/@me/@friends', 'GET', { count => 1000 }); | ||
| 48 | return $data->{entry}; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub init_user { | ||
| 52 | my $self = shift; | ||
| 53 | $self->authorized; | ||
| 54 | my $user = $self->show_user; | ||
| 55 | $self->{name} = encode_utf8($user->{displayName}); | ||
| 56 | $self->{profile_image} = encode_utf8($user->{thumbnailUrl}); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub authorized { | ||
| 60 | my ($self, $will_die) = @_; | ||
| 61 | $will_die ||= 1; | ||
| 62 | my $authorized = defined $self->access_token; | ||
| 63 | if ($will_die && !$authorized) { | ||
| 64 | $self->on_error("Unauthorized"); | ||
| 65 | } | ||
| 66 | return $authorized; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub is_login { | ||
| 70 | my ($self, $check_connection) = @_; | ||
| 71 | |||
| 72 | return 0 unless $self->authorized(0); | ||
| 73 | return 1 unless $check_connection; | ||
| 74 | |||
| 75 | my $data = eval { $self->call('people/@me/@self', 'GET') }; | ||
| 76 | |||
| 77 | my $is_login = 0; | ||
| 78 | |||
| 79 | if (ref $data eq 'HASH') { | ||
| 80 | $is_login = 1; | ||
| 81 | $self->user_id($data->{entry}->{id}); | ||
| 82 | $self->name(encode_utf8($data->{entry}->{displayName})); | ||
| 83 | $self->profile_image(encode_utf8($data->{entry}->{thumbnailUrl})); | ||
| 84 | } | ||
| 85 | |||
| 86 | return $is_login; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub publish { | ||
| 90 | my ($self, $message) = @_; | ||
| 91 | $self->call('voice/statuses', 'POST', { status => $message }); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub send_dm { | ||
| 95 | my ($self, $id, $title, $text) = @_; | ||
| 96 | |||
| 97 | my $json = encode_json { | ||
| 98 | title => $title, | ||
| 99 | body => $text, | ||
| 100 | recipients => [$id] | ||
| 101 | }; | ||
| 102 | |||
| 103 | $self->call('messages/@me/@self/@outbox', 'POST', {}, $json); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub is_following { | ||
| 107 | my ($self, $target_id) = @_; | ||
| 108 | die 'target_id must be specified.' unless defined $target_id; | ||
| 109 | |||
| 110 | unless ($self->friends) { | ||
| 111 | my $data = $self->call('people/@me/@friends', 'GET'); | ||
| 112 | $self->{friends} = $data->{entry}; | ||
| 113 | } | ||
| 114 | |||
| 115 | my $like = 0; | ||
| 116 | |||
| 117 | for my $row (@{ $self->friends }) { | ||
| 118 | if ($row->{id} eq $target_id) { | ||
| 119 | $like = 1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | return $like; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub show_user { | ||
| 127 | my ($self, $id) = @_; | ||
| 128 | my $data = $self->call('people/' . $id . '/@self', 'GET'); | ||
| 129 | if (ref $data eq 'HASH') { | ||
| 130 | return $data->{entry}; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub get_friends_ids { | ||
| 135 | my $self = shift; | ||
| 136 | return map { $_->{id} } $self->get_friends; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub get_friends { | ||
| 140 | my ($self, $rows, $offset) = @_; | ||
| 141 | $offset ||= 0; | ||
| 142 | my @a = @{ $self->friends }; | ||
| 143 | return @a unless defined $rows; | ||
| 144 | return grep { defined $_ } @a[$offset .. $offset + $rows - 1]; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get_authorization_url { | ||
| 148 | my ($self, %params) = @_; | ||
| 149 | |||
| 150 | die 'key must be specified.' unless defined $self->{key}; | ||
| 151 | die 'secret must be specified.' unless defined $self->{secret}; | ||
| 152 | |||
| 153 | $params{client_id} ||= $self->key; | ||
| 154 | $params{response_type} ||= 'code'; | ||
| 155 | $params{display} ||= 'pc'; | ||
| 156 | $params{scope} ||= 'r_profile'; | ||
| 157 | |||
| 158 | my $uri = URI->new($self->urls->{authorization}); | ||
| 159 | $uri->query_form(%params); | ||
| 160 | return $uri; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub request_access_token { | ||
| 164 | my ($self, %params) = @_; | ||
| 165 | |||
| 166 | die 'key must be specified.' unless defined $self->{key}; | ||
| 167 | die 'secret must be specified.' unless defined $self->{secret}; | ||
| 168 | die "grant_type must be specified." unless defined $params{grant_type}; | ||
| 169 | |||
| 170 | $params{client_id} ||= $self->key; | ||
| 171 | $params{client_secret} ||= $self->secret; | ||
| 172 | |||
| 173 | my $now = time; | ||
| 174 | |||
| 175 | my $res = $self->ua->post( | ||
| 176 | $self->urls->{access_token}, | ||
| 177 | \%params | ||
| 178 | ); | ||
| 179 | |||
| 180 | my $data = $self->validate($res); | ||
| 181 | $self->access_token($data->{access_token}); | ||
| 182 | $self->refresh_token($data->{refresh_token}); | ||
| 183 | $self->expires_in($data->{expires_in}); | ||
| 184 | $self->got_access_token_at($now); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub renew_access_token { | ||
| 188 | my $self = shift; | ||
| 189 | my $now = time; | ||
| 190 | |||
| 191 | if (defined $self->expires_in and defined $self->got_access_token_at) { | ||
| 192 | if ($now > $self->got_access_token_at + $self->expires_in) { | ||
| 193 | $self->request_access_token( | ||
| 194 | grant_type => "refresh_token", | ||
| 195 | refresh_token => $self->refresh_token, | ||
| 196 | ); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _make_request { | ||
| 202 | my ($self, $command, $method, $params, $content) = @_; | ||
| 203 | |||
| 204 | $method = uc $method; | ||
| 205 | |||
| 206 | my $uri = URI->new($self->urls->{api} . '/' . $command); | ||
| 207 | |||
| 208 | my @p; | ||
| 209 | if ($method eq 'GET') { | ||
| 210 | $uri->query_form(%{ $params }); | ||
| 211 | @p = ($uri, 'Authorization' => 'OAuth ' . $self->access_token); | ||
| 212 | } else { | ||
| 213 | @p = ($uri, $params, 'Authorization' => 'OAuth ' . $self->access_token); | ||
| 214 | if (defined $content) { | ||
| 215 | push @p, ( | ||
| 216 | 'Content-Type' => 'application/json', | ||
| 217 | 'Content' => $content | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | no strict 'refs'; | ||
| 223 | my $req = &{"HTTP::Request::Common::$method"}(@p); | ||
| 224 | |||
| 225 | return $req; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub call { | ||
| 229 | my ($self, $command, $method, $params, $content) = @_; | ||
| 230 | $self->authorized; | ||
| 231 | $self->renew_access_token; | ||
| 232 | my $req = $self->_make_request($command, $method, $params); | ||
| 233 | my $res = $self->ua->request($req); | ||
| 234 | return $self->validate($res); | ||
| 235 | } | ||
| 236 | |||
| 237 | sub call_async { | ||
| 238 | my $self = shift; | ||
| 239 | $self->authorized; | ||
| 240 | $self->renew_access_token; | ||
| 241 | |||
| 242 | my @requests; | ||
| 243 | for my $row (@_) { | ||
| 244 | push @requests, $self->_make_request(@{ $row }); | ||
| 245 | } | ||
| 246 | |||
| 247 | my @responses = $self->ua_async->request_in_parallel(@requests); | ||
| 248 | |||
| 249 | my @contents; | ||
| 250 | for my $res (@responses) { | ||
| 251 | push @contents, $self->validate($res); | ||
| 252 | } | ||
| 253 | |||
| 254 | return @contents; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub validate { | ||
| 258 | my ($self, $res) = @_; | ||
| 259 | my $content = eval { decode_json($res->content) }; | ||
| 260 | if ($@) { | ||
| 261 | $content = $res->content; | ||
| 262 | } | ||
| 263 | |||
| 264 | unless ($res->code =~ /^2/) { | ||
| 265 | if (my $error = $res->header("www-authenticate")) { | ||
| 266 | $self->on_error->($error); | ||
| 267 | } | ||
| 268 | $self->on_error->("Unknown Error."); | ||
| 269 | } | ||
| 270 | |||
| 271 | return $content; | ||
| 272 | } | ||
| 273 | |||
| 274 | 1; |