Skip to content

Instantly share code, notes, and snippets.

@gin0606
Created June 15, 2013 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gin0606/5788744 to your computer and use it in GitHub Desktop.
Save gin0606/5788744 to your computer and use it in GitHub Desktop.
引数に短縮URL渡すと展開したURL返す奴。HEAD Requestだから早い。
#!/usr/local/bin/python
# -*- coding: utf-8; -*-
from urlparse import urlparse
from httplib import HTTPConnection
def expandURL(url):
"""
短縮URLを展開する
Arguments:
- `url`: 展開したいURL
"""
o = urlparse(url)
con = HTTPConnection(o.netloc)
con.request('HEAD', o.path)
res = con.getresponse()
if res.getheader('location') == None:
return url
return res.getheader('location')
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
u = sys.argv[1]
uu = expandURL(u)
# 展開出来るところまで展開する
while uu != u:
u = uu
uu = expandURL(u)
print uu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment