Skip to content

URL Expander

Abstract

URL Expander is a Python project that takes shortened URLs and expands them to their original form. It demonstrates API usage, HTTP requests, and GUI development. This project is ideal for learning about web APIs, networking, and user interaction.

Prerequisites

  • Python 3.6 or above
  • requests (pip install requestspip install requests)
  • tkinter (usually pre-installed)

Before you Start

Install Python and requests. Prepare a list of shortened URLs for testing.

Getting Started

  1. Create a folder named url-expanderurl-expander.
  2. Create a file named url_expander.pyurl_expander.py.
  3. Copy the code below into your file.
⚙️ URL Expander
URL Expander
"""
URL Expander
 
A Python application that expands shortened URLs to their original form.
Features include:
- Accepting a shortened URL as input.
- Displaying the expanded URL.
"""
 
import requests
from tkinter import Tk, Label, Entry, Button, messagebox
 
 
def expand_url(short_url):
    """Expand a shortened URL to its original form."""
    try:
        response = requests.head(short_url, allow_redirects=True)
        return response.url
    except requests.RequestException as e:
        return str(e)
 
 
class URLExpanderApp:
    def __init__(self, root):
        self.root = root
        self.root.title("URL Expander")
 
        Label(root, text="Enter Shortened URL:").grid(row=0, column=0, padx=10, pady=10)
        self.url_entry = Entry(root, width=50)
        self.url_entry.grid(row=0, column=1, padx=10, pady=10)
 
        Button(root, text="Expand URL", command=self.expand_url).grid(row=1, column=0, columnspan=2, pady=10)
 
    def expand_url(self):
        """Handle the button click to expand the URL."""
        short_url = self.url_entry.get()
        if not short_url:
            messagebox.showerror("Error", "Please enter a URL.")
            return
 
        expanded_url = expand_url(short_url)
        messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")
 
 
def main():
    root = Tk()
    app = URLExpanderApp(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
URL Expander
"""
URL Expander
 
A Python application that expands shortened URLs to their original form.
Features include:
- Accepting a shortened URL as input.
- Displaying the expanded URL.
"""
 
import requests
from tkinter import Tk, Label, Entry, Button, messagebox
 
 
def expand_url(short_url):
    """Expand a shortened URL to its original form."""
    try:
        response = requests.head(short_url, allow_redirects=True)
        return response.url
    except requests.RequestException as e:
        return str(e)
 
 
class URLExpanderApp:
    def __init__(self, root):
        self.root = root
        self.root.title("URL Expander")
 
        Label(root, text="Enter Shortened URL:").grid(row=0, column=0, padx=10, pady=10)
        self.url_entry = Entry(root, width=50)
        self.url_entry.grid(row=0, column=1, padx=10, pady=10)
 
        Button(root, text="Expand URL", command=self.expand_url).grid(row=1, column=0, columnspan=2, pady=10)
 
    def expand_url(self):
        """Handle the button click to expand the URL."""
        short_url = self.url_entry.get()
        if not short_url:
            messagebox.showerror("Error", "Please enter a URL.")
            return
 
        expanded_url = expand_url(short_url)
        messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")
 
 
def main():
    root = Tk()
    app = URLExpanderApp(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
  1. Run the script: python url_expander.pypython url_expander.py

Explanation

Code Breakdown

  1. Import required modules.
import requests
import tkinter as tk
import requests
import tkinter as tk
  1. Expand shortened URL.
response = requests.head(short_url, allow_redirects=True)
expanded_url = response.url
response = requests.head(short_url, allow_redirects=True)
expanded_url = response.url
  1. Display result in GUI.
root = tk.Tk()
root.title('URL Expander')
# ...setup widgets for input and display...
root.mainloop()
root = tk.Tk()
root.title('URL Expander')
# ...setup widgets for input and display...
root.mainloop()

Features

  • Expands shortened URLs
  • GUI for user interaction
  • Easy to extend for more features

How It Works

  • User enters shortened URL
  • App expands and displays original URL

GUI Components

  • Entry box: For URL input
  • Button: Expands URL
  • Label: Shows expanded URL

Use Cases

  • Reveal original links
  • Learn API and HTTP requests
  • Build web utilities

Next Steps

You can enhance this project by:

  • Supporting batch expansion
  • Logging expanded URLs
  • Improving GUI design
  • Adding URL validation

Enhanced Version Ideas

def expand_batch():
    # Expand multiple URLs at once
    pass
 
def log_expansions():
    # Save expanded URLs to file
    pass
def expand_batch():
    # Expand multiple URLs at once
    pass
 
def log_expansions():
    # Save expanded URLs to file
    pass

Troubleshooting Tips

  • Invalid URL: Check input format
  • No expansion: Check internet connection
  • GUI not showing: Ensure Tkinter is installed

Conclusion

This project teaches API usage, HTTP requests, and GUI basics. Extend it for more features and better user experience.

Was this page helpful?

Let us know how we did