My lovely friends Danny and Jess got married recently. I took some photos and so did lots of other people. So we put them all in a flickr group. But they wanted to get hold of everyone's pics to print or whatever. Problem. For some reason flickr have seen fit to disable the API key for the one tool that (Google says) could do the job of grabbing all the pics for us. What a pain!
So, what's a perl geek to do? Build my own. Here's the code, we download all the pics from a flickr group using the flickr API. Please bear in mind that before you go downloading lots of images you need to check that that's OK with the copyright holders. You’ve been warned.
!/usr/bin/perl
use strict; use warnings; use Flickr::API; use Data::Dumper; use LWP::UserAgent;
my $photos; my $dump_directory='~/danny_jess_pics'; my $poolsize = 1000; my $per_page = 250; # Have to do <=500 at a time. my $page = 1; my $group_id = '1491617@N22'; # Find it from the flickr page source. RSS feeds help.
my $ua = LWP::UserAgent->new; $ua->timeout(15); $ua->env_proxy;
sub get_flickr_photo_collection { my ($per_page,$page) = (shift,shift); my $api = new Flickr::API({'key' => 'YOUR API KEY'}); my $response = $api->execute_method( 'flickr.groups.pools.getPhotos', { 'group_id' => $group_id, 'per_page'=>$per_page, 'page'=>$page, 'extras'=>'url_o,original_format', } );
return $response->{tree}{children}[1];
}
sub get_photos { my $collection_ref = shift; my @photos; my @kids = @{$collection_ref->{children}}; foreach(@kids) { next unless $->{name}; my $attr = $->{attributes};
# Assume jpeg file extension.
my $extn = $attr->{originalformat}?$attr->{originalformat}:"jpg";
my $filename = lc $attr->{ownername} . "-" . $attr->{title} . "-" . $attr->{id} . $attr->{secret};
$filename=~s/\s+/_/g;
$filename = "$dump_directory/$filename.$extn";
my $url = $attr->{url_o};
if(!$url) {
$url = construct_original_url($attr);
}
warn "Dumping $url to $filename\n";
$ua->mirror($url,$filename);
}
return \@photos;
}
If photo doesn't return a original url, lets assume a _b size exists.
sub construct_original_url { my $attr = shift; return "http://farm" . $attr->{farm} . ".static.flickr.com/" . $attr->{server} . "/" . $attr->{id} . "" . $attr->{secret} . "b.jpg"; }
while($page*$per_page<$poolsize){ my $collection_ref = get_flickr_photo_collection($per_page, $page); $photos = get_photos($collection_ref); $page++; }