How to Make a URL Shortener in Python?

Posted in /  

How to Make a URL Shortener in Python?
vinaykhatri

Vinay Khatri
Last updated on November 22, 2024

    Sometimes the web URL for a page is too long and complicated to share, so we use URL shorting tools like tiny URL and bitly to shorten the 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?

    Well, a URL shorting tool accepts a page URL and provides you with a short version of that URL. The short URL will not contain the actual URL details. Instead, it will provide you with a new URL that contains the domain name of the URL Shortener Tool and a redirect ID, such as https://www.domainname.com/short_url_id. The shortened URL will redirect you to the actual URL. This means that you can use the short URL as an alternative to the actual URL.

    In this Python tutorial, I will walk you through a Python program to create a 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 experience.

    Before discussing the Python program, let's take a look at the libraries that we need to create the Python program.

    Required Libraries

    In this Python tutorial, we will be using 3 libraries that are as follows:

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

    Among these three libraries, pyshorteners and paperclip are third-party libraries. So, you need to install them for your Python Environment. The pyshorteners library is used to shorten the URL using the TinyURL tool. Run the following pip command on your system's terminal or command prompt to install the pyshorteners 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 the terminal or command prompt to install the Python paperclip library:

    pip install pyperclip
    Note: Yo u do not need to install the paperclip library if you only wish to write a program to generate short URLs.

    Now as we are all set, you need to open your favorite 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 into our Python script.

    import pyshorteners

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

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

    Now, initialize the pyshorteners Shortener object.

    #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 many more. For this tutorial, we will be using its TinyURL API. Now, we need to shorten the main_url with the Shortner() object and 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 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 URLs using pyshorteners l ibrary

    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/ntvylkh URL, it will redirect you to the main URL, i.e., https://github.com/ellisonleao/pyshorteners .

    Python Graphical User Interface Application to Generate Short URL

    Now, we will create a Python-based GUI application using Tkinter which will generate short URLs for us. The following is the Python program to create a GUI application for shortening URLs:

    from tkinter import *
    import paperclip
    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 tutorial, we learned how to make a URL shortener in Python. To shorten the URLs, you only need the Python pyshorteners library, and you can use any shortener tool for the URL. The aforementioned Python program uses the PyShorteneres TinyURL API. However, if you wish, you can also use the other APIs provided by the PyShorteners library.

    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 Comment on this Post

    0 Comments