URL Shortner
URL Shortener
Description
This Python script provides a simple URL shortening functionality using the TinyURL API. It takes a long (big) URL as input and returns a shortened URL that redirects to the original URL.
How It Works
The script imports the
postmethod from therequestsmodule, which is used to send HTTP requests.It defines a function
TinyShortner(big_url), which takes a single argumentbig_url(the long URL to be shortened).Inside this function, it sends a POST request to the TinyURL API with the long URL as data. The API responds with a shortened URL, which is then returned by the function.
If the script is run as the main module, it prompts the user to input a long URL, then passes this URL to the
TinyShortnerfunction.The shortened URL is then printed to the console.
Source Code: tiny-url-shortner.py
from requests import post
# requires API from tinyurl.com
def TinyShortner(big_url: str) -> str:
"""
Function short the big urls to tiny by Tiny Api
"""
return post("https://tinyurl.com/api-create.php", data={"url": big_url}).text
if __name__ == "__main__":
url = input("Enter the Big Url to short: ")
print(TinyShortner(url))