#!/usr/bin/perl
use warnings;
use strict;
$|++;
use Flickr::Upload;
use Flickr::API;
use Data::Dumper;

my $flickr_key = "***flickr key***";
my $flickr_secret = "***Secret***";
my $flickr_authtoken = "**flickr auth token... possibly unused***";

my $dir = shift;
opendir(DIR, $dir) or die $!;
my @files = grep { /jpg$/i && (!/sized/ && !/thumb/ && !/high/) } readdir(DIR);
closedir(DIR);

my $setname = ((split /\//, $dir)[-1]);

my $ua = Flickr::Upload->new({'key' => $flickr_key, 
                              'secret' => $flickr_secret});

my $set_id = 0;
foreach my $file ( @files ) {
  
  my $photo_id = $ua->upload( 
    'photo' => $dir.'/'.$file,
    'auth_token' => $flickr_authtoken, 
    'tags' => "galleryphp $setname"
  );
  unless ( $photo_id ) {
    warn  "failed on $dir/$file ";
    next;
  }

  if ( $set_id ) {
     my $result = $ua->execute_method('flickr.photosets.addPhoto', {
				 'auth_token' => $flickr_authtoken,
				   photoset_id => $set_id, 
				   photo_id => $photo_id
                                 });
  } else{ 
     my $result = $ua->execute_method('flickr.photosets.create', {
                                 'title' => $setname, 
				 'auth_token' => $flickr_authtoken,
				 'primary_photo_id' => $photo_id
                                 });
     $set_id = $result->{'tree'}->{'children'}->[1]->{'attributes'}->{'id'};
  } 

  print "uploaded $file ($photo_id) to $set_id\n";
}


syntax highlighted by Code2HTML, v. 0.9.1