Skip to content

Instantly share code, notes, and snippets.

@scottjacksonx
Created March 20, 2010 08:03
Show Gist options
  • Save scottjacksonx/338548 to your computer and use it in GitHub Desktop.
Save scottjacksonx/338548 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# TextExpander Snippet for turning a Flickr photo URL on the clipboard into a shortened Flickr URL.
# Just make a new snippet in TextExpander, select "Shell Script" from the "Content" pull-down,
# and paste this code in.
from subprocess import Popen, PIPE, STDOUT
import sys
def getLongID(url):
index = url.find("http://www.flickr.com/photos/")
if index == 0:
isFlickrURL = 1
else:
isFlickrURL = 0
if isFlickrURL:
idStart = url.find("/", 29) + 1
idEnd = url.find("/", idStart)
return int(url[idStart:idEnd])
else:
print "Not a Flickr URL."
sys.exit()
alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
p = Popen("pbpaste", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
url = p.stdout.read()
if url[0:4] != "http":
print "No URL on the clipboard."
sys.exit()
else:
flickrLongID = getLongID(url)
baseCount = len(alphabet)
shortID = ""
while flickrLongID >= baseCount:
div = flickrLongID/baseCount
mod = flickrLongID%baseCount
shortID = alphabet[mod] + shortID
flickrLongID = int(div)
shortID = alphabet[flickrLongID] + shortID
sys.stdout.write("http://flic.kr/p/" + str(shortID))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment