Created
December 7, 2011 21:12
-
-
Save pung96/1444684 to your computer and use it in GitHub Desktop.
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
username: 'pung96a@gmail.com' | |
password: '' | |
album_id: '' |
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/env perl | |
use strict; | |
use warnings; | |
use LWP::UserAgent; | |
use YAML qw( LoadFile ); | |
use File::Basename; | |
my $config = LoadFile($ENV{HOME}.'/.google'); | |
my $user = $config->{username} or die "No Username"; | |
my $pass = $config->{password} or die "No Password"; | |
my $user_id = $config->{user_id} || 'default'; | |
my $album_id = $config->{album_id} || 'default'; | |
my @files = @ARGV; | |
#========================================== | |
# Google Auth with ClientLogin | |
#========================================== | |
my $agent = LWP::UserAgent->new(cookie_jar => {}); | |
my $res = $agent->post( 'https://www.google.com/accounts/ClientLogin', | |
'Content-type' => 'application/x-www-form-urlencoded', | |
Content=>[ | |
accountType=>'HOSTED_OR_GOOGLE', | |
Email => $user, | |
Passwd => $pass, | |
service => 'lh2', | |
source => 'my-picasauploader-0.1', | |
] | |
); | |
# Check Auth status | |
unless ($res->is_success) { | |
die "ERROR: Login failed: ".$res->status_line."\n"; | |
} | |
# Get a token | |
my %auth_params = split /=|\n/, $res->decoded_content; | |
#use Data::Dumper; | |
#DEBUG | |
#print $album_id,"\n"; | |
#print Dumper \%auth_params; | |
#========================================== | |
# Upload files | |
#========================================== | |
my $post_url = 'https://picasaweb.google.com/data/feed/api/user/'.$user_id.'/albumid/'.$album_id; | |
for my $file ( @files ){ | |
# Load image files | |
my $lfile = basename($file); | |
my $content = LoadBinary( $file ); | |
# Upload | |
my %params = ( | |
Authorization => qq/GoogleLogin auth="$auth_params{Auth}"/, | |
Content_Type => 'image/jpeg', | |
Slug => $lfile, | |
Content => $content, | |
); | |
my $res = $agent->post( $post_url, %params ); | |
unless ( $res->is_success ){ | |
die "ERROR: ", $res->status_line, "\n"; | |
} | |
# Get a link | |
my ($link) = ($res->decoded_content=~/content type.*?src='(.*?)'/gsm); | |
print $link, "\n"; | |
} | |
#========================================== | |
# SUB : LoadBinary | |
#========================================== | |
sub LoadBinary{ | |
my $file = shift; | |
open my $FH, '<', $file or die; | |
binmode $FH; | |
my $content; | |
my $buff; | |
while( read $FH, $buff, 8*2**10 ){ | |
$content .= $buff; | |
} | |
close $FH; | |
return $content; | |
} |
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/env perl | |
use strict; | |
use warnings; | |
use Net::Google::AuthSub; | |
use LWP::UserAgent; | |
use YAML qw( LoadFile ); | |
my $config = LoadFile($ENV{HOME}.'/.google.conf'); | |
my $user = $config->{username} or die "No Username"; | |
my $pass = $config->{password} or die "No Password"; | |
my $user_id = $config->{user_id} || 'default'; | |
my $album_id = $config->{album_id} || 'default'; | |
my @files = @ARGV; | |
# Auth | |
my $google_auth = Net::Google::AuthSub->new( | |
service => 'lh2', | |
source => 'my-picasauploader-0.1', | |
); | |
my $response = $google_auth->login($user, $pass); | |
unless ($response->is_success) { | |
die "ERROR: Login failed: ".$response->error."\n"; | |
} | |
use Data::Dumper; | |
print Dumper {$google_auth->auth_params}; | |
# Upload files | |
my $agent = LWP::UserAgent->new(cookie_jar => {}); | |
my $post_url = 'https://picasaweb.google.com/data/feed/api/user/'.$user_id.'/albumid/'.$album_id; | |
for my $file ( @files ){ | |
# Load image files | |
my $content = LoadBinary( $file ); | |
# Upload | |
my %params = ( | |
$google_auth->auth_params, | |
Content_Type => 'image/jpeg', | |
Slug => $file, | |
Content => $content, | |
); | |
my $res = $agent->post( $post_url, %params ); | |
unless ( $res->is_success ){ | |
die "ERROR: ", $res->status_line, "\n"; | |
} | |
# Get a link | |
my ($link) = ($res->decoded_content=~/content type.*?src='(.*?)'/gsm); | |
print $link, "\n"; | |
} | |
sub LoadBinary{ | |
my $file = shift; | |
open my $FH, '<', $file or die; | |
binmode $FH; | |
my $content; | |
my $buff; | |
while( read $FH, $buff, 8*2**10 ){ | |
$content .= $buff; | |
} | |
close $FH; | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment