I _Really_ Don't Know

A low-frequency blog by Rob Styles

Flickr Bashing

I spent some time a little while ago writing a bash interface to the Flickr api, using curl to handle the http interactions.

I was doing it because I have a backlog of around 11,000 photos that need sorting and uploading. To get them backed up I decided to simply upload them all marked as private and tagged as 'toProcess' so that I could then start to go through them online.

I'd written most of the raw bash scripts to do the job, but hadn't put enough error handling in, or enough testing, to trust them with my live flickr account.

That was back at Christmas, so coming back to it fresh a thought occurred to me. At work we've been working with php a lot, and php works just dandy as a command line scripting language as well as a web language. Not only that, but php also has a great Flickr library already, it's tested and in production use - phpFlickr by Dan Coulter.

So, with all my photos already organised in folders...

#!/usr/bin/php
<?
$path = ini_get('include_path');
$myPath = dirname(__FILE__);
ini_set('include_path', $path . $PATH_SEPERATOR . $myPath);
ini_set('include_path', $path . $PATH_SEPERATOR . $myPath . DIRECTORY_SEPARATOR . 'phpFlickr-2.2.0');
require_once 'phpFlickr.php';
$f = new phpFlickr('_your api key here_', '_your secret here_');
$f-&gt;setToken('_your token here_');
$f-&gt;auth('write');
$searchPath = $myPath.DIRECTORY_SEPARATOR.'later/';
$uploadDir = opendir($searchPath);
$is_public = 0;
$is_friend = 0;
$is_family = 0;
while (false !== ($listing = readdir($uploadDir)))
{
  $isHidden = ((preg_match('/^\./', $listing)) &gt; 0) ? true : false;
  if (is_dir($searchPath.DIRECTORY_SEPARATOR.$listing) && !$isHidden)
  {
    echo $listing."\n";
    $year = (preg_match('/[0-9]{4}/', $listing)) ? substr($listing, 0, 4) : null;
    $setName = $listing;
    $setDir = opendir($searchPath.DIRECTORY_SEPARATOR.$listing);
    $tags = "$year \"$setName\" toprocess";
    while (false !== ($setListing = readdir($setDir)))
    {
      $isJpeg = ((preg_match('/\.jpg$/i', $setListing)) &gt; 0) ? true : false;
      if ($isJpeg)
      {
        $photoFilename = $searchPath.DIRECTORY_SEPARATOR.$listing.DIRECTORY_SEPARATOR.$setListing;
        $response = $f-&gt;async_upload($photoFilename, $setListing, '', $tags, $is_public, $is_friend, $is_family);
        echo $response."\t".$photoFilename."\n";
        rename($photoFilename, $photoFilename.'.done');
        //sleep(1);
        stream_set_blocking(STDIN, FALSE);
        $stdin = fread(STDIN, 1);
        if ($stdin != '')
        {
          echo "Exiting from stdin keypress\n";
          exit(0);
        }
      }
    }
  }
}
closedir($uploadDir);
?>

This script wanders through the folders uploading any jpegs it finds, tagging them with the name of the folder they came from as well as a year taken from the first four digits of the folder name. All so simple thanks to Dan Coulter's work.

Comments

Throttling Flickr Uploads | I Really Don’t Know

[...] using phpFlickr to batch upload photos to Flickr and my script uploads as fast as the bandwidth will allow, though admittedly single-threaded. This [...]

cosbeta

it returned 500 for my program,could you please help me out? setToken(MYTOKEN); $f->auth('write'); $res = $f->async_upload("001.jpg", $title = "jpgirl", $description = "no", $tags = "homezz_admin"); ?>

Rob Styles

@Michael, this script was uploading sets of photos from directories on my local machine up to Flickr. I don't know why you'd want to upload them to an intermediate server first unless perhaps you didn't have PHP and libcurl installed on your local machine.

Michael

So out of interest, is this uploading photos directly from your hard drive, or from a server? Dan was saying that his script can't be used to directly upload from my pc to flickr; that i would have to put them on my server first... I would rather not have to do that, as my server has very limited space... Would really love some help. Thanks...