Skip to content

Commit 78e4687

Browse files
committed
added url shortener tutorial
1 parent ecf941b commit 78e4687

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
104104
- [How to Use Github API in Python](https://www.thepythoncode.com/article/using-github-api-in-python). ([code](general/github-api))
105105
- [How to Use Google Drive API in Python](https://www.thepythoncode.com/article/using-google-drive--api-in-python). ([code](general/using-google-drive-api))
106106
- [How to Translate Text in Python](https://www.thepythoncode.com/article/translate-text-in-python). ([code](general/using-google-translate-api))
107+
- [How to Make a URL Shortener in Python](https://www.thepythoncode.com/article/make-url-shortener-in-python). ([code](general/url-shortener))
107108

108109
- ### [Database](https://www.thepythoncode.com/topic/using-databases-in-python)
109110
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))

Diff for: general/url-shortener/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [How to Make a URL Shortener in Python](https://www.thepythoncode.com/article/make-url-shortener-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Edit your account credentials for both `bitly_shortener.py` and `cuttly_shortener.py` and run the scripts along with URL you want to shorten, example:
5+
```
6+
python bitly_shortener.py https://www.thepythoncode.com/article/make-url-shortener-in-python
7+
```

Diff for: general/url-shortener/bitly_shortener.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import requests
2+
import sys
3+
4+
# account credentials
5+
username = "o_3v0ulxxxxx"
6+
password = "your_password_here"
7+
8+
# the URL you want to shorten
9+
url = sys.argv[1]
10+
11+
# get the access token
12+
auth_res = requests.post("https://api-ssl.bitly.com/oauth/access_token", auth=(username, password))
13+
if auth_res.status_code == 200:
14+
# if response is OK, get the access token
15+
access_token = auth_res.content.decode()
16+
print("[!] Got access token:", access_token)
17+
else:
18+
print("[!] Cannot get access token, exiting...")
19+
exit()
20+
21+
# construct the request headers with authorization
22+
headers = {"Authorization": f"Bearer {access_token}"}
23+
24+
# get the group UID associated with our account
25+
groups_res = requests.get("https://api-ssl.bitly.com/v4/groups", headers=headers)
26+
if groups_res.status_code == 200:
27+
# if response is OK, get the GUID
28+
groups_data = groups_res.json()['groups'][0]
29+
guid = groups_data['guid']
30+
else:
31+
print("[!] Cannot get GUID, exiting...")
32+
exit()
33+
34+
# make the POST request to get shortened URL for `url`
35+
shorten_res = requests.post("https://api-ssl.bitly.com/v4/shorten", json={"group_guid": guid, "long_url": url}, headers=headers)
36+
if shorten_res.status_code == 200:
37+
# if response is OK, get the shortened URL
38+
link = shorten_res.json().get("link")
39+
print("Shortened URL:", link)

Diff for: general/url-shortener/cuttly_shortener.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import requests
2+
import sys
3+
4+
# replace your API key
5+
api_key = "64d1303e4ba02f1ebba4699bc871413f0510a"
6+
7+
# the URL you want to shorten
8+
url = sys.argv[1]
9+
10+
# preferred name in the URL
11+
12+
api_url = f"https://cutt.ly/api/api.php?key={api_key}&short={url}"
13+
# or
14+
# api_url = f"https://cutt.ly/api/api.php?key={api_key}&short={url}&name=some_unique_name"
15+
16+
# make the request
17+
data = requests.get(api_url).json()["url"]
18+
if data["status"] == 7:
19+
# OK, get shortened URL
20+
shortened_url = data["shortLink"]
21+
print("Shortened URL:", shortened_url)
22+
else:
23+
print("[!] Error Shortening URL:", data)

Diff for: general/url-shortener/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)