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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_link_url_shortener-1.0-py3-none-any.whl.
File metadata
- Download URL: django_link_url_shortener-1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0dd014ecbac2a07d46c3a523fe835d061179570a7eaa6d4902de27c63609b494
|
|
| MD5 |
11ebbf0041e4ba781cf01e5df52e474f
|
|
| BLAKE2b-256 |
de129e1096378d0f0c8a2ba3b85104bb175c2829387a22f449524c9884cba0b1
|