Downloading Flickr photos

My wife has a legitimate complaint about Flickr: it requires too much busywork to download photos. I like putting my personal photos there, and while it’s easy for her to review the shots I took, if she wants to download some to email to a relative or post on Facebook, she has to do an awful lot of clicking to

And when the photos are downloaded, they have unhelpful filenames like 6190385164_92954962bc_b.jpg. So I decided to write a little script that would simplify the process.

As I’ve said in these recent posts, I’ve been playing around with the Flickr API and have put some of my work in a GitHub repository. A downloading script turned out to be pretty easy.

The first thing was to add a new function to my library. It’s called currentFlickrTitle and it does pretty much what it says: returns the title of the Flickr image in the current browser window. Here’s the source code:

 31:  def currentFlickrTitle():
 32:    '''Return the title of the Flickr image currently showing in the browser.
 34:    The function works through Apple Events and supports only the Safari and
 35:    Chrome browsers.'''
 37:    # Flickr parameters
 38:    fuser = 'Flickr username'
 39:    key = 'Get key from Flickr'
 40:    secret = 'Get secret from Flickr'
 42:    # Get the image ID.
 43:    try:
 44:      imageID = currentFlickrID()
 45:    except IndexError:
 46:      return "Not a Flickr image"
 48:    # Establish the connection with Flickr.
 49:    flickr = FlickrAPI(api_key=key, secret=secret)
 51:    # Get the title.
 52:    etree = flickr.photos_getInfo(photo_id = imageID, format = 'etree')
 53:    for i in etree[0]:
 54:     if i.tag == 'title':
 55:       return i.text
 56:       break
 58:    # If the size wasn't found.
 59:    return "Title not found"

Like the other functions in, it uses Sybren Stüvel’s FlickrAPI library. Lines 38-40 are filled with authorization strings that Flickr gives out to developers who want to use the API; I described how to get authorized in this post. Line 44 calls another function in the library, currentFlickrID, to do the interaction with Safari or Chrome and get the Flickr ID of the image in the current browser window. It uses the appscript library, which allows Python to do AppleScript-like interaction with applications. The FlickrAPI library uses the ElementTree format to return the data from Flickr, and Lines 52-56 pull the photo’s title out of that format.

With currentFlickrTitle defined to provide the name for the downloaded file, and the previously defined currentFlickrURL function to provide the URL, the script for downloading the file and saving it locally is straightforward:

 1:  #!/usr/bin/python
 3:  from urllib2 import urlopen
 4:  from currentflickr import currentFlickrURL, currentFlickrTitle
 5:  from os import environ
 6:  from appscript import app
 8:  # This script downloads the large version of the Flickr image
 9:  # currently showing in the browser window and saves it to the
10:  # Desktop. The filename is the Flickr image title, with ".jpg"
11:  # appended if necessary.
13:  try:
14:    # Get the large version of the image and its title.
15:    name = currentFlickrTitle()
16:    if name[-4:].lower() != '.jpg':
17:      name += '.jpg'
18:    image = urlopen(currentFlickrURL('Large')).read()
20:    # Open a file for writing with the name of the image.
21:    imgFile = open(environ['HOME'] + '/Desktop/' + name, 'w')
23:    # Write the image and close the file.
24:    imgFile.write(image)
25:    imgFile.close()
26:    app('Play Sound').play('Macintosh HD:System:Library:Sounds:Glass.aiff')
28:  except:
29:    app('Play Sound').play('Macintosh HD:System:Library:Sounds:Basso.aiff')

I’m not sure what I can say about this script that isn’t said in the comments or the code itself. I decided to standardize on getting the “Large” version of the photo, which is 1024 pixels in its longest dimension, because its a good size for emailing and posting to Facebook—big enough to show detail, but not cumbersome.

I guess I should say something about the sounds in Lines 26 and 29. To let my wife know whether the download worked or not, I decided to have the pingy “Glass” sound play when it works and the burpy “Basso” sound play when it doesn’t. These lines make use of the Play Sound application that I first wrote about back in 2007.

How will my wife call this script? I’ll install FastScripts on her computer and save the script in her ~/Library/Scripts folder with a nice descriptive name, like Download Flickr Image so it’ll show up that way in her FastScripts menu. That’ll be the easy part. The hard part will be remembering all the libraries I’ll need to install.

Because I wrote this so my wife could download my photos, I didn’t include any code for checking the photos’ license—she can do whatever she wants with my pictures. If you want to adapt it for your own use, everything described in this post is in the GitHub repository, but make sure you honor the photographers’ wishes.