Skip to content

Instantly share code, notes, and snippets.

@swinton
Created January 10, 2010 22:04
Show Gist options
  • Save swinton/273811 to your computer and use it in GitHub Desktop.
Save swinton/273811 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import httplib, re, sys
from urllib import urlencode
def get_spotl_url(uri):
"""
Returns a spo.tl URL, given a Spotify track URI
"""
#
# Make sure we have a Spotify track URI
#
regex = re.compile(r'\bspotify:track:\S+\b')
if regex.search(uri) == None:
return ''
#
# Perform a HEAD request to get the shortened track URI
#
conn = httplib.HTTPConnection("spo.tl")
conn.request("HEAD", "/shorten?%s" % urlencode({'link' : uri}))
res = conn.getresponse()
if res.status == 302:
location = res.getheader('Location')
path = location.split('/')[-1]
return 'http://spo.tl/' + path
return ''
if __name__ == '__main__':
print(get_spotl_url(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment