Skip to content

Latest commit

 

History

History
191 lines (131 loc) · 3.81 KB

File metadata and controls

191 lines (131 loc) · 3.81 KB

URL Shortener Library

A simple and efficient URL shortening library implemented in Python and Go.

Features

  • Shorten URLs with multiple algorithms (hash-based, random)
  • Expand short codes back to original URLs
  • Click statistics tracking
  • Thread-safe operations (Go implementation)
  • Duplicate detection - same URL returns same short code
  • Simple API - easy to use

Supported Languages

  • Python (3.9, 3.10, 3.11)
  • Go (1.20, 1.21, 1.22)

Installation

Python

cd python
pip install -r requirements.txt

Go

cd go
go mod download

Usage

Python

from url_shortener import URLShortener

# Create shortener instance
shortener = URLShortener()

# Shorten a URL (hash method)
short_code = shortener.shorten("https://www.example.com/very/long/url", method="hash")
print(f"Short code: {short_code}")

# Shorten a URL (random method)
short_code = shortener.shorten("https://www.example.com/another/url", method="random")

# Expand short code
original_url = shortener.expand(short_code)
print(f"Original URL: {original_url}")

# Get statistics
clicks = shortener.get_stats(short_code)
print(f"Clicks: {clicks}")

Go

package main

import (
    "fmt"
    "github.com/yourusername/urlshortener"
)

func main() {
    // Create shortener instance
    shortener := urlshortener.NewURLShortener()

    // Shorten a URL (hash method)
    shortCode, err := shortener.Shorten("https://www.example.com/very/long/url", "hash")
    if err != nil {
        panic(err)
    }
    fmt.Printf("Short code: %s\n", shortCode)

    // Expand short code
    originalURL, exists := shortener.Expand(shortCode)
    if exists {
        fmt.Printf("Original URL: %s\n", originalURL)
    }

    // Get statistics
    clicks, exists := shortener.GetStats(shortCode)
    if exists {
        fmt.Printf("Clicks: %d\n", clicks)
    }
}

API Reference

Python

URLShortener()

Create a new URLShortener instance.

shorten(long_url: str, method: str = "hash") -> str

Shorten a long URL.

  • Parameters:
    • long_url: The URL to shorten
    • method: Either "hash" or "random" (default: "hash")
  • Returns: Short code string
  • Raises: ValueError if URL is empty or method is invalid

expand(short_code: str) -> Optional[str]

Expand a short code to its original URL.

  • Parameters:
    • short_code: The shortened code
  • Returns: Original URL or None if not found

get_stats(short_code: str) -> Optional[int]

Get click statistics for a short code.

  • Parameters:
    • short_code: The shortened code
  • Returns: Number of clicks or None if not found

Go

NewURLShortener() *URLShortener

Create a new URLShortener instance.

Shorten(longURL string, method string) (string, error)

Shorten a long URL.

  • Parameters:
    • longURL: The URL to shorten
    • method: Either "hash" or "random"
  • Returns: Short code and error (if any)

Expand(shortCode string) (string, bool)

Expand a short code to its original URL.

  • Parameters:
    • shortCode: The shortened code
  • Returns: Original URL and existence flag

GetStats(shortCode string) (int, bool)

Get click statistics for a short code.

  • Parameters:
    • shortCode: The shortened code
  • Returns: Number of clicks and existence flag

Running Tests

Python

cd python
python -m pytest test_url_shortener.py -v

Go

cd go
go test -v

Algorithm Details

Hash Method

  • Uses MD5 hash of the URL
  • Takes first 6 characters of the hash
  • Same URL always produces the same short code
  • Fast and deterministic

Random Method

  • Generates random 6-character code
  • Uses alphanumeric characters (a-z, A-Z, 0-9)
  • Checks for collisions before assignment
  • Different code each time (unless URL already shortened)