-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyurl.py
123 lines (108 loc) · 3.8 KB
/
tinyurl.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import requests
print("TinyURL has loaded!")
#Test stuff
def test():
print("TinyURL is working.")
def create(apiToken, urlToShorten, alias, domain):
if str(domain) == "":
domain = "tinyurl.com"
if str(alias) == "":
return "Alias was not provided"
reqData = {
"api_token": str(apiToken),
"url": str(urlToShorten),
"domain": str(domain),
"alias": str(alias)
}
r = requests.post('https://api.tinyurl.com/create', data = reqData)
if r.status_code == 200:
print("Successful operation")
print("Response:")
return r.text
elif r.status_code == 401:
print("You are not authorized to use to access this API endpoint or resource. Please check your API token")
print("Response:")
return r.text
elif r.status_code == 405:
print("You do not have permission to access this resource 405")
print("Response:")
return r.text
elif r.status_code == 422:
print("Alias field is too long or is already a alias")
print("Response:")
return r.text
else:
print("There was an unexpected error processing your request")
print("Response:")
return r.text
def update(apiToken, alias, domain, newAlias, newDomain):
if str(domain) == "":
return "Domain was not provided"
if str(alias) == "":
return "Alias was not provided"
if str(newDomain) == "":
newDomain = "tinyurl.com"
reqData = {
"api_token": str(apiToken),
"domain": str(domain),
"alias": str(alias),
"new_domain": str(newDomain),
"new_alias": str(newAlias),
}
r = requests.post('https://api.tinyurl.com/update', data = reqData)
if r.status_code == 200:
print("Successful operation")
print("Response:")
return r.text
elif r.status_code == 401:
print("You are not authorized to use to access this API endpoint or resource. Please check your API token")
print("Response:")
return r.text
elif r.status_code == 405:
print("You do not have permission to access this resource 405")
print("Response:")
return r.text
elif r.status_code == 422:
print("Alias field is too long or is already a alias")
print("Response:")
return r.text
else:
print("There was an unexpected error processing your request")
print("Response:")
return r.text
def change(apiToken, alias, domain, newAlias, newUrl):
if str(domain) == "":
return "Domain was not provided"
if str(alias) == "":
return "Alias was not provided"
if str(newUrl) == "":
return "New URL was not provided"
reqData = {
"api_token": str(apiToken),
"domain": str(domain),
"alias": str(alias),
"url": str(newUrl),
}
r = requests.post('https://api.tinyurl.com/change', data = reqData)
if r.status_code == 200:
print("Successful operation")
print("Response:")
return r.text
elif r.status_code == 401:
print ("You are not authorized to use to access this API endpoint or resource. Please check your API token")
print ("Response:")
return r.text
elif r.status_code == 405:
print("You do not have permission to access this resource 405")
print("Response:")
return r.text
elif r.status_code == 422:
print("Alias field is too long or is already a alias")
print("Response:")
return r.text
else:
print("There was an unexpected error processing your request")
print("Response:")
return r.text
def bulk():
return "Not supported, yet."