Skip to content

SemTiOne/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener 🔗

A simple command-line URL shortener built with Python and MySQL. Shorten long URLs, track click statistics, and manage your links efficiently.

Features

  • ✂️ Shorten long URLs into compact codes
  • 📊 Track click statistics for each URL
  • 🔍 Expand short codes back to original URLs
  • 📝 List all shortened URLs
  • 💾 Persistent storage with MySQL database

Technologies Used

  • Python 3.14.3 - Core application logic
  • MySQL/MariaDB - Database for storing URLs
  • mysql-connector-python - MySQL database connector

Prerequisites

Before running this project, make sure you have:

  • Python 3 or higher installed
  • MySQL or MariaDB server running
  • pip (Python package manager)

Installation

  1. Clone the repository
git clone https://github.com/SemTiOne/url-shortener.git
cd url-shortener
  1. Install dependencies
pip install -r requirements.txt
  1. Configure database

Open url_shortener.py and update the database credentials:

self.connection = mysql.connector.connect(
    host='localhost',
    user='root',          # Change to your MySQL username
    password='',          # Change to your MySQL password
    database='url_shortener'
)
  1. Run the application
python url_shortener.py

The application will automatically create the database and table on first run.

Usage

Main Menu

1. Shorten a URL - Create a short code for a long URL
2. Expand a short code - Get the original URL from a short code
3. View statistics - See clicks and details for a URL
4. List all URLs - Display all shortened URLs
5. Exit - Close the application

Example Usage

Shortening a URL:

Enter URL to shorten: https://www.example.com/very/long/url/path

✓ URL shortened successfully!

Original: https://www.example.com/very/long/url/path
Shortened: short.ly/aBc123
Short Code: aBc123

Expanding a short code:

Enter short code: aBc123

✓ Found URL!
Short Code: aBc123
Original URL: https://www.example.com/very/long/url/path

Database Schema

CREATE TABLE urls (
    id INT AUTO_INCREMENT PRIMARY KEY,
    original_url VARCHAR(2048) NOT NULL,
    short_code VARCHAR(10) UNIQUE NOT NULL,
    clicks INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Project Structure

url-shortener/
├── 🐍 url_shortener.py    # Main application file
├── 🗄️ setup.sql           # Database setup script
├── 📦 requirements.txt    # Python dependencies
├── 📄 LICENSE             # MIT license
├── 📄 .gitignore          # Git exclusions
└── 📖 README.md           # This file

Features Breakdown

1. URL Shortening

  • Generates random 6-character alphanumeric codes
  • Checks for duplicate URLs (returns existing short code)
  • Ensures unique short codes

2. Click Tracking

  • Increments counter each time a short code is accessed
  • Persistent storage in database

3. Statistics

  • View creation date
  • Track total clicks
  • Display original and shortened URLs

4. List Management

  • View all shortened URLs at once
  • Display click counts
  • Sort by creation date

Possible Enhancements

Here are some ideas to expand this project:

  • Custom short codes (let users choose their own)
  • Expiration dates for URLs
  • QR code generation for shortened URLs
  • Web interface using Flask/FastAPI
  • URL validation before shortening
  • Analytics dashboard with charts
  • User authentication and accounts
  • API endpoints for programmatic access

Troubleshooting

"Error connecting to database"

  • Make sure MySQL/MariaDB is running
  • Check your database credentials in url_shortener.py
  • Verify MySQL is accessible on localhost:3306

"Module not found: mysql.connector"

  • Install the required package: pip install mysql-connector-python

"Access denied for user"

  • Update the username and password in the code
  • Grant necessary permissions in MySQL

License

This project is open source and available under the MIT License.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

Author

Dane Parin - @SemTiOne


Note: This is a learning project designed to demonstrate Python and MySQL integration. For production use, consider adding security features like input validation, SQL injection prevention, and HTTPS for the shortened URLs.

About

A simple CLI URL shortener built with Python and MySQL. Shorten URLs, track clicks, and manage links efficiently.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages