Flickr script

I shoot photos, not as much as I used to, but it is something I do—see 125PX or my flickr. My approach to flickr has been similar to advice I was given in graduate school: Even if you have a lot of results to show any given week, show just one. Save the rest for later. Meter your output to a steady sustainable flow.

So, that means uploading to flickr just a few files a day. Of course, there are always times where I might upload a whole set pertaining to one event at once, but for the most part, I’ve limited my uploads to flickr to 1–3 photos per day. This number was determined by the combination of my output and what contacts see, which is your last 1–5 photos.

Periodically, I would cast about for some automated solution for this, but never really saw anything lightweight and easy. I’m not sure why I never resorted to scripting a solution. Reading something at Dr. Drang’s place about his old flickr upload script finally prompted me to get off my but and script something. I borrowed his code and modified it a bit to provide an easy way to upload images from a directory.

Dr. Drang tends to write pretty succinct scripts. This is a virtue in my opinion, but it’s something I can’t quite do myself. I usually need to add a bit more heavy duty argument parsing and some light error catching. No extreme exception handling here (I don’t know enough about that), but I don’t like my scripts to error if a directory doesn’t exist, etc.

This Python script is simple in operation. One can either call it from the command line with a file as its argument to upload that file, or one can call it with a -n N argument, at which point the script selects N files at random from a predetermined directory to upload to flickr. After the files are uploaded, they are moved to another predetermined output directory. Simply put a line in your crontab calling the script and voilà, your files are periodically uploaded to flickr. For example, my crontab entry:

0   16  *   *   *   /Users/tgray/bin/flickr_up -n 1

Originally, I had a version of the script that picked off N files from the directory, sorted by access time. The thinking was you could queue up files for upload in a specific order. If you didn’t like the order, you could use touch -a file to bump it to the top of the list. The next iteration of the script used a text file with a queue order for the files, listed one per line. The script took the top N entries, uploaded them, and then removed them from the queue file. One could generate the file as one wished. To duplicate the access or modification time method, just run ls -1tc > queue.txt. I use the wonderful Photomechanic for many photo management tasks, so I actually exported a text file based on an arbitrary arrangement that I chose in the contact sheet.

After implementing these various versions, I decided to simplify and just randomly select an image from the directory and upload it. Simple is good sometimes.

Potential future features

I will probably add back in the ability to read a queue for a text file. I already have the code written so it is just a matter of integrating it into the final version of the script.

One feature I would like to implement is a check to see if a picture was uploaded already on a given day. If one has already been uploaded, I’d like to skip the random upload. That way, the automated script will fill in the gaps when I don’t get around to hand curating an upload on a given day.

This should be pretty attainable using the flickr.people.getPublicPhotos and API calls.


I went ahead and spent another 30 minutes figuring out those two API calls and added an option to check to see if a photo was uploaded in the last 24 hours1. If so, the script will not upload a random photo. If not, it will. This does not affect the uploading a specified file. The nice thing about using the API calls to check for the last uploaded picture is that it respects uploads from any source. Unfortunately this new functionality requires pytz, but if you use Python, you should have that anyway.

The script


Some quick notes on the script. You can adjust the timezone and the time period at the top of the script. User credentials and directories are set in ~/.flickr_rc. I decided to stash that info there so other flickr scripts I might write can use the same information if needed. You’ll need to get your flickr credentials from flickr, as outlined in this Dr. Drang post. If you want to muddle through it yourself, register your ‘app’ with flickr here. You want a non-commercial key with write access. Then simply get your key and secret and stick it in ~/.flickr_rc—the script will tell you the format you need or you can check the source.


Update - 2015-09-07

I added a quick call out to Exiftool to grab the ‘Headline’ and ‘Caption/Description’ XMP fields and use those as the title and description on the uploaded file. I do not have this as a command line option. If you don’t have Exiftool installed, these fields are set to the base filename for the title and nothing for the description. If you’d like to use other fields to describe your photo, or not use Exiftool at all, you’ll have to modify the code.

  1. Actually 23 hours and 50 minutes. If you have this on a cronjob running once a day, I don’t want to skip an upload so I added in a 10 minute buffer. [return]

Trying something new

I have several places that I blog, all of which are severely neglected. The obvious solution is to create a new blog, in the grand tradition of bloggers with nothing to say.

Seriously though, has evolved from a place where all writings of a collection of friends appeared to writings by those friends and for those friends. It’s one of may ways we stay in touch now that we’ve been scattered to the four winds. We gladly accept outsiders, but few come around.

125PX is photography related. Output is sporadic, but the information and documents stored there are best left alone and not diluted with other noise.

Here… here I will write about non-photography topics. And topics that won’t necessarily interest my friends. I wanted a place to collect my thoughts on software use, code snippets, etc. I’ll probably also write things related to science as well. Of course, there is always the possibility of some cross posting, or at least a heads up to a protozoic article that might fit in here as well.

It’s probably unwise to split my meager output amongst several sites, but it’s what I’ve decided to do. I probably won’t blog at 125PX at all anymore. I might migrate over those posts to here, or just leave them there. That site might just be a repository for information and the few photography related articles I write.