Python - URL Shortener using Tinyurl API


Introduction

In the web era, concise links are crucial to distribute hyperlinks via social networking sites, text messages, and alternative communication methods. Nevertheless, lengthy URLs might pose challenges when sharing and might be truncated when sending messages. The long URLs are frequently challenging to memorize and can be highly unwieldy to enter. In order to solve the issue at hand, web address shortening platforms such as TinyURL were created to manage the duty. Python offers a convenient approach to connecting with these options. Within this post, we are going to write a Python code to engage with the TinyURL website API system.

Definition

A link shortener is a software that receives a lengthy URL as data and produces a smaller, more convenient URL. This aids to create extended URLs simpler to exchange and recall. This abbreviated URL forwards people to the authentic prolonged URL upon clicking. Link shorteners are widely utilized on social networking sites, electronic mail communications, and any circumstance when lengthy URLs have to be exchanged conveniently. These tools to shorten extended website links into smaller and easier−to−handle links.

Syntax

import requests

url = 'http://tinyurl.com/api-create.php?url='
long_url = 'https://www.example.com/path/to/content/'

response = requests.get(url+long_url)
short_url = response.text

print(short_url)

This piece of code initially imports the module for making requests to perform requests over HTTP. The variable named "url" holds the base link for the TinyURL application programming interface. This "original_url" variable stores the web address that we need to reduce in length. Next, we make an HTTP request for the TinyURL API employing the requests.get() method and handing over the whole URL of the API with the prolonged URL attached.

The reply from the TinyURL application programming interface is sent back as textual data using the response.text property. That is subsequently assigned to the variable named "short_url". Ultimately, the code displays the abbreviated web address.

Algorithm

  • Step 1: Import the requests module

  • Step 2: Generate the primary URL designed for the TinyURL API endpoint

  • Step 3: Set the extended URL that needs to be abbreviated

  • Step 4: Initiate an HTTP request to the TinyURL service including the lengthy URL

  • Step 5: Get the compressed URL from the result and show it

Approach

  • Approach 1: By using requests.get() method.

  • Approach 2: Using PyShorteners Approach

Approach 1: By using requests.get() method.

Example

import requests

def shorten(url):
  base_url = 'http://tinyurl.com/api-create.php?url='
  response = requests.get(base_url+url)
  short_url = response.text
  return short_url

long_url = 'https://www.example.com/path/to/content/'
short_url = shorten(long_url)
print(short_url)

Output

https://tinyurl.com/2jvveeo5

Initially, the code brings in the `requests` module. The module is commonly utilized with Python to create web requests. The module is utilized for sending a query to the TinyURL interface and getting the condensed URL. Then, the script establishes a function referred to as `shorten ()`. The function accepts a URL as a value and outputs the compact URL. The base URL acts as the beginning of building the request to the API.

In order to generate the abbreviated URL, the software sends an HTTP GET call to the TinyURL application programming interface. This process is by means of combining the `base_url` together with the provided parameter `url`. The `requests` module's `get()` method is employed to initiate the request, by including the constructed URL. The answer from the server query is placed in the `result` variable. In order to extract the abbreviated URL from the server's response, the code obtains the `text` parameter of the response instance. The obtained abbreviated URL is subsequently assigned to the variable denoted as `short_url`.

This lengthy URL gets passed as input to the `shorten()` function as input. The compact URL is then shown using the `print()` command. If you execute this script, the result is the abbreviated URL obtained from the link shortening API for the input `long_url`.

Every time you execute the program, the resulting condensed URL will be distinct. That is because this is based on the reply from the link shortener API. The abbreviated URL can be employed to lead users to the primary extended URL. This enables it simpler to exchange and retain.

Approach 2: Using PyShorteners Approach

Example

import pyshorteners

long_url = 'https://www.example.com/path/to/content/'

s = pyshorteners.Shortener()
short_url = s.tinyurl.short(long_url)
print(short_url)

Output

https://tinyurl.com/abcd123

To begin with, the script brings in the `pyshorteners` module file. The module provides a Python package that supplies URL−shrinking functionality options. The module is utilized to generate the abbreviated web address. Then, the algorithm assigns an extended URL to the variable named `long_url`. These are the initial URL that we aim to truncate.

The script generates a new object of the `Reducer` class utilizing the `is.gd` module. Afterwards sets this instance to the variable `s`. This object is going to be used to retrieve the website shortening functions offered by the toolset.

In order to create the abbreviated URL, the algorithm triggers the `compress()` method on the `s.tinyurl` field. The `short()` function accepts the extended URL as a variable and yields the related abbreviated URL. In this case, the `long_url` variable is sent as the input. The resulting shortened URL is recorded within the `short_url` variable.

In conclusion, the script utilizes the `display()` function to exhibit the compressed URL in the command prompt. If you execute this code, you will get consist of the abbreviated URL created by the `pyshorteners` module corresponding to the given `long_url`. Every time you execute the program, the created compact URL will be distinct. It relies on a particular web address shortening service accessed by the program.

The objective of this program is to show how to use the `shortenurl` library to speedily produce a concise URL from a big URL. It is possible helpful for situations in which you want to distribute a brief, compact edition of a uniform resource locator.

Conclusion

Link shortening has transformed into a crucial component in contemporary communication. This tool assists in creating lengthy and intricate web addresses simpler to manage and more accessible to pass along with friends and colleagues. In this learning post, we have shown how to utilize the short URL API to shrink URLs Python−based. We investigated dual strategies including complete runnable code samples and interpretations.

To sum up, this API provides a simple and trustworthy technique for shortening web addresses using Python with straightforward procedures. The process can be executed within a short time frame. By utilizing of the Python programming language and TinyURL's API, producing shorter web addresses has become easier than ever before. By adhering to the demonstrations given in this post, any novice programmer can generate web address shorteners utilizing the TinyURL API. People can further integrate the items into their assignments.

Updated on: 04-Mar-2024

571 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements