Last active
April 11, 2018 13:38
-
-
Save KhasMek/c569fc4504a26859be4f71175611d40a to your computer and use it in GitHub Desktop.
unsorten the given url.
This file contains 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/bin/env python2 | |
import json | |
import httplib | |
import os | |
import sys | |
import urlparse | |
logging = True | |
script_location = os.path.dirname(os.path.realpath(__file__)) | |
logfile = os.path.join(script_location, "unshorten_log.json") | |
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')) | |
else: | |
return url | |
if __name__ == "__main__": | |
print("") | |
try: | |
url = sys.argv[1] | |
print(" [*] SHORT URL:\t\t{}".format(url)) | |
original_url = unshorten_url(url) | |
if logging: | |
_json = {"short_url": url, "original_url": original_url} | |
with open(logfile, 'a+') as logfile: | |
json.dump(_json, logfile) | |
logfile.write("\n") | |
print(" [*] ORIGINAL URL:\t{}".format(original_url)) | |
except IndexError: | |
print(" [!] ERROR: no url given.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment