-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURL Unshortener
More file actions
79 lines (54 loc) · 2.55 KB
/
URL Unshortener
File metadata and controls
79 lines (54 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
#Date created: 08/03/2022
#Author: Juan van Genderen
#Purpose: To unshorten a URL and display where it leads to
import requests
import argparse
class colours:
red = "\033[91m"
green = "\033[92m"
blue = "\033[34m"
orange = "\033[33m"
purple = "\033[35m"
end = "\033[0m"
class URL_Unshortener:
def parse_arg(self):
parser = argparse.ArgumentParser(prog="Unshorten",
description="Did you get a shortened URL that you don't trust? This tool will unshorten it for you so that you can see where it actually leads to.")
parser.add_argument("url",
help="Enter a shortened URL to examine. (E.G: https://cutt.ly/dAd4ipy should lead back to my Github repo)")
parser.add_argument("-v",
default=False,
action="store_true",
help="Verbose output")
args = parser.parse_args()
self.args = args
def Unshorten(self):
if self.args.v == True:
try:
r = requests.head(self.args.url)
r2= requests.get(self.args.url)
if r.status_code//100 == 2:
print (f"{colours.red}The request was not redirected{colours.end}")
if r.status_code//100 == 3:
print (f"{colours.purple}Status code: {colours.end}{colours.orange}{r.status_code}{colours.end} \n{colours.red}Redirected to: {colours.end}{colours.blue}{r.headers['Location']}{colours.end}")
if r2.history:
for resp in r2.history:
print (f"{colours.purple}Status code: {colours.end}{colours.orange}{r2.status_code}{colours.end} \n{colours.red}Redirected to: {colours.end}{colours.blue}{r2.url}{colours.end}")
print (f"{colours.green}Final destination: {r2.url}{colours.end}")
except:
print (f"{colours.red}Something went wrong!{colours.end}")
if self.args.v == False:
try:
r = requests.head(self.args.url)
r2= requests.get(self.args.url)
if r.status_code//100 == 2:
print (f"{colours.red}The request was not redirected{colours.end}")
if r.status_code//100 == 3:
print (f"{colours.green}Final destination: {r2.url}{colours.end}")
except:
print (f"{colours.red}Something went wrong!{colours.end}")
if __name__ == "__main__":
URL_Unshortener = URL_Unshortener()
URL_Unshortener.parse_arg()
URL_Unshortener.Unshorten()