Shortened Flickr URLs
March 20th, 2010 at 11:58 pm
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 4425073240. That ID can be part of several Flickr URLs, all of which point to some form of that photo. For example,
http://www.flickr.com/photos/drdrang/4425073240/http://www.flickr.com/photos/drdrang/4425073240/in/set-72157623475203695/http://www.flickr.com/photos/drdrang/4425073240/in/set-72157617952751035/http://www.flickr.com/photos/drdrang/4425073240/sizes/m/http://www.flickr.com/photos/drdrang/4425073240/sizes/s/
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
http://flic.kr/p/7K2E7d
Following that URL will take you to the photostream version. The string at the end of the URL, 7K2E7d, is derived mathematically from the photo’s ID number, 4425073240, 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
123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ
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.
Update 3/22/10
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[0].URL.get()
20: ids = re.findall(r'flickr\.com/photos/.*/(\d+)/?', url)
21:
22: shortflickr = 'http://flic.kr/p/%s' % b58encode(int(ids[0]))
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 print with a comma at the end, the answer is simple: I kept getting a newline at the end of the URL when I tried
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 bit.ly or 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.
Update 3/21/10
Acting on a tip from Tanja in the comments, I bought Mobile Fotos for my iPhone. It does the three steps I wanted, launching Tweetie with the photo’s title and its flic.kr URL preloaded in a new tweet. Perfect.











March 21st, 2010 at 6:39 am
I’ve wanted to make one of these scripts myself, but hadn’t found the time / energy to do so yet, so thanks!
For your last bit, check out Mobile Fotos, a third party flickr-app, that lets you upload and browse (I prefer it over the official flickr one). You can set your twitter-app in the settings and then share photos through twitter, and it uses the flic.kr link.
March 21st, 2010 at 7:13 am
By the way, I use OmniWeb as my browser, and the script works if you replace line 19 with: url = appscript.app(u’OmniWeb’).browsers[1].active_tab.address.get()
I’d also like a version that uses a link from the clipboard, but I can’t seem to make it work (my python-fu is tiny, and using ‘url = %clipboard’ doesn’t seem to work). Any thoughts on that? :)
March 21st, 2010 at 1:52 pm
Tanja, thanks for the tip on Mobile Fotos. It sounds like just what I want.
As for getting the Flickr URL off the clipboard, you can either use the
osaxmodule, which Clark (of Clark’s Tech Blog) discusses here, or you can use thesubprocessmodule to call thepbpasteshell command:That will put whatever’s on your clipboard into the
urlvariable.Good luck!
March 22nd, 2010 at 2:25 am
I can’t seem to get TextExpander to work with these snippet. Set it to shell script format and even decided to use the ;flick trigger to expand it. Otherwise, it looks really useful if I could just make it work.
March 22nd, 2010 at 8:21 am
Alex, there’s a decent chance you don’t have the
appscriptmodule installed. See the 3/22 update in the post.March 22nd, 2010 at 3:41 pm
Doh, can’t believe I missed that obvious blurb. Thanks.
March 22nd, 2010 at 3:59 pm
Alex, you didn’t miss it. It was your comment that prompted me to add that update.
March 22nd, 2010 at 9:39 pm
Thanks. I did some research on my problem and ended up installing XCode 3.2.1 and also noticed a few other people had a problem similar to mine. In “sudo easy_install -U setuptools” and received: Running appscript-0.21.1/setup.py -q bdist_egg —dist-dir /tmp/easy_install-OZPKuu/appscript-0.21.1/egg-dist-tmp-BxfR7W unable to execute gcc-4.2: No such file or directory error: Setup script exited with error: command ‘gcc-4.2’ failed with exit status 1. Haven’t been able to figure out why it’s doing this but will keep plugging away. If anyone else has any tips, I’m all ears.
March 23rd, 2010 at 11:11 am
Alex, check your
/usr/binforgcc-4.2and make sure your$PATHincludes/usr/bin. You might also want to see if/usr/bin/gccis a symbolic link to/usr/bin/gcc-4.2. Beyond that, I’m not sure how to advise you.I’ve always found the
appscriptguys to be friendly. Have you asked them for help?