Skip to content

Latest commit

 

History

History
132 lines (98 loc) · 4.67 KB

README.md

File metadata and controls

132 lines (98 loc) · 4.67 KB

Link Shortener

This is a Python program that allows you to shorten URLs using various URL shortening services. It provides a user-friendly interface using the Gradio library.

Access it from here 🔗

Installation

Before running the program, make sure you have the following libraries installed:

  • gradio
  • pyshorteners

You can install these dependencies using pip:

pip install gradio pyshorteners

Program Overview

The program provides a selection of URL shortening services: Bitly, TinyURL, and Cuttly. You can enter a URL to be shortened and choose the desired service from a dropdown menu. After clicking the "Shorten" button, the program will generate a shortened URL using the selected service.

Usage

To use the program, follow these steps:

  1. Import the required libraries:
import random
import gradio as gr
from gradio.components import *
import pyshorteners
from pyshorteners.exceptions import BadAPIResponseException, BadURLException, ExpandingErrorException, ShorteningErrorException
  1. Define random error messages:
def get_random_error_message():
    # Error messages list
    error_messages = [
        "Oops, it seems you've taken a wrong turn in the digital labyrinth! Please provide a valid link to navigate back on track.",
        "Hmmm, this doesn't look like a magical gateway to another realm. Let's make sure you enter a valid link to unlock the portal!",
        # ...
        # Add more error messages if desired
        # ...
        "Please forgive me if I made a mistake, but it seems the link you entered isn't valid. Let's find a genuine URL and give it another shot!"
    ]
    return random.choice(error_messages)

random_error = get_random_error_message()
  1. Define URL shortening functions for each service:
def bitly_shorten(url):
    # Add 'https://' prefix if not present
    if not url.startswith("https://") and not url.startswith("http://"):
        url = "https://" + url
    try:
        # Shorten URL using Bitly
        s = pyshorteners.Shortener(api_key='ENTER_YOUR_OWN_API_KEY')
        shortened_url = s.bitly.short(url)
        return shortened_url
    except (BadAPIResponseException, BadURLException, ExpandingErrorException, ShorteningErrorException) as e:
        return get_random_error_message()

def tinyurl_shorten(url):
    try:
        # Shorten URL using TinyURL
        s = pyshorteners.Shortener()
        shortened_url = s.tinyurl.short(url)
        return shortened_url
    except (BadAPIResponseException, BadURLException, ExpandingErrorException, ShorteningErrorException) as e:
        return get_random_error_message()

def cuttly_shorten(url):
    try:
        # Shorten URL using Cuttly
        s = pyshorteners.Shortener(api_key="ENTER_YOUR_OWN_API_KEY")
        shortened_url = s.cuttly.short(url)
        return shortened_url
    except (BadAPIResponseException, BadURLException, ExpandingErrorException, ShorteningErrorException) as e:
        return get_random_error_message()
  1. Create a dictionary of services:
services = {
    "Bitly": bitly_shorten,
    "TinyURL": tinyurl_shorten,
    "Cuttly": cuttly_shorten
}
  1. Define the main function:
def shorten_url(url, selected_service):
    # Get shortened

 URL using the selected service
    shortened_url = services[selected_service](url)
    return shortened_url
  1. Create the Gradio interface:
url_input = gr.Textbox(label="Enter the URL to shorten:", placeholder="https://github.com/krsatyam7")

service_dropdown = gr.Dropdown(choices=list(services.keys()), label="Select the URL shortening service:", value="Please select any")

output_text = gr.outputs.Textbox(label="Shortened URL:")

interface = gr.Interface(fn=shorten_url, inputs=[url_input, service_dropdown], outputs=output_text, title="Link Shortener 🔗", theme=gr.themes.Soft(), allow_flagging="never")
interface.launch()

Running the Program

To run the program, execute the Python script. The Gradio interface will open in your default web browser. Enter the URL you want to shorten and select the desired URL shortening service from the dropdown menu. Click the "Shorten" button to generate the shortened URL.

Note: Make sure you have an active internet connection to access the URL shortening services.

Customization

Feel free to customize the error messages, add more URL shortening services, or modify the appearance of the Gradio interface according to your preferences.

Enjoy shortening your URLs with ease using this Python program!