Skip to content

URL Shortener

Abstract

URL Shortener is a simple Python application that converts long URLs into short, manageable links using the pyshortenerspyshorteners library. The application supports multiple URL shortening services including TinyURL and Bitly. This project demonstrates working with external APIs, URL manipulation, and third-party libraries. Users can input a long URL and receive a shortened version that redirects to the original link, making it perfect for sharing on social media or in constrained spaces.

Prerequisites

  • Python 3.6 or above
  • A code editor or IDE
  • pyshorteners module
  • Internet connection

Before you Start

Before starting this project, you must have Python installed on your computer. If you don’t have Python installed, you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed, you can download Visual Studio Code from here. You must have the pyshorteners module installed on your computer. You can install it using the following command:

command
C:\Users\Your Name>pip install pyshorteners
command
C:\Users\Your Name>pip install pyshorteners

For Bitly integration (optional), you’ll need to:

  1. Create a free account at Bitly
  2. Get your API key from the Bitly developer dashboard
  3. Replace the placeholder API key in the code

Getting Started

Create a Project

  1. Create a folder named url-shortenerurl-shortener.
  2. Open the folder in your favorite code editor or IDE.
  3. Create a file named urlshorter.pyurlshorter.py.
  4. Copy the given code and paste it in your urlshorter.pyurlshorter.py file.

Write the Code

  1. Copy and paste the following code in your urlshorter.pyurlshorter.py file.
⚙️ URL Shortener
URL Shortener
# URL Shorter
 
import pyshorteners # pip install pyshorteners
 
 
 
url = input("Enter URL: ")
# TinyURL
shortener = pyshorteners.Shortener()
shortURL = shortener.tinyurl.short(url)
 
# Bitly
# bitlyShortener = pyshorteners.Shortener(api_key='01b6c587cskek4kdfijsjce4cf27ce2') # Change API Key with your own API Key
# shortURL = bitlyShortener.bitly.short(url) # bitly
print("Short URL: ", shortURL) 
URL Shortener
# URL Shorter
 
import pyshorteners # pip install pyshorteners
 
 
 
url = input("Enter URL: ")
# TinyURL
shortener = pyshorteners.Shortener()
shortURL = shortener.tinyurl.short(url)
 
# Bitly
# bitlyShortener = pyshorteners.Shortener(api_key='01b6c587cskek4kdfijsjce4cf27ce2') # Change API Key with your own API Key
# shortURL = bitlyShortener.bitly.short(url) # bitly
print("Short URL: ", shortURL) 
  1. Save the file.
  2. Open the terminal in your code editor or IDE and navigate to the folder url-shortenerurl-shortener.
command
C:\Users\Your Name\url-shortener> python urlshorter.py
Enter URL: https://www.example.com/very/long/url/with/many/parameters?param1=value1&param2=value2
Short URL:  https://tinyurl.com/abc123def
 
C:\Users\Your Name\url-shortener> python urlshorter.py
Enter URL: https://github.com/Ravikisha/PythonCentralHub
Short URL:  https://tinyurl.com/pythonhub123
command
C:\Users\Your Name\url-shortener> python urlshorter.py
Enter URL: https://www.example.com/very/long/url/with/many/parameters?param1=value1&param2=value2
Short URL:  https://tinyurl.com/abc123def
 
C:\Users\Your Name\url-shortener> python urlshorter.py
Enter URL: https://github.com/Ravikisha/PythonCentralHub
Short URL:  https://tinyurl.com/pythonhub123

Explanation

  1. Import the required module.
urlshorter.py
import pyshorteners # pip install pyshorteners
urlshorter.py
import pyshorteners # pip install pyshorteners
  1. Get the URL input from the user.
urlshorter.py
url = input("Enter URL: ")
urlshorter.py
url = input("Enter URL: ")
  1. Create a shortener object and generate short URL using TinyURL.
urlshorter.py
shortener = pyshorteners.Shortener()
shortURL = shortener.tinyurl.short(url)
urlshorter.py
shortener = pyshorteners.Shortener()
shortURL = shortener.tinyurl.short(url)
  1. Alternative Bitly implementation (commented out).
urlshorter.py
# bitlyShortener = pyshorteners.Shortener(api_key='your_api_key_here')
# shortURL = bitlyShortener.bitly.short(url)
urlshorter.py
# bitlyShortener = pyshorteners.Shortener(api_key='your_api_key_here')
# shortURL = bitlyShortener.bitly.short(url)
  1. Display the shortened URL.
urlshorter.py
print("Short URL: ", shortURL)
urlshorter.py
print("Short URL: ", shortURL)

Supported Services

This URL shortener supports multiple services:

TinyURL (Default)

  • Pros: No API key required, simple to use
  • Cons: Limited customization options
  • Usage: Included in the default code

Bitly

  • Pros: Custom short URLs, analytics, branded links
  • Cons: Requires API key registration
  • Usage: Uncomment the Bitly section and add your API key

Other Supported Services

The pyshortenerspyshorteners library also supports:

  • Is.gd
  • V.gd
  • QR.net
  • Chilp.it
  • Clck.ru
  • And many more!

Features

  • Convert long URLs to short, shareable links
  • Support for multiple URL shortening services
  • Simple command-line interface
  • No database required
  • Instant URL conversion
  • Works with any valid HTTP/HTTPS URL
  • Cross-platform compatibility

How to Use

  1. Run the application
  2. Enter the long URL you want to shorten
  3. Press Enter to get the shortened URL
  4. Copy and share the short URL
  5. The short URL will redirect to the original long URL when clicked

API Key Setup (For Bitly)

To use Bitly services:

  1. Visit Bitly Developer
  2. Create a free account
  3. Generate an API access token
  4. Replace 'your_api_key_here''your_api_key_here' with your actual API key
  5. Uncomment the Bitly code section

Next Steps

You can enhance this project by:

  • Adding a GUI interface using Tkinter
  • Creating a web application using Flask
  • Implementing URL validation
  • Adding custom short URL aliases
  • Creating a URL history database
  • Adding QR code generation for short URLs
  • Implementing click analytics
  • Adding bulk URL shortening
  • Creating browser extension integration

Error Handling

Consider adding error handling for:

  • Invalid URLs
  • Network connectivity issues
  • API rate limits
  • Service unavailability

Example enhanced version:

urlshorter.py
try:
    shortURL = shortener.tinyurl.short(url)
    print("Short URL: ", shortURL)
except Exception as e:
    print("Error creating short URL:", str(e))
urlshorter.py
try:
    shortURL = shortener.tinyurl.short(url)
    print("Short URL: ", shortURL)
except Exception as e:
    print("Error creating short URL:", str(e))

Conclusion

In this project, we learned how to create a URL Shortener using Python and the pyshortenerspyshorteners library. We explored working with external APIs, URL manipulation, and third-party service integration. This project demonstrates how to leverage existing services to create useful applications quickly and efficiently. The skills learned here can be applied to other API-based projects and web service integrations. To find more projects like this, you can visit Python Central Hub.

Was this page helpful?

Let us know how we did