Skip to content

Instantly share code, notes, and snippets.

@jhill-cmd
Created June 14, 2020 13:31
Show Gist options
  • Save jhill-cmd/736a448967a2bbea369c4a42c80fc0c6 to your computer and use it in GitHub Desktop.
Save jhill-cmd/736a448967a2bbea369c4a42c80fc0c6 to your computer and use it in GitHub Desktop.
Unshorten_URLs
#!/usr/bin/python
#https://colin.guru/index.php?title=Unshorten_URLs
import requests, fileinput
import sys
def main(argv):
inputfile = ''
# ensure correct usage
if len(sys.argv) < 2:
print "Usage : " + sys.argv[0] + " <inputfile>"
sys.exit(1)
else:
inputfile = sys.argv[1]
# iterate each line in the input file
for line in fileinput.input([inputfile]):
line = line.rstrip('\n')
# use try, except pass to skip over any errors
try:
r = requests.get(line, allow_redirects=True, timeout=2.0)
# print each hop in the request history
for idx, hop in enumerate(r.history):
for i in range(idx):
print "-",
print hop.url
except:
print "Skipping over " + line
pass
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment