Created
June 15, 2013 16:58
-
-
Save gin0606/5788744 to your computer and use it in GitHub Desktop.
引数に短縮URL渡すと展開したURL返す奴。HEAD Requestだから早い。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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