Created
July 12, 2015 16:35
-
-
Save hrpunio/484857dbe6affab4a4be to your computer and use it in GitHub Desktop.
Upload file, create album and list albums @ picasaweb via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# *** Upload file | Create album | List albums @ picasaweb via API *** | |
use strict; | |
use LWP::UserAgent; | |
use Getopt::Long; | |
use File::MimeInfo; | |
use XML::LibXML; | |
use open ':std', ':encoding(UTF-8)'; | |
my $profileID="default"; | |
my $AlbumId ="6170354040646469009"; ## default album | |
### ### ### ### ### ### #### ### ### ### ### ### | |
my $Action = 'u'; ## u | c | l (default is Upload) | |
my $AlbumTitle = ''; my $AlbumDescription = ''; my $AlbumKeywords = ''; | |
my $ActionUpload =''; my $ActionList = ''; my $ActionCreate = ''; | |
my $ImageFile = ''; | |
GetOptions( "upload" => \$ActionUpload, "list" => \$ActionList, "create" => \$ActionCreate, | |
"title=s" => \$AlbumTitle, "description=s" => \$AlbumDescription, "keywords=s" => \$AlbumKeywords, ## CreateAlbum | |
"file=s" => \$ImageFile, "album" => $AlbumId, ## UploadFile to Album | |
) ; | |
## Determine action | |
if ( $ActionUpload ) {$Action = 'u'} elsif ( $ActionList ) { $Action = 'l'} | |
elsif ( $ActionCreate ) { $Action = 'c'} | |
if ( $Action eq 'c' && ($AlbumTitle eq '' || $AlbumDescription eq '' || $AlbumKeywords eq '' )) { | |
print STDERR "*** USAGE: $0 -create -title Tytuł -descr Opis -keywords Keywords\n" ; exit; } | |
if ( $Action eq 'u' && (! -e $ImageFile )) { | |
print STDERR "*** USAGE: $0 -upload -file Plik [-album AlbumId]\n" ; | |
print STDERR "*** USAGE: $0 -create -title Tytuł -descr Opis -keywords Keywords\n" ; | |
print STDERR "*** USAGE: $0 -list\n" ; exit; | |
} | |
### ### ### #### | |
my $ACCESS_TOKEN=`oauth2picasa.py`; # get ACCESS_TOKEN (cf oauth2picasa.py gist) | |
print STDERR "*** AccessToken: $ACCESS_TOKEN\n"; | |
my $req ; my $blog_entry ; | |
my $picasawebURL = "https://picasaweb.google.com/data/feed/api/user/$profileID"; | |
if ( $Action eq 'c' ) { | |
$req = HTTP::Request->new(POST => $picasawebURL ); | |
$req->header( 'Content-Type' => 'application/atom+xml' ); | |
$blog_entry = "<entry xmlns='http://www.w3.org/2005/Atom' | |
xmlns:media='http://search.yahoo.com/mrss/' | |
xmlns:gphoto='http://schemas.google.com/photos/2007'>" | |
. "<title type='text'>$AlbumTitle</title>" | |
. "<summary type='text'>$AlbumDescription</summary>" | |
. "<media:group><media:keywords>$AlbumKeywords</media:keywords></media:group>" | |
. "<category scheme='http://schemas.google.com/g/2005#kind' | |
term='http://schemas.google.com/photos/2007#album'></category></entry>"; | |
$req->content($blog_entry); | |
} | |
elsif ( $Action eq 'l' ) { | |
$req = HTTP::Request->new(GET => $picasawebURL ); | |
} | |
elsif ( $Action eq 'u' ) {## Upload 1 photo | |
my $mimeType = mimetype($ImageFile); | |
## https://developers.google.com/picasa-web/docs/2.0/developers_guide_protocol | |
$req = HTTP::Request->new(POST => "$picasawebURL/albumid/$AlbumId" ); | |
$req->header( 'Content-Type' => "$mimeType" ); | |
$req->header( 'Slug' => "$ImageFile" ); | |
## http://www.perlmonks.org/?node_id=131584 | |
open(FILE, $ImageFile); | |
$req->content(join('',<FILE>)); | |
close(FILE); | |
} | |
$req->header( 'Authorization' => "Bearer $ACCESS_TOKEN" ); | |
$req->header( 'GData-Version' => '2' ); | |
## ### ### | |
my $ua = LWP::UserAgent->new; | |
my $res = $ua->request($req); | |
# Print message verbatim in case of problems | |
# http://www.perlmonks.org/bare/?node_id=464442 | |
# $ua->prepare_request($req); print($req->as_string); my $res; exit; | |
if ($res->is_success) { | |
my $decoded_response = $res->decoded_content; # or whatever | |
my $parser = XML::LibXML->new(); | |
my $doc ; eval { $doc = $parser->parse_string($decoded_response) }; | |
if ($@) { print STDERR "*** Error parsing XML $decoded_response\n"; next ;} | |
if ( $Action eq 'u' ) { | |
my $xpc = XML::LibXML::XPathContext->new( $doc->documentElement() ); | |
## http://stackoverflow.com/questions/4083550/why-does-xmllibxml-find-no-nodes-for-this-xpath-query-when-using-a-namespace | |
$xpc->registerNs( 'media', 'http://search.yahoo.com/mrss/'); | |
foreach my $node ( $xpc->findnodes('//media:title') ) { print ">> " . $node->textContent() . "\n"; } | |
foreach my $node ( $xpc->findnodes('(//media:content|//media:thumbnail)') ) { | |
print ">> " . $node->getAttribute('url') . "\n"; } | |
} | |
elsif ( $Action eq 'l' ) {### ListAlbums | |
my $xpc = XML::LibXML::XPathContext->new( $doc->documentElement() ); | |
$xpc->registerNs( 'dfl', 'http://www.w3.org/2005/Atom'); | |
print "******* WARNING: Only meaningfull titles are listed: *********\n"; | |
foreach my $node ( $xpc->findnodes('//dfl:entry') ) { | |
my $xpe = XML::LibXML::XPathContext->new( $node ); | |
$xpe->registerNs( 'dfl', 'http://www.w3.org/2005/Atom'); | |
my $title = ($xpe->findnodes('./dfl:title'))[0]->textContent(); | |
if ($title =~ /[^0-9\-\.]/ ) { | |
print ">> Album title: $title ## "; | |
foreach my $entry ( $xpe->findnodes('dfl:link') ) { | |
if ($entry->getAttribute('rel') eq 'alternate') { print $entry->getAttribute('href') . "\n"; } | |
} } | |
} | |
############# | |
} | |
else { | |
print "*** OK *** $decoded_response\n"; | |
} | |
} | |
else { die $res->status_line; } | |
## // koniec // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment