Strange Symphonies The whole is greater than the sum of its parts

20Nov/081

Batch Download of Photos From Flickr Based on Their Tag

For the FOSS.my event, I wanted everyone to tag their photos with foss.my or foss.my 2008. Because this makes it easier to easily see FOSS.my buzz as it happens on Flickr!

A Tale of Sucky Wordpress Plugins

I was looking for a plugin to grab Flickr photos by a specified tag, so that we can use it in the foss.my gallery. Though there are many Flickr plugins for Wordpress, there was none catered to my requirements. Unable to find a solution, I thought it was quicker to hack one myself.

So this script just expanded from there, and I quickly had a Flickr batch downloader.

[block:important]There are several requirements:

  • Download phpFlickr, and unzip it to the same directory as the script.
  • You need to obtain a Flickr API key.

[/block]

Use the source

<?
/*
* phpFlickr
* http://phpflickr.com/
*
* Flickr API
* http://www.flickr.com/services/api/
*
* Flickr API call flickr.photos.search
* http://www.flickr.com/services/api/flickr.photos.search.html
*
* Photos are available via the following format:
* http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
*
* For more information visit http://www.flickr.com/services/api/misc.urls.html
*
* Applying for a Flickr API Key
* http://www.flickr.com/services/api/keys/apply/
*/

require_once("phpFlickr.php");

define('VERBOSE', array_search('-v', $argv));

// Download a batch of photos from the given page
function download($photos) {
$download = 0;
foreach ($photos['photo'] as $photo) {
$filename = sprintf("pics/%s-%s.jpg", $photo['owner'], $photo['id']);
$url = sprintf("http://farm%d.static.flickr.com/%s/%s_%s_o.jpg", $photo['farm'], $photo['server'], $photo['id'], $photo['secret']);
if (file_exists($filename)) {
if (VERBOSE)
print sprintf("33[33m %-10s %s33[0m \n", "Skip", $url);
else
print sprintf("33[33m.33[0m");
continue;
}
if (VERBOSE)
print sprintf("33[32;1m %-10s %s33[0m \n", "Download", $url);
else
print sprintf("33[32m.33[0m");

file_put_contents($filename, file_get_contents($url));
$download += 1;
}
return $download;
}

$flickr = new phpFlickr("YOUR_API_KEY");
$options = array('tags' => 'fossmy, "fossmy 2008", foss.my, "foss.my 2008"');

$photos = $flickr->photos_search($options);
print sprintf("Available 33[32;1m %d 33[0m \n", $photos['total']);

// Download the first batch of photos
$downloaded = download($photos);

$page = $photos['page'];
$pages = $photos['pages'];
// Download the following batch of photos
for ($i = 2; $i < $pages; $i++) {
$options['page'] = $i;
$photos = $flickr->photos_search($options);
$downloaded += download($photos);
}
if (! VERBOSE)
echo "\n";
print sprintf("Downloaded 33[32;1m %d 33[0m/ %d \n", $downloaded, $photos['total']);

Running it

It's just as a simple as:
[block:terminal]php download.php[/block]

Sample Results:

[block:terminal]
Available 492
..................[trim]............
Downloaded 0 / 492
[/block]

It even comes with a -v flag, so you can view what files are being downloaded.

Related posts

Tags

Comments (1) Trackbacks (0)
  1. Too complex. You could also use Flickr Downloadr to do the same thing. Here is the tutorial.

    http://www.photo-to-dvd.com/tutorial/make-flickr-photo-dvd-slideshow.html#125


Leave a comment


No trackbacks yet.