Skip to content

Instantly share code, notes, and snippets.

@jseidl
Created December 31, 2013 04:39
Show Gist options
  • Save jseidl/8192692 to your computer and use it in GitHub Desktop.
Save jseidl/8192692 to your computer and use it in GitHub Desktop.
URL unshortener in python. Just following HTTP 3XX codes within HEAD requests. Based on http://stackoverflow.com/questions/7153096/how-can-i-un-shorten-a-url-using-python/7153185#7153185 fixed for HTTPS
import httplib
import urlparse
def unshorten_url(url):
parsed = urlparse.urlparse(url)
if parsed.scheme == 'https':
h = httplib.HTTPSConnection(parsed.netloc)
else:
h = httplib.HTTPConnection(parsed.netloc)
resource = parsed.path
if parsed.query != "":
resource += "?" + parsed.query
h.request('HEAD', resource )
response = h.getresponse()
if response.status/100 == 3 and response.getheader('Location'):
return unshorten_url(response.getheader('Location')) # changed to process chains of short urls
else:
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment