Saturday, January 21, 2012

Download all of your images from Smugmug

I cheated and used wget to grab the files. This could be done using a perl module if you wanted.

You will need to have the WWW::SmugMug::API module installed for this script to work. All of the others should be standard stuff.




#!/opt/bin/perl

use warnings;
use strict;
use Data::Dumper;
use WWW::SmugMug::API;
use File::Path;

my $APIKey='your_key';
my $username='your_login_email';
my $password='your_password';

my $dest_folder='/share/jgarland/Photos/Smugmug';

my $sm_api = WWW::SmugMug::API->new(
 {
  sm_api_key => $APIKey,
  secure     => 0
 });

$sm_api->login_withPassword(
 {
  EmailAddress => $username,
  Password     => $password
 });

&get_images;

$sm_api->logout;
exit;





sub get_images {

 my $albums=$sm_api->albums_get();
 foreach my $album (@{$albums->{'Albums'}}) {
  my $images=$sm_api->images_get(
   {
    AlbumID => $album->{'id'},
    AlbumKey => $album->{'Key'}
   });
  foreach my $image (@{$images->{'Images'}}) {
   my $image_info=$sm_api->images_getInfo(
    {
     ImageID => $image->{'id'},
     ImageKey => $image->{'key'}
    });
   download($album, $image_info);
  }
 }
}


sub download {
 my $album=shift;
 my $image_info=shift;
 my $album_folder=$dest_folder . '/' . $album->{'Category'}->{'Name'} . '/' . $album->{'Title'};
 my $image_file=$album_folder . '/' . $image_info->{'Image'}->{'FileName'};

 unless(-d $album_folder) { mkpath $album_folder or die; }

 if ( -e $image_file ) { return(); }
 
 my $command='/usr/bin/wget -c -q \'' . $image_info->{'Image'}->{'OriginalURL'} . '\' -O "' . $image_file . '"';
 print "$command\n";
 my @results=`$command`;
 my $errlvl=$?;
 print Dumper(@results);
 if ( $errlvl == 0 ) {
  return();
 } else {
  unlink($image_file);
  die("errlvl=$errlvl\nDeleting $image_file\n");
 }

}