« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Validator/Constraint/Default.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Amazon.pm »
| 1 | package Dwarf::Module::SocialMedia::Facebook; | ||
| 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 | ||
| 15 | user_id 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://graph.facebook.com', | ||
| 27 | old_api => 'https://api.facebook.com', | ||
| 28 | authorization => 'https://graph.facebook.com/oauth/authorize', | ||
| 29 | access_token => 'https://graph.facebook.com/oauth/access_token', | ||
| 30 | }; | ||
| 31 | |||
| 32 | $self->{on_error} ||= sub { die @_ }; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _build_name { | ||
| 36 | my $self = shift; | ||
| 37 | $self->init_user unless defined $self->{name}; | ||
| 38 | return $self->{name}; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub _build_profile_image { | ||
| 42 | my $self = shift; | ||
| 43 | $self->init_user unless defined $self->{profile_image}; | ||
| 44 | return $self->{profile_image}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub init_user { | ||
| 48 | my $self = shift; | ||
| 49 | $self->authorized; | ||
| 50 | my $user = $self->show_user; | ||
| 51 | $self->{user_id} = $user->{uid}; | ||
| 52 | $self->{name} = $user->{name}; | ||
| 53 | $self->{profile_image} = $user->{pic_square}; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub authorized { | ||
| 57 | my ($self, $will_die) = @_; | ||
| 58 | $will_die ||= 1; | ||
| 59 | my $authorized = defined $self->access_token; | ||
| 60 | if ($will_die && !$authorized) { | ||
| 61 | $self->on_error("Unauthorized"); | ||
| 62 | } | ||
| 63 | return $authorized; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub is_login { | ||
| 67 | my ($self, $check_connection) = @_; | ||
| 68 | |||
| 69 | return 0 unless $self->authorized(0); | ||
| 70 | return 1 unless $check_connection; | ||
| 71 | |||
| 72 | my $user = $self->show_user; | ||
| 73 | my $is_login = 0; | ||
| 74 | |||
| 75 | # ARRAY 以外のフォーマットが返ってきた時はエラー | ||
| 76 | if (ref $user eq 'HASH') { | ||
| 77 | $is_login = 1; | ||
| 78 | $self->{user_id} = $user->{uid}; | ||
| 79 | $self->{name} = $user->{name}; | ||
| 80 | $self->{profile_image} = $user->{pic_square}; | ||
| 81 | } | ||
| 82 | |||
| 83 | return $is_login; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub publish { | ||
| 87 | my ($self, $message, %data) = @_; | ||
| 88 | $data{message} = $message; | ||
| 89 | $self->call('me/feed', 'POST', \%data); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub reply { | ||
| 93 | my ($self, $id, $message, %data) = @_; | ||
| 94 | $data{message} = $message; | ||
| 95 | $self->call("$id/feed", 'POST', \%data); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub upload { | ||
| 99 | my ($self, $src, $id, $message, %data) = @_; | ||
| 100 | $id //= 'me'; | ||
| 101 | $data{source} = [ $src ], | ||
| 102 | $data{message} = $message if defined $message; | ||
| 103 | $self->call("$id/photos", 'MULTIPART_POST', \%data); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub create_album { | ||
| 107 | my ($self, $name, $message, %data) = @_; | ||
| 108 | $data{name} = $name; | ||
| 109 | $data{message} = $message; | ||
| 110 | $self->call('me/albums', 'POST', \%data); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub is_following { | ||
| 114 | my ($self, $target_id) = @_; | ||
| 115 | die 'target_id must be specified.' unless defined $target_id; | ||
| 116 | |||
| 117 | my $like = 0; | ||
| 118 | my $data = $self->call('me/likes', 'GET'); | ||
| 119 | |||
| 120 | for my $row (@{ $data->{data} }) { | ||
| 121 | if ($row->{id} eq $self->target_id) { | ||
| 122 | $like = 1; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | return $like; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub show_user { | ||
| 130 | my ($self, $id) = @_; | ||
| 131 | $id ||= 'me()'; | ||
| 132 | |||
| 133 | my $data = eval { | ||
| 134 | $self->call( | ||
| 135 | 'method/fql.query', | ||
| 136 | 'GET', | ||
| 137 | { query => 'SELECT uid, name, pic_square FROM user WHERE uid = ' . $id } | ||
| 138 | ); | ||
| 139 | }; | ||
| 140 | if ($@) { | ||
| 141 | warn $@; | ||
| 142 | } | ||
| 143 | |||
| 144 | if (ref $data eq 'ARRAY' and @$data == 1) { | ||
| 145 | return $data->[0]; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | sub get_timeline { | ||
| 150 | my ($self, $id) = @_; | ||
| 151 | $id ||= 'me'; | ||
| 152 | my $res = $self->call("$id/posts", 'GET'); | ||
| 153 | return $res->{data}; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub get_mentions { | ||
| 157 | my ($self, $id) = @_; | ||
| 158 | $id ||= 'me'; | ||
| 159 | my $res = $self->call("$id/feed", 'GET'); | ||
| 160 | my $feed = $res->{data}; | ||
| 161 | my @mentions; | ||
| 162 | for my $f (@{ $feed }) { | ||
| 163 | push @mentions, $f | ||
| 164 | if $f->{from}->{id} ne $self->user_id; | ||
| 165 | } | ||
| 166 | return \@mentions; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub get_likes { | ||
| 170 | my ($self, $id) = @_; | ||
| 171 | $id ||= 'me'; | ||
| 172 | my $res = $self->call("$id/likes", 'GET'); | ||
| 173 | return $res->{data}; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub get_albums { | ||
| 177 | my ($self, $id) = @_; | ||
| 178 | $id ||= 'me'; | ||
| 179 | my $res = $self->call("$id/albums", 'GET'); | ||
| 180 | return $res->{data}; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub get_friends_ids { | ||
| 184 | my ($self, $id) = @_; | ||
| 185 | $id ||= $self->user_id; | ||
| 186 | |||
| 187 | my $result = $self->call('method/fql.query', 'GET', { | ||
| 188 | query => "SELECT uid2 FROM friend WHERE uid1 = me()" | ||
| 189 | }); | ||
| 190 | $result = [] if ref $result ne 'ARRAY'; | ||
| 191 | |||
| 192 | return [ map { $_->{uid2} } @{ $result } ]; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub lookup_users { | ||
| 196 | my ($self, $ids, $rows, $offset) = @_; | ||
| 197 | $ids = join ',', @{ $ids }; | ||
| 198 | |||
| 199 | my $fql = "SELECT uid, name, pic_square FROM user WHERE uid IN"; | ||
| 200 | $fql .= " (" . $ids . ")"; | ||
| 201 | $fql .= " LIMIT $rows" if defined $rows; | ||
| 202 | $fql .= " OFFSET $offset" if defined $offset; | ||
| 203 | |||
| 204 | my $result = $self->call('method/fql.query', 'GET', { query => $fql }); | ||
| 205 | $result = [] if ref $result ne 'ARRAY'; | ||
| 206 | |||
| 207 | return @{ $result }; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub get_authorization_url { | ||
| 211 | my ($self, %params) = @_; | ||
| 212 | |||
| 213 | die 'key must be specified.' unless defined $self->key; | ||
| 214 | die 'secret must be specified.' unless defined $self->secret; | ||
| 215 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 216 | |||
| 217 | $params{client_id} ||= $self->key; | ||
| 218 | $params{scope} ||= 'publish_stream,read_stream,user_photos,user_likes'; | ||
| 219 | |||
| 220 | my $uri = URI->new($self->urls->{authorization}); | ||
| 221 | $uri->query_form(%params); | ||
| 222 | return $uri; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub request_access_token { | ||
| 226 | my ($self, %params) = @_; | ||
| 227 | |||
| 228 | die 'key must be specified.' unless defined $self->key; | ||
| 229 | die 'secret must be specified.' unless defined $self->secret; | ||
| 230 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 231 | die "code must be specified." unless defined $params{code}; | ||
| 232 | |||
| 233 | $params{client_id} ||= $self->key; | ||
| 234 | $params{client_secret} ||= $self->secret; | ||
| 235 | |||
| 236 | my $uri = URI->new($self->urls->{access_token}); | ||
| 237 | $uri->query_form(%params); | ||
| 238 | |||
| 239 | my $res = $self->ua->get($uri); | ||
| 240 | |||
| 241 | if ($res->code !~ /^2/) { | ||
| 242 | $self->on_error->('Facebook OAuth Error: Could not get access token.'); | ||
| 243 | return; | ||
| 244 | } | ||
| 245 | |||
| 246 | my $access_token = $res->decoded_content; | ||
| 247 | $access_token =~ s/^access_token=//; | ||
| 248 | $access_token =~ s/&expires=[0-9]+$//; | ||
| 249 | |||
| 250 | $self->access_token($access_token); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _make_request { | ||
| 254 | my ($self, $command, $method, $params) = @_; | ||
| 255 | |||
| 256 | $method = uc $method; | ||
| 257 | $params->{access_token} ||= $self->access_token; | ||
| 258 | $params->{format} ||= 'json'; | ||
| 259 | |||
| 260 | my $base_url = $command =~ /^method\// | ||
| 261 | ? $self->urls->{'old_api'} | ||
| 262 | : $self->urls->{'api'}; | ||
| 263 | |||
| 264 | my $uri = URI->new($base_url . '/' . $command); | ||
| 265 | $uri->query_form(%{ $params }) if $method =~ /^(GET|DELETE)$/; | ||
| 266 | |||
| 267 | my %data = %{ $params }; | ||
| 268 | |||
| 269 | if ($method eq 'MULTIPART_POST') { | ||
| 270 | $method = 'POST'; | ||
| 271 | my $source = $params->{source}; | ||
| 272 | delete $params->{source}; | ||
| 273 | $uri->query_form(%{ $params }); | ||
| 274 | %data = ( | ||
| 275 | Content_Type => 'multipart/form-data', | ||
| 276 | Content => [ source => $source ], | ||
| 277 | ); | ||
| 278 | } elsif ($method eq 'POST') { | ||
| 279 | %data = (Content => $params) | ||
| 280 | } | ||
| 281 | |||
| 282 | no strict 'refs'; | ||
| 283 | my $req = &{"HTTP::Request::Common::$method"}($uri, %data); | ||
| 284 | $req->header("Content-Length", 0) if $method eq 'DELETE'; | ||
| 285 | |||
| 286 | return $req; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub call { | ||
| 290 | my ($self, $command, $method, $params) = @_; | ||
| 291 | $self->authorized; | ||
| 292 | my $req = $self->_make_request($command, $method, $params); | ||
| 293 | my $res = $self->ua->request($req); | ||
| 294 | return $self->validate($res); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub call_async { | ||
| 298 | my $self = shift; | ||
| 299 | return if @_ == 0; | ||
| 300 | |||
| 301 | $self->authorized; | ||
| 302 | |||
| 303 | my @requests; | ||
| 304 | for my $row (@_) { | ||
| 305 | push @requests, $self->_make_request(@{ $row }); | ||
| 306 | } | ||
| 307 | |||
| 308 | my @responses = $self->ua_async->request_in_parallel(@requests); | ||
| 309 | |||
| 310 | my @contents; | ||
| 311 | for my $res (@responses) { | ||
| 312 | push @contents, $self->validate($res); | ||
| 313 | } | ||
| 314 | |||
| 315 | return @contents; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub validate { | ||
| 319 | my ($self, $res) = @_; | ||
| 320 | my $content = eval { decode_json($res->decoded_content) }; | ||
| 321 | if ($@) { | ||
| 322 | warn "Couldn't decode JSON: $@"; | ||
| 323 | $content = $res->decoded_content; | ||
| 324 | } | ||
| 325 | |||
| 326 | if ($res->code !~ /^2/) { | ||
| 327 | if ($content) { | ||
| 328 | if (ref $content) { | ||
| 329 | my $error_code = $content->{error}->{code} // ''; | ||
| 330 | |||
| 331 | # 506 = 二重投稿 | ||
| 332 | if ($error_code eq '506') { | ||
| 333 | warn 'Facebook API Error: ', $content->{error}->{message}; | ||
| 334 | return $content; | ||
| 335 | } | ||
| 336 | |||
| 337 | $self->on_error->($content->{error}->{message}); | ||
| 338 | } else { | ||
| 339 | $self->on_error->($content); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | $self->on_error->("Invalid Response Header"); | ||
| 344 | } | ||
| 345 | |||
| 346 | return $content; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub parse_date { | ||
| 350 | my ($self, $value) = @_; | ||
| 351 | return DateTime::Format::HTTP | ||
| 352 | ->parse_datetime($value) | ||
| 353 | ->set_time_zone('Asia/Tokyo'); | ||
| 354 | } | ||
| 355 | |||
| 356 | 1; |