URL Shortener in Python

Today we build URL Shortener in Python. As you know URL shortener help to reduce the length of the URL so that it can be shared easily on platforms like Twitter, where a number of characters are an issue.

There are so many URL Shorteners available in the market today, that will definitely help you out to solve the purpose. But today we build url shortner in python.

To do this we are using pyshorteners python module. pyshorteners is a Python lib to help you shorten and expand URLs using the most famous URL Shorteners available.

Installation

pip3 install pyshorteners

Code

TinyURL.com shortener implementation

import pyshorteners

url = input("Enter your url: ")
print("Your url: ", pyshorteners.Shortener().tinyurl.short(url))

#follow insta/@code_snail 

Output

Bit.ly shortener Implementation

Bitly: Bitly provides a platform to shorten URLs, share them, and keep a track of the activity on the shortened URL.

Before starting using Bitly API, you first need to signup on the site to get an API Key. This is very important to get access to the API to use it for programming.

To generate your OAuth access token:

  1. Click the profile menu.
  2. Click Profile Settings.

url shortener in python

3. Click Generic Access Token.

bit.ly url shortener in python

4. Enter your Bitly account password.

2020-05-04_09h15_23.png

5. Click Generate Token.

url shortener using python

6. Click Copy to copy the token. If you leave this setting and then access it again, you’ll be prompted to enter your password again to view the token.

url shortener in python

7. You can now paste the token into your code or settings to connect via the API.

Code

import pyshorteners

url = input("Enter your url: ")
s = pyshorteners.Shortener(api_key="YOUR_KEY")
print(s.bitly.short(url))

#follow insta/@code_snail

Output

url shortners in python

See codes for other url shortners services: https://pyshorteners.readthedocs.io/en/latest/apis.html#tinyurl-com

Hope you like this URL shortener code with python. Now make your own URL shortener in python. Must share it with your friends.

Other python mini projects,