blib/lib/Net/Google/PicasaWeb.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 316 CC 23 SCAR 26.5
CC
23
Cov
0%
request
56.0
_feed_url
20.0
get_comment
20.0
CRAP 14.2
1 use warnings;
2 use strict;
3  
4 package Net::Google::PicasaWeb;
5 use Moose;
6  
7 use Carp;
8 use HTTP::Message;
9 use HTTP::Request::Common;
10 use HTTP::Request;
11 use LWP::UserAgent;
12 use Net::Google::AuthSub;
13 use URI;
14 use XML::Twig;
15  
16 use Net::Google::PicasaWeb::Album;
17 use Net::Google::PicasaWeb::Comment;
18 use Net::Google::PicasaWeb::MediaEntry;
19  
20 =head1 NAME
21  
22 Net::Google::PicasaWeb - use Google's Picasa Web API
23  
24 =head1 VERSION
25  
26 Version 0.06_01
27  
28 B<DEVELOPMENT VERSION:> The update features of this API are EXPERIMENTAL. They may change in a future release. They are not well-tested (there are not even unit tests as of this writing).
29  
30 =cut
31  
32 our $VERSION = '0.06_01';
33  
34 =head1 SYNOPSIS
35  
36 use Net::Google::PicasaWeb;
37  
38 my $service = Net::Google::PicasaWeb->new;
39  
40 # Login via one of these
41 $service->login('jondoe@gmail.com', 'north23AZ');
42  
43 # Working with albums (see Net::Google::PicasaWeb::Album)
44 my @albums = $service->list_albums( user_id => 'jondoe');
45 $album->title('Quick Trip To Italy');
46  
47 # Listing photos (see Net::Google::PicasaWeb::MediaEntry)
48 my @photos = $album->list_media_entries;
49 my @recent = $album->list_media_entries( max_results => 10 );
50 my @puppies = $album->list_media_entries( q => 'puppies' );
51 my @all_puppies = $service->list_media_entries( q => 'puppies' );
52  
53 # Updating/Deleting photos (or video)
54 $photo->title('Plz to love RealCat');
55  
56 # Listing tags
57 my @user_tags = $service->list_tags( user_id => 'jondoe' );
58 my @album_tags = $album->list_tags;
59 my @photo_tags = $photo->list_tags;
60  
61 # Listing comments (see Net::Google::PicasaWeb::Comment)
62 my @recent = $service->list_comments( user_id => 'jondoe', max_results => 10 );
63 my @photo_comments = $photo->list_comments;
64  
65 =head1 ATTRIBUTES
66  
67 This module uses L<Moose> to handle attributes and such. These attributes are readable, writable, and may be passed to the constructor unless otherwise noted.
68  
69 =head2 authenticator
70  
71 This is an L<Net::Google::AuthSub> object used to handle authentication. The default is an instance set to use a service of "lh2" and a source of "Net::Google::PicasaWeb-VERSION".
72  
73 =cut
74  
75 has authenticator => (
76 is => 'rw',
77 isa => 'Net::Google::AuthSub',
78 default => sub {
79 Net::Google::AuthSub->new(
80 service => 'lh2', # Picasa Web Albums
81 source => 'Net::Google::PicasaWeb-'.$VERSION,
82 );
83 },
84 );
85  
86 =head2 user_agent
87  
88 This is an L<LWP::UserAgent> object used to handle web communication.
89  
90 =cut
91  
92 has user_agent => (
93 is => 'rw',
94 isa => 'LWP::UserAgent',
95 default => sub {
96 LWP::UserAgent->new(
97 cookie_jar => {},
98 );
99 },
100 );
101  
102 =head1 METHODS
103  
104 =head2 new
105  
106 my $service = Net::Google::PicasaWeb->new(%params);
107  
108 See the L</ATTRIBUTES> section for a list of possible parameters.
109  
110 =head2 login
111  
112 my $success = $service->login($username, $password, %options);
113  
114 This is a shortcut for performing:
115  
116 $service->authenticator->login($username, $password, %options);
117  
118 It has some additional error handling. This method will return a true value on success or die on error.
119  
120 See L<Net::Google::AuthSub>.
121  
122 =cut
123  
124 sub login {
125 my $self = shift;
126 my $response = $self->authenticator->login(@_);
127  
128 croak "error logging in: $@" unless defined $response;
129 croak "error logging in: ", $response->error unless $response->is_success;
130  
131 return 1;
132 }
133  
134 =head2 list_albums
135  
136 my @albums = $service->list_albums(%params);
137  
138 This will list a set of albums available from Picasa Web Albums. If no C<%params> are set, then this will list the albums belonging to the authenticated user. If the user is not authenticated, this will probably not return anything. Further control is gained by specifying one or more of the following parameters:
139  
140 =over
141  
142 =item user_id
143  
144 This is the user ID to request a list of albums from. The defaults to "default", which lists those belonging to the current authenticated user.
145  
146 =back
147  
148 This method also takes the L</STANDARD LIST OPTIONS>.
149  
150 =cut
151  
152 sub list_albums {
153 my ($self, %params) = @_;
154 $params{kind} = 'album';
155  
156 my $user_id = delete $params{user_id} || 'default';
157 return $self->list_entries(
158 'Net::Google::PicasaWeb::Album',
159 [ 'user', $user_id ],
160 %params
161 );
162 }
163  
164 =head2 get_album
165  
166 my $album = $service->get_album(
167 user_id => 'hanenkamp',
168 album_id => '5143195220258642177',
169 );
170  
171 This will fetch a single album from the Picasa Web Albums using the given C<user_id> and C<album_id>. If C<user_id> is omitted, then "default" will be used instead.
172  
173 This method returns C<undef> if no such album exists.
174  
175 =cut
176  
177 sub get_album {
178 my ($self, %params) = @_;
179  
180 croak "missing album_id parameter" unless defined $params{album_id};
181  
182 my $user_id = delete $params{user_id} || 'default';
183 my $album_id = delete $params{album_id};
184  
185 return $self->get_entry(
186 'Net::Google::PicasaWeb::Album',
187 [ 'user', $user_id, 'albumid', $album_id ],
188 %params
189 );
190 }
191  
192 =head2 add_album
193  
194 http://code.google.com/intl/en-US/apis/picasaweb/developers_guide_protocol.html#AddAlbums
195  
196 my $album = $service->add_album(
197 title => 'Trip to Italy',
198 summary => 'This was the recent trip I took to Italy',
199 location => 'Italy',
200 access => 'public',
201 commentingEnabled => 'true',
202 timestamp => '1152255600000',
203 keywords => ('italy', 'vacation'),
204 );
205  
206 =over
207  
208 =item title
209  
210 Title of the new album.
211  
212 =back
213  
214 Default values will be applied by PicasaWeb on server side.
215  
216 =cut
217  
218 sub add_album {
219 my ($self, %params) = @_;
220  
221 my $twig = XML::Twig->new(
222 pretty_print => 'indented',
223 empty_tags => 'expand',
224 );
225  
226 my $root = XML::Twig::Elt->new(
227 'entry' => {
228 'xmlns' => 'http://www.w3.org/2005/Atom',
229 'xmlns:media' => 'http://search.yahoo.com/mrss/',
230 'xmlns:gphoto' => 'http://schemas.google.com/photos/2007',
231 }
232 );
233  
234 $twig->set_root($root);
235  
236 $root->insert_new_elt('last_child', title => {type => 'text'}, $params{title});
237 $root->insert_new_elt('last_child', summary => {type => 'text'}, $params{summary});
238  
239 foreach my $gphoto ('location', 'access', 'commentingEnabled', 'timestamp') {
240 $root->insert_new_elt('last_child', 'gphoto:' . $gphoto, $params{$gphoto});
241 }
242  
243 my $group = $root->insert_new_elt('last_child', 'media:group');
244  
245 if (defined $params{keywords}) {
246 $group->insert_new_elt('last_child', 'media:keywords', join(', ', $params{keywords}));
247 }
248  
249 $root->insert_new_elt('last_child',
250 'category' => {
251 'scheme' => 'http://schemas.google.com/g/2005#kind',
252 'term' => 'http://schemas.google.com/photos/2007#album'
253 }
254 );
255  
256 my $uri = 'http://picasaweb.google.com/data/feed/api/user/default';
257 my $response = $self->request('POST', $uri, $twig->sprint(), 'application/atom+xml');
258  
259 $twig->purge();
260  
261 if ($response->is_error) {
262 croak $response->status_line;
263 }
264  
265 my @entries = $self->_parse_feed('Net::Google::PicasaWeb::Album', 'entry', $response->content);
266 return scalar $entries[0];
267 }
268  
269 =head2 list_tags
270  
271 Returns a list of tags that have been used by the logged user or the user named in the C<user_id> parameter.
272  
273 This method accepts this parameters:
274  
275 =over
276  
277 =item user_id
278  
279 The ID of the user to find tags for. Defaults to the current user.
280  
281 =back
282  
283 This method also takes all the L</STANDARD LIST OPTIONS>.
284  
285 =cut
286  
287 # This is a tiny cheat that allows us to reuse the list_entries method
288 {
289 package Net::Google::PicasaWeb::Tag;
290  
291 sub from_feed {
292 my ($class, $service, $entry) = @_;
293 return $entry->field('title');
294 }
295 }
296  
297 sub list_tags {
298 my ($self, %params) = @_;
299 $params{kind} = 'tag';
300  
301 my $user_id = delete $params{user_id} || 'default';
302 return $self->list_entries(
303 'Net::Google::PicasaWeb::Tag',
304 [ 'user', $user_id ],
305 %params
306 );
307 }
308  
309 =head2 list_comments
310  
311 Returns comments on photos for the current account or the account given by the C<user_id> parameter.
312  
313 It accepts the following parameters:
314  
315 =over
316  
317 =item user_id
318  
319 This is the ID of the user to search for comments within. The comments returned will be commons on photos owned by this user. The default is to search the comments of the authenticated user.
320  
321 =back
322  
323 This method also accepts the L</STANDARD LIST OPTIONS>.
324  
325 =cut
326  
327 sub list_comments {
328 my ($self, %params) = @_;
329 $params{kind} = 'comment';
330  
331 my $user_id = delete $params{user_id} || 'default';
332 return $self->list_entries(
333 'Net::Google::PicasaWeb::Comment',
334 [ 'user', $user_id ],
335 %params
336 );
337 }
338  
339 sub _feed_url {
340 my ($self, $path, $query) = @_;
341  
342 $path = join '/', @$path if ref $path;
343 $path = 'http://picasaweb.google.com/data/feed/api/' . $path
344 unless $path =~ m{^https?:};
345  
346 my $uri = URI->new($path);
347 $uri->query_form($query) if $query;
348  
349 return $uri;
350 }
351  
352 =head2 get_comment
353  
354 my $comment = $service->get_comment(
355 user_id => $user_id,
356 album_id => $album_id,
357 photo_id => $photo_id,
358 comment_id => $comment_id,
359 );
360  
361 Retrieves a single comment from Picasa Web via the given C<user_id>, C<album_id>, C<photo_id>, and C<comment_id>. If C<user_id> is not given, "default" will be used.
362  
363 Returns C<undef> if no matching comment is found.
364  
365 =cut
366  
367 sub get_comment {
368 my ($self, %params) = @_;
369  
370 croak "missing album_id parameter" unless defined $params{album_id};
371 croak "missing photo_id parameter" unless defined $params{photo_id};
372 croak "missing comment_id parameter" unless defined $params{comment_id};
373  
374 my $user_id = delete $params{user_id} || 'default';
375 my $album_id = delete $params{album_id};
376 my $photo_id = delete $params{photo_id};
377 my $comment_id = delete $params{comment_id};
378  
379 return $self->get_entry(
380 'Net::Google::PicasaWeb::Comment',
381 [
382 user => $user_id,
383 albumid => $album_id,
384 photoid => $photo_id,
385 commentid => $comment_id,
386 ],
387 %params
388 );
389 }
390  
391 =head2 list_media_entries
392  
393 =head2 list_photos
394  
395 =head2 list_videos
396  
397 Returns photos and videos based on the query options given. If a C<user_id> option is set, the photos returned will be those related to the named user ID. Without a user ID, the photos will be pulled from the general community feed.
398  
399 It accepts the following parameters:
400  
401 =over
402  
403 =item user_id
404  
405 If given, the photos will be limited to those owned by this user. If it is set to "default", then the authenticated user will be used. If no C<user_id> is set, then the community feed will be used rather than a specific user.
406  
407 =back
408  
409 This method also accepts the L</STANDARD LIST OPTIONS>.
410  
411 The L</list_photos> and L</list_videos> methods are synonyms for L</list_media_entries>.
412  
413 =cut
414  
415 sub list_media_entries {
416 my ($self, %params) = @_;
417 $params{kind} = 'photo';
418  
419 my $user_id = delete $params{user_id};
420 my $path = [ 'user', $user_id ] if $user_id;
421 $path ||= 'all';
422  
423 return $self->list_entries(
424 'Net::Google::PicasaWeb::MediaEntry',
425 $path,
426 %params
427 );
428 }
429  
430 *list_photos = *list_media_entries;
431 *list_videos = *list_media_entries;
432  
433 =head2 get_media_entry
434  
435 =head2 get_photo
436  
437 =head2 get_video
438  
439 my $media_entry = $service->get_media_entry(
440 user_id => $user_id,
441 album_id => $album_id,
442 photo_id => $photo_id,
443 );
444  
445 Returns a specific photo or video entry when given a C<user_id>, C<album_id>, and C<photo_id>. If C<user_id> is not given, "default" will be used.
446  
447 If no such photo or video can be found, C<undef> will be returned.
448  
449 =cut
450  
451 sub get_media_entry {
452 my ($self, %params) = @_;
453  
454 croak "missing album_id parameter" unless defined $params{album_id};
455 croak "missing photo_id parameter" unless defined $params{photo_id};
456  
457 my $user_id = delete $params{user_id} || 'default';
458 my $album_id = delete $params{album_id};
459 my $photo_id = delete $params{photo_id};
460  
461 return $self->get_entry(
462 'Net::Google::PicasaWeb::MediaEntry',
463 [
464 user => $user_id,
465 albumid => $album_id,
466 photoid => $photo_id,
467 ],
468 %params
469 );
470 }
471  
472 *get_photo = *get_media_entry;
473 *get_video = *get_media_entry;
474  
475 =head2 add_media_entry
476  
477 =head2 add_photo
478  
479 =head2 add_video
480  
481 my $media_entry = $service->add_media_entry(
482 user_id => $user_id,
483 album_id => $album_id,
484 title => $title,
485 summary => $summary,
486 keywords => ($keyword, $keyword, ),
487 data => $binary,
488 data_type => $content_type,
489 );
490  
491 =cut
492  
493 sub add_media_entry {
494 my ($self, %params) = @_;
495  
496 my $user_id = $params{user_id} || 'default';
497 my $album_id = $params{album_id} || 'default';
498 my $data_type = $params{data_type} || 'image/jpeg';
499  
500 # Prepare Atom
501 my $twig = XML::Twig->new(
502 pretty_print => 'indented',
503 empty_tags => 'expand',
504 );
505  
506 my $root = XML::Twig::Elt->new(
507 'entry' => {
508 'xmlns' => 'http://www.w3.org/2005/Atom',
509 }
510 );
511  
512 $twig->set_root($root);
513  
514 $root->insert_new_elt('last_child', title => {type => 'text'}, $params{title});
515 $root->insert_new_elt('last_child', summary => {type => 'text'}, $params{summary});
516  
517 # TODO:
518 # <media:group>
519 # <media:keywords>
520 # keyword, keyword, ...
521 # </media:keywords>
522 # </media:group>
523  
524 $root->insert_new_elt('last_child',
525 'category' => {
526 'scheme' => 'http://schemas.google.com/g/2005#kind',
527 'term' => 'http://schemas.google.com/photos/2007#photo'
528 }
529 );
530  
531 # Prepare REST message
532 my $uri = "http://picasaweb.google.com/data/feed/api/user/$user_id/albumid/$album_id";
533 my $request = HTTP::Request->new(POST => $uri, [$self->authenticator->auth_params,
534 'Content-Type' => 'multipart/related',
535 'MIME-version' => '1.0']);
536 $request->add_part(HTTP::Message->new(['Content-Type' => 'application/atom+xml'], $twig->sprint()));
537 $request->add_part(HTTP::Message->new(['Content-Type' => $data_type], $params{data}));
538  
539 # Drain cache (workaround)
540 $request->_content($request);
541  
542 # Clear unneeded Twig
543 $twig->purge();
544  
545 my $response = $self->user_agent->request($request);
546  
547 $request->clear();
548  
549 if ($response->is_error) {
550 croak $response->status_line;
551 }
552  
553 # FIXME: Should be proper parser here
554 my @entries = $self->_parse_feed('Net::Google::PicasaWeb::MediaEntry', 'entry', $response->content);
555 return scalar $entries[0];
556 }
557  
558 *add_photo = *add_media_entry;
559 *add_video = *add_media_entry;
560  
561 =head1 HELPERS
562  
563 These helper methods are used to do some of the work.
564  
565 =head2 request
566  
567 my $response = $service->request($method, $path, $query, $content);
568  
569 This handles the details of making a request to the Google Picasa Web API.
570  
571 =cut
572  
573 sub request {
574 my $self = shift;
575 my $method = shift;
576 my $path = shift;
577 my $query = ($method eq 'GET') ? shift : undef;
578 my $content = shift;
579 my $type = (($method eq 'POST') or ($method eq 'PUT')) ? shift : undef;
580  
581 my @headers = $self->authenticator->auth_params;
582  
583 my $url = $self->_feed_url($path, $query);
584
585 my $request;
586 {
587 local $_ = $method;
588 if (/GET/) { $request = GET ($url, @headers) }
589 elsif (/POST/) { $request = POST ($url, @headers, Content => $content, Content_Type => $type) }
590 elsif (/PUT/) { $request = PUT ($url, @headers, Content => $content, Content_Type => $type) }
591 elsif (/DELETE/) { $request = DELETE($url, @headers) }
592 else { confess "unknown method [$_]" }
593 }
594  
595 return $self->user_agent->request($request);
596 }
597  
598 =head2 get_entry
599  
600 my $entry = $service->get_entry($class, $path, %params);
601  
602 This is used by the C<get_*> methods to pull and initialize a single object from Picasa Web.
603  
604 =cut
605  
606 sub get_entry {
607 my ($self, $class, $path, %params) = @_;
608 my $content = $self->_fetch_feed($path, %params);
609 my @entries = $self->_parse_feed($class, 'feed', $content);
610 return scalar $entries[0];
611 }
612  
613 sub _fetch_feed {
614 my ($self, $path, %params) = @_;
615  
616 my $response = $self->request( GET => $path => [ %params ] );
617  
618 if ($response->is_error) {
619 croak $response->status_line;
620 }
621  
622 return $response->content;
623 }
624  
625 sub _parse_feed {
626 my ($self, $class, $element, $content) = @_;
627  
628 my @items;
629 my $feed = XML::Twig->new(
630 map_xmlns => {
631 'http://search.yahoo.com/mrss/' => 'media',
632 'http://schemas.google.com/photos/2007' => 'gphoto',
633 },
634 twig_handlers => {
635 $element => sub {
636 push @items, $class->from_feed($self, $_);
637 },
638 },
639 );
640 $feed->parse($content);
641  
642 return @items;
643 }
644  
645 =head2 list_entries
646  
647 my @entries = $service->list_entries($class, $path, %params);
648  
649 This is used by the C<list_*> methods to pull and initialize lists of objects from feeds.
650  
651 =cut
652  
653 sub list_entries {
654 my ($self, $class, $path, %params) = @_;
655  
656 my $content = $self->_fetch_feed($path, %params);
657 return $self->_parse_feed($class, 'entry', $content);
658 }
659  
660  
661 =head1 STANDARD LIST OPTIONS
662  
663 Several of the listing methods return entries that can be modified by setting the following options.
664  
665 =over
666  
667 =item access
668  
669 This is the L<http://code.google.com/apis/picasaweb/reference.html#Visibility|visibility value> to limit the returned results to.
670  
671 =item thumbsize
672  
673 This option is only used when listing albums and photos or videos.
674  
675 By passing a single scalar or an array reference of scalars, you may select the size or sizes of thumbnails attached to the items returned. Please see the L<http://code.google.com/apis/picasaweb/reference.html#Parameters|parameters> documentation for a description of valid values.
676  
677 =item imgmax
678  
679 This option is only used when listing albums and photos or videos.
680  
681 This is a single scalar selecting the size of the main image to return with the items found. Please see the L<http://code.google.com/apis/picasaweb/reference.html#Parameters|parameters> documentation for a description of valid values.
682  
683 =item tag
684  
685 This option is only used when listing albums and photos or videos.
686  
687 This is a tag name to use to filter the items returned.
688  
689 =item q
690  
691 This is a full-text query string to filter the items returned.
692  
693 =item max_results
694  
695 This is the maximum number of results to be returned.
696  
697 =item start_index
698  
699 This is the 1-based index of the first result to be returned.
700  
701 =item bbox
702  
703 This option is only used when listing albums and photos or videos.
704  
705 This is the bounding box of geo coordinates to search for items within. The coordinates are given as an array reference of exactly 4 values given in the following order: west, south, east, north.
706  
707 =item l
708  
709 This option is only used when listing albums and photos or videos.
710  
711 This may be set to the name of a geo location to search for items within. For example, "London".
712  
713 =back
714  
715 =head1 AUTHOR
716  
717 Andrew Sterling Hanenkamp, C<< <hanenkamp at cpan.org> >>
718  
719 =head1 BUGS
720  
721 Please report any bugs or feature requests to C<bug-net-google-photos at rt.cpan.org>, or through
722 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Google-PicasaWeb>. I will be notified, and then you'll
723 automatically be notified of progress on your bug as I make changes.
724  
725 =head1 SUPPORT
726  
727 You can find documentation for this module with the perldoc command.
728  
729 perldoc Net::Google::PicasaWeb
730  
731 You can also look for information at:
732  
733 =over 4
734  
735 =item * RT: CPAN's request tracker
736  
737 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Google-PicasaWeb>
738  
739 =item * AnnoCPAN: Annotated CPAN documentation
740  
741 L<http://annocpan.org/dist/Net-Google-PicasaWeb>
742  
743 =item * CPAN Ratings
744  
745 L<http://cpanratings.perl.org/d/Net-Google-PicasaWeb>
746  
747 =item * Search CPAN
748  
749 L<http://search.cpan.org/dist/Net-Google-PicasaWeb>
750  
751 =back
752  
753 =head1 ACKNOWLEDGEMENTS
754  
755 Thanks to:
756  
757 =over
758  
759 =item *
760  
761 Robert May for responding to email messages quickly and transfering ownership of the C<Net::Google::PicasaWeb> namespace and providing some sample code to examine.
762  
763 =item *
764  
765 Simon Wistow for L<Net::Google::AuthSub>, which took care of all the authentication details.
766  
767 =back
768  
769 =head1 COPYRIGHT & LICENSE
770  
771 Copyright 2008 Andrew Sterling Hanenkamp
772  
773 This program is free software; you can redistribute it and/or modify it
774 under the same terms as Perl itself.
775  
776 =cut
777  
778 1; # End of Net::Google::PicasaWeb