| 1 | package Net::Google::PicasaWeb; | ||
| 2 | { | ||
| 3 | $Net::Google::PicasaWeb::VERSION = '0.12'; | ||
| 4 | } | ||
| 5 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | # ABSTRACT: use Google's Picasa Web API | ||
| 8 | |||
| 9 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use HTTP::Message; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use HTTP::Request::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use HTTP::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use Net::Google::AuthSub; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use URI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use XML::Twig; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | |||
| 18 | use Net::Google::PicasaWeb::Album; | ||
| 19 | use Net::Google::PicasaWeb::Comment; | ||
| 20 | use Net::Google::PicasaWeb::MediaEntry; | ||
| 21 | |||
| 22 | |||
| 23 | has authenticator => ( | ||
| 24 | is => 'rw', | ||
| 25 | isa => 'Net::Google::AuthSub', | ||
| 26 | required => 1, | ||
| 27 | lazy_build => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub _build_authenticator { | ||
| 31 | my $version = $Net::Google::PicasaWeb::VERSION || 'TEST'; | ||
| 32 | Net::Google::AuthSub->new( | ||
| 33 | service => 'lh2', # Picasa Web Albums | ||
| 34 | source => 'Net::Google::PicasaWeb-'.$version, | ||
| 35 | ); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | has user_agent => ( | ||
| 40 | is => 'rw', | ||
| 41 | isa => 'LWP::UserAgent', | ||
| 42 | required => 1, | ||
| 43 | lazy_build => 1, | ||
| 44 | ); | ||
| 45 | |||
| 46 | sub _build_user_agent { | ||
| 47 | LWP::UserAgent->new( | ||
| 48 | cookie_jar => {}, | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | has service_base_url => ( | ||
| 54 | is => 'rw', | ||
| 55 | isa => 'Str', | ||
| 56 | required => 1, | ||
| 57 | default => 'http://picasaweb.google.com/data/feed/api/', | ||
| 58 | ); | ||
| 59 | |||
| 60 | |||
| 61 | has xml_namespaces => ( | ||
| 62 | is => 'rw', | ||
| 63 | isa => 'HashRef[Str]', | ||
| 64 | required => 1, | ||
| 65 | lazy_build => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | sub _build_xml_namespaces { | ||
| 69 | { | ||
| 70 | 'http://search.yahoo.com/mrss/' => 'media', | ||
| 71 | 'http://schemas.google.com/photos/2007' => 'gphoto', | ||
| 72 | 'http://www.georss.org/georss' => 'georss', | ||
| 73 | 'http://www.opengis.net/gml' => 'gml', | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub login { | ||
| 79 | my $self = shift; | ||
| 80 | my $response = $self->authenticator->login(@_); | ||
| 81 | |||
| 82 | croak "error logging in: $@" unless defined $response; | ||
| 83 | croak "error logging in: ", $response->error unless $response->is_success; | ||
| 84 | |||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub list_albums { | ||
| 90 | my ($self, %params) = @_; | ||
| 91 | $params{kind} = 'album'; | ||
| 92 | |||
| 93 | my $user_id = delete $params{user_id} || 'default'; | ||
| 94 | return $self->list_entries( | ||
| 95 | 'Net::Google::PicasaWeb::Album', | ||
| 96 | [ 'user', $user_id ], | ||
| 97 | %params | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub get_album { | ||
| 103 | my ($self, %params) = @_; | ||
| 104 | |||
| 105 | croak "missing album_id parameter" unless defined $params{album_id}; | ||
| 106 | |||
| 107 | my $user_id = delete $params{user_id} || 'default'; | ||
| 108 | my $album_id = delete $params{album_id}; | ||
| 109 | |||
| 110 | return $self->get_entry( | ||
| 111 | 'Net::Google::PicasaWeb::Album', | ||
| 112 | [ 'user', $user_id, 'albumid', $album_id ], | ||
| 113 | %params | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | sub add_album { | ||
| 119 | my ($self, %params) = @_; | ||
| 120 | |||
| 121 | my $twig = XML::Twig->new( | ||
| 122 | pretty_print => 'indented', | ||
| 123 | empty_tags => 'expand', | ||
| 124 | ); | ||
| 125 | |||
| 126 | my $root = XML::Twig::Elt->new( | ||
| 127 | 'entry' => { | ||
| 128 | 'xmlns' => 'http://www.w3.org/2005/Atom', | ||
| 129 | 'xmlns:media' => 'http://search.yahoo.com/mrss/', | ||
| 130 | 'xmlns:gphoto' => 'http://schemas.google.com/photos/2007', | ||
| 131 | } | ||
| 132 | ); | ||
| 133 | |||
| 134 | $twig->set_root($root); | ||
| 135 | |||
| 136 | $root->insert_new_elt('last_child', title => {type => 'text'}, $params{title}); | ||
| 137 | $root->insert_new_elt('last_child', summary => {type => 'text'}, $params{summary}); | ||
| 138 | |||
| 139 | foreach my $gphoto ('location', 'access', 'commentingEnabled', 'timestamp') { | ||
| 140 | $root->insert_new_elt('last_child', 'gphoto:' . $gphoto, $params{$gphoto}); | ||
| 141 | } | ||
| 142 | |||
| 143 | my $group = $root->insert_new_elt('last_child', 'media:group'); | ||
| 144 | |||
| 145 | if (defined $params{keywords}) { | ||
| 146 | $group->insert_new_elt('last_child', 'media:keywords', join(', ', $params{keywords})); | ||
| 147 | } | ||
| 148 | |||
| 149 | $root->insert_new_elt('last_child', | ||
| 150 | 'category' => { | ||
| 151 | 'scheme' => 'http://schemas.google.com/g/2005#kind', | ||
| 152 | 'term' => 'http://schemas.google.com/photos/2007#album' | ||
| 153 | } | ||
| 154 | ); | ||
| 155 | |||
| 156 | my $uri = $self->service_base_url . 'user/default'; | ||
| 157 | my $response = $self->request('POST', $uri, $twig->sprint(), 'application/atom+xml'); | ||
| 158 | |||
| 159 | $twig->purge(); | ||
| 160 | |||
| 161 | if ($response->is_error) { | ||
| 162 | croak $response->status_line; | ||
| 163 | } | ||
| 164 | |||
| 165 | my @entries = $self->_parse_feed('Net::Google::PicasaWeb::Album', 'entry', $response->content); | ||
| 166 | return scalar $entries[0]; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | # This is a tiny cheat that allows us to reuse the list_entries method | ||
| 171 | { | ||
| 172 | package Net::Google::PicasaWeb::Tag; | ||
| 173 | { | ||
| 174 | $Net::Google::PicasaWeb::Tag::VERSION = '0.12'; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub from_feed { | ||
| 178 | my ($class, $service, $entry) = @_; | ||
| 179 | return $entry->field('title'); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | sub list_tags { | ||
| 184 | my ($self, %params) = @_; | ||
| 185 | $params{kind} = 'tag'; | ||
| 186 | |||
| 187 | my $user_id = delete $params{user_id} || 'default'; | ||
| 188 | return $self->list_entries( | ||
| 189 | 'Net::Google::PicasaWeb::Tag', | ||
| 190 | [ 'user', $user_id ], | ||
| 191 | %params | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | sub list_comments { | ||
| 197 | my ($self, %params) = @_; | ||
| 198 | $params{kind} = 'comment'; | ||
| 199 | |||
| 200 | my $user_id = delete $params{user_id} || 'default'; | ||
| 201 | return $self->list_entries( | ||
| 202 | 'Net::Google::PicasaWeb::Comment', | ||
| 203 | [ 'user', $user_id ], | ||
| 204 | %params | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _feed_url { | ||
| 209 | my ($self, $path, $query) = @_; | ||
| 210 | |||
| 211 | $path = join '/', @$path if ref $path; | ||
| 212 | $path = $self->service_base_url . $path | ||
| 213 | unless $path =~ m{^https?:}; | ||
| 214 | |||
| 215 | my $uri = URI->new($path); | ||
| 216 | $uri->query_form($query) if $query; | ||
| 217 | |||
| 218 | return $uri; | ||
| 219 | } | ||
| 220 | |||
| 221 | |||
| 222 | sub get_comment { | ||
| 223 | my ($self, %params) = @_; | ||
| 224 | |||
| 225 | croak "missing album_id parameter" unless defined $params{album_id}; | ||
| 226 | croak "missing photo_id parameter" unless defined $params{photo_id}; | ||
| 227 | croak "missing comment_id parameter" unless defined $params{comment_id}; | ||
| 228 | |||
| 229 | my $user_id = delete $params{user_id} || 'default'; | ||
| 230 | my $album_id = delete $params{album_id}; | ||
| 231 | my $photo_id = delete $params{photo_id}; | ||
| 232 | my $comment_id = delete $params{comment_id}; | ||
| 233 | |||
| 234 | return $self->get_entry( | ||
| 235 | 'Net::Google::PicasaWeb::Comment', | ||
| 236 | [ | ||
| 237 | user => $user_id, | ||
| 238 | albumid => $album_id, | ||
| 239 | photoid => $photo_id, | ||
| 240 | commentid => $comment_id, | ||
| 241 | ], | ||
| 242 | %params | ||
| 243 | ); | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | sub list_media_entries { | ||
| 248 | my ($self, %params) = @_; | ||
| 249 | $params{kind} = 'photo'; | ||
| 250 | |||
| 251 | my $user_id = delete $params{user_id}; | ||
| 252 | my $featured = delete $params{featured}; | ||
| 253 | |||
| 254 | croak "user_id may not be combined with featured" | ||
| 255 | if $user_id and $featured; | ||
| 256 | |||
| 257 | my $path; | ||
| 258 | $path = [ 'user', $user_id ] if $user_id; | ||
| 259 | $path = 'featured' if $featured; | ||
| 260 | $path ||= 'all'; | ||
| 261 | |||
| 262 | return $self->list_entries( | ||
| 263 | 'Net::Google::PicasaWeb::MediaEntry', | ||
| 264 | $path, | ||
| 265 | %params | ||
| 266 | ); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub list_photos { shift->list_media_entries(@_) } | ||
| 270 | sub list_videos { shift->list_media_entries(@_) } | ||
| 271 | |||
| 272 | |||
| 273 | sub get_media_entry { | ||
| 274 | my ($self, %params) = @_; | ||
| 275 | |||
| 276 | croak "missing album_id parameter" unless defined $params{album_id}; | ||
| 277 | croak "missing photo_id parameter" unless defined $params{photo_id}; | ||
| 278 | |||
| 279 | my $user_id = delete $params{user_id} || 'default'; | ||
| 280 | my $album_id = delete $params{album_id}; | ||
| 281 | my $photo_id = delete $params{photo_id}; | ||
| 282 | |||
| 283 | return $self->get_entry( | ||
| 284 | 'Net::Google::PicasaWeb::MediaEntry', | ||
| 285 | [ | ||
| 286 | user => $user_id, | ||
| 287 | albumid => $album_id, | ||
| 288 | photoid => $photo_id, | ||
| 289 | ], | ||
| 290 | %params | ||
| 291 | ); | ||
| 292 | } | ||
| 293 | |||
| 294 | sub get_photo { shift->get_media_entry(@_) } | ||
| 295 | sub get_video { shift->get_media_entry(@_) } | ||
| 296 | |||
| 297 | |||
| 298 | sub add_media_entry { | ||
| 299 | my ($self, %params) = @_; | ||
| 300 | |||
| 301 | my $user_id = delete $params{user_id} || 'default'; | ||
| 302 | my $album_id = delete $params{album_id} || 'default'; | ||
| 303 | my $data_type = delete $params{data_type} || 'image/jpeg'; | ||
| 304 | |||
| 305 | # Prepare Atom | ||
| 306 | my $twig = XML::Twig->new( | ||
| 307 | pretty_print => 'indented', | ||
| 308 | empty_tags => 'expand', | ||
| 309 | ); | ||
| 310 | |||
| 311 | my $root = XML::Twig::Elt->new( | ||
| 312 | 'entry' => { | ||
| 313 | 'xmlns' => 'http://www.w3.org/2005/Atom', | ||
| 314 | } | ||
| 315 | ); | ||
| 316 | |||
| 317 | $twig->set_root($root); | ||
| 318 | |||
| 319 | $root->insert_new_elt('last_child', title => {type => 'text'}, $params{title}); | ||
| 320 | $root->insert_new_elt('last_child', summary => {type => 'text'}, $params{summary}); | ||
| 321 | |||
| 322 | # TODO: | ||
| 323 | # <media:group> | ||
| 324 | # <media:keywords> | ||
| 325 | # keyword, keyword, ... | ||
| 326 | # </media:keywords> | ||
| 327 | # </media:group> | ||
| 328 | |||
| 329 | $root->insert_new_elt('last_child', | ||
| 330 | 'category' => { | ||
| 331 | 'scheme' => 'http://schemas.google.com/g/2005#kind', | ||
| 332 | 'term' => 'http://schemas.google.com/photos/2007#photo' | ||
| 333 | } | ||
| 334 | ); | ||
| 335 | |||
| 336 | # Prepare REST message | ||
| 337 | my $uri = $self->service_base_url . "user/$user_id/albumid/$album_id"; | ||
| 338 | my $request = HTTP::Request->new(POST => $uri, [$self->authenticator->auth_params, | ||
| 339 | 'Content-Type' => 'multipart/related', | ||
| 340 | 'MIME-version' => '1.0']); | ||
| 341 | $request->add_part(HTTP::Message->new(['Content-Type' => 'application/atom+xml'], $twig->sprint())); | ||
| 342 | $request->add_part(HTTP::Message->new(['Content-Type' => $data_type], $params{data})); | ||
| 343 | |||
| 344 | # Clear unneeded Twig | ||
| 345 | $twig->purge(); | ||
| 346 | |||
| 347 | my $response = $self->user_agent->request($request); | ||
| 348 | |||
| 349 | $request->clear(); | ||
| 350 | |||
| 351 | if ($response->is_error) { | ||
| 352 | croak $response->status_line; | ||
| 353 | } | ||
| 354 | |||
| 355 | # FIXME: Should be proper parser here | ||
| 356 | my @entries = $self->_parse_feed('Net::Google::PicasaWeb::MediaEntry', 'entry', $response->content); | ||
| 357 | return scalar $entries[0]; | ||
| 358 | } | ||
| 359 | |||
| 360 | *add_photo = *add_media_entry; | ||
| 361 | *add_video = *add_media_entry; | ||
| 362 | |||
| 363 | |||
| 364 | sub request { | ||
| 365 | my $self = shift; | ||
| 366 | my $method = shift; | ||
| 367 | my $path = shift; | ||
| 368 | my $query = ($method eq 'GET') ? shift : undef; | ||
| 369 | my $content = shift; | ||
| 370 | my $type = (($method eq 'POST') or ($method eq 'PUT')) ? shift : undef; | ||
| 371 | |||
| 372 | my @headers = $self->authenticator->auth_params; | ||
| 373 | |||
| 374 | my $url = $self->_feed_url($path, $query); | ||
| 375 | |||
| 376 | my $request; | ||
| 377 | { | ||
| 378 | local $_ = $method; | ||
| 379 | if (/GET/) { $request = GET ($url, @headers) } | ||
| 380 | elsif (/POST/) { $request = POST ($url, @headers, Content => $content, Content_Type => $type) } | ||
| 381 | elsif (/PUT/) { $request = PUT ($url, @headers, Content => $content, Content_Type => $type) } | ||
| 382 | elsif (/DELETE/) { $request = DELETE($url, @headers) } | ||
| 383 | else { confess "unknown method [$_]" } | ||
| 384 | } | ||
| 385 | |||
| 386 | return $self->user_agent->request($request); | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub get_entry { | ||
| 391 | my ($self, $class, $path, %params) = @_; | ||
| 392 | my $content = $self->_fetch_feed($path, %params); | ||
| 393 | my @entries = $self->_parse_feed($class, 'feed', $content); | ||
| 394 | return scalar $entries[0]; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _fetch_feed { | ||
| 398 | my ($self, $path, %params) = @_; | ||
| 399 | |||
| 400 | # Allow thumbsize to be passed as an array | ||
| 401 | if (defined $params{thumbsize} and ref $params{thumbsize}) { | ||
| 402 | $params{thumbsize} = join ',', @{ $params{thumbsize} }; | ||
| 403 | } | ||
| 404 | |||
| 405 | my $response = $self->request( GET => $path => [ %params ] ); | ||
| 406 | |||
| 407 | if ($response->is_error) { | ||
| 408 | croak $response->status_line; | ||
| 409 | } | ||
| 410 | |||
| 411 | return $response->content; | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _parse_feed { | ||
| 415 | my ($self, $class, $element, $content) = @_; | ||
| 416 | |||
| 417 | my @items; | ||
| 418 | my $feed = XML::Twig->new( | ||
| 419 | map_xmlns => $self->xml_namespaces, | ||
| 420 | twig_handlers => { | ||
| 421 | $element => sub { | ||
| 422 | push @items, $class->from_feed($self, $_); | ||
| 423 | }, | ||
| 424 | }, | ||
| 425 | ); | ||
| 426 | $feed->parse($content); | ||
| 427 | |||
| 428 | return @items; | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | sub list_entries { | ||
| 433 | my ($self, $class, $path, %params) = @_; | ||
| 434 | |||
| 435 | my $content = $self->_fetch_feed($path, %params); | ||
| 436 | return $self->_parse_feed($class, 'entry', $content); | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | __PACKAGE__->meta->make_immutable; | ||
| 442 | |||
| 443 | 1; | ||
| 444 | |||
| 445 | __END__ | ||