Skip to content

Instantly share code, notes, and snippets.

@mlbright
Last active December 11, 2015 16:58
Show Gist options
  • Select an option

  • Save mlbright/4631114 to your computer and use it in GitHub Desktop.

Select an option

Save mlbright/4631114 to your computer and use it in GitHub Desktop.
upload all pictures from a given directory to Flickr, and then to Picasa Web Albums, using your secret email upload address.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Flickr::Upload;
use File::Spec;
use File::Find;
use Config::Simple;
use Path::Class;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;
use Email::MIME;
use File::Slurp;
# your perl application name
my ( $volume, $directories, $app_title ) = File::Spec->splitpath($0);
my $cfg = Config::Simple->new('buploader.cfg');
my $from = $cfg->param('gmail_from');
my $dest = $cfg->param('picasa_private_email'); # picasa private upload email
my $transport = Email::Sender::Transport::SMTP->new(
{
host => 'smtp.gmail.com',
ssl => 1,
sasl_username => $cfg->param('gmail_from'),
sasl_password => $cfg->param('gmail_password'),
}
);
# flickr api authentication credentials
my $auth_key = $cfg->param('auth_key');
my $auth_secret = $cfg->param('auth_secret');
# this will require application authorization to obtain a proper token.
# Use the 'flickr_auth_token.pl' script.
my $auth_token = $cfg->param('auth_token');
# directory path of files to upload
my $dir = dir( $cfg->param('upload_dir') );
# optional tags
my $tags = "batch";
#
# We assume friends and family are OK, but you can
# define public-view permissions here.
#
# 0 = private;
# 1 = public;
#
my $public = 0;
my $ua =
Flickr::Upload->new( { 'key' => $auth_key, 'secret' => $auth_secret } );
$ua->agent($app_title);
open my $processed, '>>', "processed.txt";
upload_folder( $dir, $tags, $public );
sub upload {
my ( $file, $tags, $public ) = @_;
my $photo_id = $ua->upload(
'photo' => $file,
'auth_token' => $auth_token,
'tags' => $tags,
'is_public' => $public,
'is_family' => 1,
'is_friend' => 0,
);
if ( defined($photo_id) ) {
say "flickr: $photo_id => " . file($file);
}
else {
say "flickr: Could not upload " . file($file);
}
my $email = Email::MIME->create(
header_str => [
From => $from,
To => $dest,
Subject => 'uploads',
],
);
my $image_bin = read_file $file, binmode => ':raw';
my @parts = (
Email::MIME->create(
attributes => {
filename => "$file",
content_type => "image/jpeg",
encoding => "base64",
name => "$file",
disposition => 'attachment',
},
body => $image_bin,
),
);
$email->parts_set( \@parts );
my $result = sendmail( $email, { transport => $transport } );
if ($result) {
say "sent to Picasa Web Albums...";
}
else {
say "could not send to Picasa Web Albums";
}
say $processed $file;
}
sub upload_folder {
my ( $dir, $tags, $public ) = @_;
find(
sub {
upload( $File::Find::name, $tags, $public )
if ( -f $_ && $_ =~ /^.+\.(jpg|jpeg)$/i );
},
$dir
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment