March 20, 2010 at 11:58 PM by Dr. Drang
Have you seen those “flic.kr” URLs? For some reason, I hadn’t noticed them until the past few weeks, even though they seem very useful. Once I learned how they work, I made up a quick TextExpander snippet so I could insert them into tweets and emails.
Here’s how they work. Each photo on Flickr has a unique ID number, say 5169665786. That ID can be part of several Flickr URLs, all of which point to some form of that photo. For example,
all refer to this photo
but in different contexts. Number 1 is its address in my photostream, 2 and 3 are its addresses as part of photo sets, and 4 and 5 are the addresses of its medium and small sizes.
The shortened address for this photo is
Following that URL will take you to the photostream version. The string at the end of the URL, 8SPTwJ, is derived mathematically from the photo’s ID number, 5169665786, by converting it from base 10 to base 58.
Since there’s no standard for the “digits” of base 58, any set of 58 alphanumeric characters can be used. Flickr has chosen these
which are in ascending order. The characters are basically the numerals, the lower case letters, and the upper case letters with four omissions. The omissions are:
- The numeral 0 and the upper case O.
- The lower case l and the upper case I.
The latter two were obviously chosen to avoid confusion with the numeral 1. I can’t figure out why they decided to omit the numeral 0; once upper case O is gone, there’s nothing else one could mistake for a zero. But that’s what they did, so that’s what we work with.
My conversion script is written in Python and is set up as a shell script snippet in TextExpander.
Forgot to mention that you’ll need to install the nonstandard
appscript module, which lets you control AppleScriptable applications from within Python. Follow these simple instructions to download and install it.
1: #!/usr/bin/python 2: 3: import appscript 4: import re 5: import sys 6: 7: def b58encode(n): 8: chars = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' 9: basecount = len(chars) 10: b58 =  11: while (n >= basecount): 12: (div, mod) = divmod(n, basecount) 13: b58.insert(0, chars[mod]) 14: n = div 15: if (n > 0): 16: b58.insert(0, chars[n]) 17: return ''.join(b58) 18: 19: url = appscript.app('Safari').documents.URL.get() 20: ids = re.findall(r'flickr\.com/photos/.*/(\d+)/?', url) 21: 22: shortflickr = 'http://flic.kr/p/%s' % b58encode(int(ids)) 23: sys.stdout.write(shortflickr)
Most of the work is done by the function
b58encode, which does what any base conversion routine would do: continually divide the input number by the base and use the remainder as the next digit working from right to left. Lines 19-20 extract the photo’s ID number from the URL of the frontmost Safari tab; it can use any of the various Flickr URLs. Lines 22-23 construct and print the shortened URL.
(If you’re wondering why I used
sys.stdout.write instead of a simple
23: print shortflickr,
I don’t know why the comma didn’t suppress the newline, but it didn’t.)
I have the snippet bound to the abbreviation
;flickr, so I can type that whenever I want to insert the shortened URL of the frontmost Flickr photo showing in Safari.
Why not just use a conventional shortener, like
xrl.us? They don’t tell the reader that the link is to a Flickr photo. The
flic.kr URL does.
Unfortunately, I don’t know how to get the same functionality on the iPhone, where it would be very nice to
- Take a picture.
- Upload it to Flickr.
- Tweet it.
in one fell swoop. Or even two fell swoops. If the Flickr iPhone app had a button for putting the shortened URL on the clipboard, that would be really helpful and would make
flic.kr URLs more widely used.