Updated Flickr URL script for TextExpander

Last week I wrote a little Python script that printed out the URL of a Flickr image when that image’s page is currently showing in Safari. I used that script with TextExpander to automatically type out the URL when I needed it without having to dig in a couple of levels to get the image URL by hand. I’ve since improved the script to be more flexible and easier to modify.

I won’t go through my motivation for writing the script; it’s laid out in last week’s post. I’ll just point out that there were two problems with the script as it was originally written:

  1. “Special” strings, like URLs, were buried in the code instead of defined at the beginning.
  2. It worked only when the current page in Safari was the main page for the photo. It failed when the current page was, for example, one of the “sized” pages for the image.

Both of these problems have been fixed with this new version.

 1:  #!/usr/bin/python
 2:  
 3:  import appscript
 4:  import re
 5:  import sys
 6:  from urllib import urlopen
 7:  
 8:  # The basic URL format for photos.
 9:  baseURL = 'http://www.flickr.com/photos/%s/%s/'
10:  
11:  # The regex for extracting user and photo info.
12:  infoRE = r'flickr\.com/photos/(.*)/(\d+)/?'
13:  
14:  # The various image URL suffixes.
15:  suffixes = {'master': '_m.jpg',
16:              'original':    '_o_d.jpg',
17:              'large':   '_b_d.jpg',
18:              'medium640':   '_z_d.jpg',
19:              'medium500':   '_d.jpg',
20:              'small': '_m_d.jpg',
21:              'thumbnail':   '_t_d.jpg',
22:              'square':  '_s_d.jpg'}
23:  
24:  # Get the URL of the frontmost Safari tab and extract the photo info.
25:  thisURL = appscript.app('Safari').documents[0].URL.get()
26:  info = re.findall(infoRE, thisURL)
27:  
28:  # Download the main page for that photo and get its "master URL."
29:  # Use the master to generate the URL for the medium500 image
30:  # and print it.
31:  try:
32:    user = info[0][0]
33:    id = info[0][1]
34:    pageURL = baseURL % (user, id)
35:    html = urlopen(pageURL).read()
36:    imageURL = re.search(r'<link\s+rel="image_src"\s+href="([^"]+)"', html).group(1)
37:    imageURL = imageURL.replace(suffixes['master'], suffixes['medium500'])
38:    sys.stdout.write(imageURL)
39:  
40:  # Print an error message if there's any problem.
41:  except:
42:    sys.stdout.write("wrongpagewrongpage")

Lines 8-22 pull all the special strings out to the top of the code, where they can be seen (and adjusted if Flickr changes its URL format). The new suffixes dictionary included all the size possibilities, so it would be a simple matter to change the code to return, say, the Thumbnail URL; just change medium500 in Line 37 to thumbnail.

In the previous version of this script, the URL of the current Safari page would be downloaded and searched for the special <link rel="image_src" > tag. The problem with this was that some Flickr image pages—in particular, the pages associated with “sized” images—didn’t have this tag, so the search would fail. This version defines the baseURL for the photo, and downloads it instead of the current Safari page, insuring that the <link> tag will be present.

Errors are now handled through exceptions instead of an if/else test. This allows us to handle a multitude of errors with a single error message.

As before, I have this script saved as a Shell Script in TextExpander and tied to an abbreviation of ;500. Now it’s a snap to enter Flickr image URLs wherever I need them.