How to Make a URL Shortener in Python?

By | September 27, 2021
How to Make a URL Shortener in Python

Often the web URL for a page gets too long and complicated to send, so we use the URL shorting tools like tiny URL and bitly to short our URL. You have also seen that most of the malicious cyber link attacks also use the shorter URL tool to short the URL link so the user cannot see the actual redirect link.

So what does a URL Shorting tool does?- The URL Shorting Tool accepts a page URL and provides you with a short form of that URL. The Short URL will not contain the actual URL text detail, instead, it will provide you with the URL of the URL Shortener Tool domain name with the redirect ID such as https://www.domainname.com/short_url_id.

The Short You get will redirect you to the actual URL. This means you can use the short URL as an alternative for the actual URL.

In this Python tutorial, I will walk you through the Python program to Generate URL Shortner.

First, we will write the URL Shortener Python program for the console output, later we will also build a GUI application using Python Tkinter for a better User Interface.

Vamware

Before Diving into the Program let’s Install the Required library.

Install Required Library

In this Python tutorial, we will be using 3 Libraries.

Vamware
  1. pyshorteners (Python Third Party)
  2. paperclip (Python Third Party)
  3. Tkinter (Python Standard Library)

Among these three libraries pyshorteners and paperclip are the Python Third-Party Open Source Libraries, so you need to install them for your Python Environment.

The pyshorteners library is used to short the URL using the TinyURL tool.

Run the following pip command on your terminal or command prompt to install this library.

pip install pyshorteners

The paperclip library is used to handle the text characters in the Python program we will be using this library to copy the text while creating the GUI Python program to generate a short URL.

Run the following pip command on your terminal or command prompt to install the Python paperclip library.

pip install pyperclip

Note: You do not need to istall paperclip libary if you only wish write a program to Generate short URLs. 

Now we are all set, Open your best Python IDE or Text Editor and start coding with Me.

Python Program to Make a URL Shortener

Let’s start with importing the pyshorteners module in our Python script

import pyshorteners

Now ask the user to enter the URL for which he/she wish to create a short URL.

main_url = input("Enter the Main URL: ")

Now initialize the pyshortenersShortenerobject.

#initialize the Shortener
url_shortener = pyshorteners.Shortener()

The Shortener class supports many URL shortening tool APIs such as Adf.ly, bitly, Chilp.it, TinyURL, and my more. But for this tutorial, we will be using its TinyURL API.

Now short our main_url with the Shortner()object tinyurl.short()method.

#short the url with tinyurl
short_url = url_shortener.tinyurl.short(main_url)

The tinyurl.short() method accepts the main URL and returns a tiny short URL.

Now print or access the short_url.

print(f"The Short Url of {main_url} is: ")
print(short_url)

Put all the code together and execute.

#Python program to generate Short URL using pyshorteners Library.

import pyshorteners
main_url = input("Enter the Main URL: ")
#initialize the Shortener
url_shortener = pyshorteners.Shortener()
#short the url with tinyurl
short_url = url_shortener.tinyurl.short(main_url)
print(f"The Short Url of {main_url} is: ")
print(short_url)

Output

Enter the Main URL: https://github.com/ellisonleao/pyshorteners
The Short Url of https://github.com/ellisonleao/pyshorteners is: 
https://tinyurl.com/ntvylkh

When you visit the https://tinyurl.com/ntvylkhURL will redirect you to the main URL https://github.com/ellisonleao/pyshorteners

Python Graphical User Interface Application to Generate Short URL

Now we will be using a GUI Python application using Tkinter which will generate the Short URL for us.

from tkinter import *
import pyperclip
import pyshorteners

def url_shortner():
    shortener = pyshorteners.Shortener()
    url_short = shortener.tinyurl.short(main_url.get())
    
    #set the gloabal short_url
    short_url.set(url_short)

def copy_url():
    #copy short url on clipboard
    pyperclip.copy( short_url.get())

if __name__=="__main__":
    root = Tk()
    root.geometry("700x700")
    root.title("My URL Shortener App")
    root.configure(bg="#49A")

    main_url = StringVar()
    short_url= StringVar()
    
    Label(root, text="Enter The Main URL", font="poppins").pack(pady=5)
    Entry(root,textvariable=main_url, width =100).pack(pady=5)
    Button(root, text="Generate Short URL", command =url_shortner).pack(pady=5)

    Label(root, text="The Short URL ", font="poppins").pack(pady=5)
    Entry(root, textvariable= short_url, width=50).pack(pady=5)
    Button(root, text="Copy the Short URL", command= copy_url).pack(pady=5)
    root.mainloop()

Output

Conclusion

In this Python tutorial, we learned how to Make a URL Shortener in Python. To make the shorteners URLs you only need the Python pyshorteners library, and you can use any Shortener tool for the URL.

In this Python tutorial, we have used the PyShorteneres TinyURL API, if you wish you can also use the other API provided by the PyShorteners.

I would recommend you read the official documentation of the Python pyshorteners library to know more about its implementation.

People are also reading: 

Leave a Reply

Your email address will not be published.