Wednesday, 12 March 2008

flickr -> photobox transfer

SWMBO asked me if I could help transfer some of our flickr photos to photobox so that she can create and print an album. A quick Google seemed to find a few links for doing it the other way around, so it seemed like time to get knitting some perl!

Flickr has an API and photobox lets you use ftp to upload, so I thought I'd use the perl API to download the 4 sets that my wife had arranged on flickr. Anyway, here's the code with keys etc obscured. Sorry it's pretty rough and my perl is not too hot, but at least it works.


use Flickr::API;
use Data::Dumper;

my $api = new Flickr::API({'key' => 'your_flickr_api_key',
'secret' => 'your flickr_secret'});


my $frob = $api->execute_method('flickr.auth.getFrob', {
'api_key' => 'your_flickr_api_key',
});
my $f = $frob->{tree}->{children}[1]->{children}[0]->{content};

my $url = $api->request_auth_url("read", $f);
my $cmd = "C:/Progra~1/Mozill~1/firefox.exe \"$url\"";
system($cmd);
sleep(3);

my $token = $api->execute_method('flickr.auth.getToken', {
'api_key' => 'your_flickr_api_key',
'frob' => $f,
});

my $t = $token->{tree}->{children}[1]->{children}[1]->{children}[0]->{content};

my $response = $api->execute_method('flickr.photosets.getPhotos', {
'photoset_id' => '72157601397952579',
'auth_token' => $t,
});

my $ids = $response->{tree}->{children}[1]->{children};

for my $id (@$ids) {
if ($id->{attributes}) {
my $photo_id = $id->{attributes}->{id};
print "$photo_id\n";
my $sizes = $api->execute_method('flickr.photos.getSizes', {
'api_key' => 'your_flickr_api_key',
'photo_id' => $photo_id,
'auth_token' => $t,
});

my $s = $sizes->{tree}->{children}[1]->{children};
for my $size (@$s) {
if ($size->{attributes}) {
if ($size->{attributes}->{label} eq "Original") {
my $url = $size->{attributes}->{source};
print "$url\n";
my $c = "wget $url";
system($c);
}
}
}
}
}

4 comments:

Jen said...

This looks great! SOme of the code is obscured though (goes off the page) - do you have a link to it in a text file or something?

Cheers!
Jen

Adrian Smith said...

I've chosen a new template.

swede said...

Hi Adrian,

Just writing from photobox with an update to your blog post and tips that I found searching Google for printing Flickr photos with Photobox.

We've just launched functionality using the Flickr API to give us access to high res Flickr photos and print them.

Info is available on our site at www.photobox.co.uk/content/flickr-how-it-works.

Hope this is useful. Ben (at Photobox)

martin said...

Thankyou very much! I'm coding a backup script and this was very useful :)