-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_shortener.py
112 lines (91 loc) · 3.69 KB
/
url_shortener.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
from urllib.parse import urlparse
from http import HTTPStatus
import json
import requests
import sys
def create_link(head, long_url):
"""
Shortens a long URL into a bit.ly short link URL
:param head: request headers (API key and content type)
:param long_url: a properly-formatted URL
:return: a properly-formatted bit.ly short link URL
"""
short_url = None
# convert the URL parameters to JSON
data = {
'long_url': long_url,
'domain': 'bit.ly'
}
data = json.dumps(data)
# send the request to the bit.ly API and parse the JSON response for a 'content' field
response = requests.post('https://api-ssl.bitly.com/v4/shorten', headers=head, data=data)
re_json = json.loads(response.content)
try:
short_url = re_json['link']
except KeyError as ke:
# the JSON content couldn't be found, print the error
print('KeyError:', ke, file=sys.stderr)
return response, short_url
def update_custom(head, old_link, new_link):
"""
Changes the ending of a bit.ly ID
:param head: request headers (API key and content type)
:param old_link: a properly-formatted bitlink ID to change
:param new_link: an unused, properly-formatted bitlink ID
:return: boolean indicating the status of the change
"""
# convert the URL parameters to JSON
data = {
'custom_bitlink': new_link,
'bitlink_id': old_link
}
data = json.dumps(data)
# send the request to the bit.ly API and obtain the response
response = requests.post('https://api-ssl.bitly.com/v4/custom_bitlinks', headers=head, data=data)
return response
def create_short_url(key, long, short):
"""
Create a bit.ly ID given a long URL
:param key: the bit.ly API key for the user
:param long: a properly-formatted long URL
:param short: a properly-formatted bit.ly ID
:return: status code of the result
"""
# insert the api key into the request headers
headers = {
'Authorization': 'Bearer {}'.format(key),
'Content-Type': 'application/json',
}
# create a bit.ly ID from a long URL
result, bitly_link = create_link(headers, long)
# return details of the result if unsuccessful
if ((result.status_code != HTTPStatus.OK) and (result.status_code != HTTPStatus.CREATED)) or bitly_link is None:
print('fail: error', result.status_code, 'creating the initial short link from', long, file=sys.stderr)
print(result.content, file=sys.stderr)
return result
# parse the HTTP link to a bit.ly ID
parsed_bitly_link = urlparse(bitly_link)
bitly_id = parsed_bitly_link.netloc + parsed_bitly_link.path
# change the bit.ly ID to have a custom ending
result = update_custom(headers, bitly_id, short)
# return details of the result if unsuccessful
if result.status_code != HTTPStatus.OK:
# the response returned something else
print('fail: error', result.status_code, 'creating new short link', short, 'from generated link', bitly_id,
'for', long, file=sys.stderr)
print(result.content, file=sys.stderr)
# return the status code of the change
return result
if __name__ == '__main__':
# check to see if the API key was supplied in program arguments, and exit if not
if len(sys.argv) != 2:
print('argument error: missing API key', file=sys.stderr)
exit(1)
api_key = sys.argv[1]
url_to_shorten = input('Enter a URL to shorten:')
change_to = input('Enter a new bit.ly ID to shorten to:')
# shorten the URL
results = create_short_url(api_key, url_to_shorten, change_to)
# print the result
if results.status_code == HTTPStatus.OK:
print('Success!')