Skip to main content

A professional URL shortener utility for Django projects.

Project description

Django URL Shortener

A professional and flexible URL shortener utility for Django projects.

Features

  • Random or Hash-Based Shortening: You can choose between random string generation or hash-based shortening for URLs.
  • Easy to Use: Simple interface for shortening and resolving URLs.
  • Customizable: Extendable strategy pattern for different URL shortening methods.
  • Validation: Built-in URL validation before shortening.
  • Tested: Includes unit tests to ensure functionality.

Installation

To install the package, you can simply use pip:

pip install django-url-shortener

Usage :

  • After installing the package, you can use it in your Django project or any other Python project.

Basic Example :

Here’s a simple example of how to use the URL shortener:
from shortener.shortener import URLShortener

# Create an instance of the URLShortener
shortener = URLShortener()

# Shorten a URL
original_url = "https://www.google.com"
short_url = shortener.shorten(original_url)
print(f"Shortened URL: {short_url}")

# Resolve the shortened URL back to the original
resolved_url = shortener.resolve(short_url)
print(f"Original URL: {resolved_url}")

Using a Hash-Based Strategy :

By default, the shortener uses a random string strategy. However, you can switch to a hash-based strategy as follows:
from shortener.shortener import URLShortener
from shortener.strategies.hash_strategy import HashShorteningStrategy

# Use the hash-based strategy
hash_shortener = URLShortener(strategy=HashShorteningStrategy())

# Shorten a URL
short_url = hash_shortener.shorten("https://www.example.com")
print(f"Shortened URL using hash: {short_url}")

Error Handling :

The URLShortener will raise an InvalidURLException if the input URL is not valid:
from shortener.shortener import URLShortener
from shortener.exceptions import InvalidURLException

shortener = URLShortener()

try:
    shortener.shorten("invalid-url")
except InvalidURLException as e:
    print(f"Error: {e}")

Customization :

You can create your own URL shortening strategy by subclassing BaseShorteningStrategy:
from shortener.strategies.base_strategy import BaseShorteningStrategy

class CustomShorteningStrategy(BaseShorteningStrategy):
    def shorten(self, original_url):
        return "custom-short-url"

# Use the custom strategy
custom_shortener = URLShortener(strategy=CustomShorteningStrategy())
short_url = custom_shortener.shorten("https://www.example.com")
print(f"Custom Shortened URL: {short_url}")

Running Tests :

The package includes unit tests for verifying functionality. To run the tests:
python -m unittest discover tests

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_link_url_shortener-1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page