#!/usr/bin/env perl # # picasaweb - list or get Picasaweb albums # # $Id$ # use strict; use warnings; use Net::Google::PicasaWeb; use LWP::Protocol::https; # or the login will fail use Getopt::Std; my @valid_sizes = qw(94 110 128 200 220 288 320 400 512 576 640 720 800 912 1024 1152 1280 1440 1600); my $valid_sizes = join( ', ', @valid_sizes ); my %opt; getopts( 'hu:p:m:rlz', \%opt ); $opt{h} and HELP_MESSAGE(); ( $opt{l} || $opt{z} ) or die "Specify either -l or -z; specify -h for help\n."; ( !$opt{l} || !$opt{z} ) or die "Specify either -l or -z; specify -h for help\n."; sub HELP_MESSAGE { print STDERR < $opt{m} ) : (); my $username = $opt{u} // ask('Username'); my $password = $opt{p} // ask( 'Password', '*' ); $svc->login( $username, $password ) or die "Cannot log in as user '$username' with given password.\n"; my @albums = $svc->list_albums( user_id => $username ) or die "Cannot find any albums for user '$username': $!\n"; if (@ARGV) { if ( $opt{r} ) { my $arg_rx = join( '|', @ARGV ); $arg_rx = qr($arg_rx); @albums = grep { $_->title =~ /$arg_rx/ } @albums or die "No matching albums were found.\n"; } else { my %in_arg = map { $_ => 1 } @ARGV; @albums = grep { $in_arg{ $_->title } } @albums or die "None of the listed albums were found.\n"; } } if ($list) { list(@albums); } if ($zip) { get(@albums); } exit(0); #--- auxiliaries ---# # sub ask # slightly modified from http://www.perlmonks.org/?node_id=886306 { use Term::ReadKey; my ( $prompt, $repl ) = @_; my $pword; my $key; open( my $out_fh, '>', '/dev/tty' ) or die "Cannot write to tty: $!\n"; my $stdout = *STDOUT; select($out_fh); local $| = 1; # Turn off STDOUT buffering for immediate response print "$prompt: "; ReadMode 4; # Change to Raw Mode, disable Ctrl-C while (1) { while ( !defined( $key = ReadKey(-1) ) ) { } if ( $key =~ /^[\r\n]/ ) { # if Enter was pressed... print $key; # print a newline last; # and get out of here } print( $repl // $key ); $pword .= $key; } ReadMode 0; # Reset tty mode before exiting. <==IMPORTANT select($stdout); return $pword; } sub list { foreach my $album (@_) { my @photos = $album->list_media_entries(%list_opt); print scalar(@photos), ' images in ', $album->title, "\n"; foreach my $me (@photos) { my $content = $me->photo->content; printf( "%s in %s\n", identify($me), $album->title ); } } } sub get { use Archive::Zip; use URI::Escape; my $zip = Archive::Zip->new(); foreach my $album (@_) { foreach my $me ( $album->list_media_entries(%list_opt) ) { my $content = $me->photo->content; warn sprintf( "%s in %s\n", identify($me), $album->title ); my $member_name = join( '/', map { uri_escape($_) } ( $album->title, $me->title ) ); $zip->addString( $content->fetch(), $member_name ); } } $zip->writeToFileHandle(*STDOUT); } sub identify { my ($me) = @_; sprintf( '%s (%s, original %sx%s, also in %s)', $me->title, $me->photo->content->mime_type, $me->height, $me->width, join( ', ', map { sprintf( '%sx%s', $_->height, $_->width ) } $me->photo->thumbnails ) ); }