In this Blog article, we will learn how to Create a URL Shortner. URL shortening is the process of reducing the length of the URL. There are many messaging platforms where the length of the message is limited. Here, anyone can face the issue in sharing the long URLs, so that it can be shared easily on platforms like Twitter, where number of characters is an issue. There are so many URL Shorteners available in the market today, We will implementation it in Python.
Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!
You can refer to my YouTube video tutorial for better Understanding
What will be covered in this Blog
1. URL Introduction
2. What is Pyshorteners?
3. Creating a URL Shortner
URL Introduction:
The Dictionary defination:
A URL is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it.
A URL stands for Uniform Resource Locator.
An example of a URL is youtube.com/ayushirawat which is the URL for my YouTube channel.
URL Breakdown: URL = Protocol + Domain + path (simplest breakdown, it can be divided further!)
A basic URL breakdown of my blog will look like this.
If you wish to know more about it, you can refer to URL Wikipedia Page. Use this link to navigate to the URL's Wikipedia Page.
What is Pyshorteners?
pyshorteners is a Python lib to help you short and expand urls using the most famous URL Shorteners availables. With the help of pyshorteners , you can generate a short url or expand another one which is as easy as typing. This is basically a library in Python that provides implementation of few popular URL Shorteners.
If you wish to know more about it, you can refer to Pyshorteners Documentation. Use this link to navigate to the documentation.
Now that you are aware of URL and Pyshorteners basics, we can move forward to the coding section.
Time to code!
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
Installing Pyshorteners
Open your terminal and run the following command
pip install pyshorteners
Now that we have the package, we are ready to import it in our python script.
import pyshorteners as sh
Now, lets store the URL that we will use for URL shortening.
link = 'https://www.youtube.com/ayushirawat/videos'
I am storing my YouTube channel URL in link
. You can even take the URL as a user input if you like, so you wont have to make changes in your program everytime you wish to shorten a URL.
s = sh.Shortener()
Now, let's call Shortener
method and store it.
print(s.tinyurl.short(link))
Now, I am calling tinyurl.short
on s
and passing the URL stored in link
. Finally lets print
our result.
https://tinyurl.com/y533jl6f
and here is our shortened URL.
This is all about Creating a URL Shortner. That's it! simple, isn't it? Hope this tutorial has helped.
You can find all the code at my GitHub Repository. Drop a star if you find it useful.
Thank you for reading, I would love to connect with you at Twitter.
Do share your valuable feedback and suggestions!
You should definitely check out my other Blogs:
- Python 3.9: All You need to know
- The Ultimate Python Resource hub
- GitHub CLI 1.0: All you need to know
- Become a Better Programmer
- How to make your own Google Chrome Extension
- Create your own Audiobook from any pdf with Python
- You are Important & so is your Mental Health!
Resources:
See you in my next Blog article, Take car