A simple package to shorten URLs
Project description
URL Shortener Package
This package provides utility functions as well as views for shortening urls and redirecting to original urls.
Installation
You can install this package using pip:
## required for reading env file
pip install django-environ
## migration file is included in the package.
## Need run following cmd to migrate
python manage.py migrate
Configuration
Setup env file:
URLSHORT_SCHEME=http/https
URLSHORT_DOMAIN=127.0.0.1:9000/your_endpoint
specify the domain name for shortened url
note: domain should include endpoint specified for urlshort.urls in settings.py
URLSHORT_EXPIRES_AT=2
specify the validity of url in days
Add the following in your project's settings.py file:
## add following lines if not present else ignore
import environ
env = environ.Env()
environ.Env.read_env()
## add urlshort in INSTALLED_APPS
INSTALLED_APPS = [
... ,
'urlshort_python2'
]
If using .env file
URLSHORT = {
'URLSHORT_SCHEME': env('URLSHORT_SCHEME'),
'URLSHORT_DOMAIN': env('URLSHORT_DOMAIN'),
'EXPIRES_AT': env('URLSHORT_EXPIRES_AT')
}
If using secrets.json file
URLSHORT = {
'URLSHORT_SCHEME': get_secret('URLSHORT_SCHEME'),
'URLSHORT_DOMAIN': get_secret('URLSHORT_DOMAIN'),
'EXPIRES_AT': get_secret('URLSHORT_EXPIRES_AT')
}
Usage
Valid URL should always end with backlash: https://www.google.com/ Following URL will be considered invalid: http://www.google.com
Using views:
Add following in your project's urls.py file:
urlpatterns = [
... ,
path('your_endpoint/', include('urlshort_python2.urls'))
]
shorten_url
ENDPOINT = defined in urlpatterns for the package's urls
POST request:
curl --location 'your_server_domain/ENDPOINT/apis/url/create' \
--data '{
"original_url": "https://www.postgresql.org/docs/7.3/arrays.html"
}
for example:
curl --location 'your_server_domain/your_endpoint/apis/url/create' \
--data '{
"original_url": "https://www.postgresql.org/docs/7.3/arrays.html"
}
SCHEME and DOMAIN = defined in your env/secrets file
response:
{
"status": "01",
"data": {
"created_at": "2023-10-25T12:00:12.623Z",
"original_url": "https://www.postgresql.org/docs/7.3/arrays.html",
"short_url": "SCHEME://DOMAIN/ENDPOINT/CWmPYIc",
"expires_at": "2023-10-26T12:00:12.621Z",
"alias": "CWmPYIc",
"domain": "DOMAIN",
"is_active": true,
"hit_count": 0
}
}
Using helper functions:
shorten url:
original_url = "https://www.google.com/"
from urlshort_python2.helpers import shorten_url
short_url = shorten_url(value)
get_original_url:
note: get_original_url function takes in unique_id as argument:
unqiue_id = Pq3E8yy
from urlshort_python2.helpers import get_original_url
original_url = get_original_url(unique_id)
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 Distribution
Built Distribution
File details
Details for the file ldc-urlshort-python2-0.0.1.tar.gz
.
File metadata
- Download URL: ldc-urlshort-python2-0.0.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61c4fe26734e353a6ef9642df9e14e7c910328489fb61d40590538b72759ec5c |
|
MD5 | 5ce8296d8e2de02fed3b7509716a826a |
|
BLAKE2b-256 | 1036fdc55a0c184dfec3ec326b218d10ab8b92ded9fbad351c702c36c889d474 |
File details
Details for the file ldc_urlshort_python2-0.0.1-py2-none-any.whl
.
File metadata
- Download URL: ldc_urlshort_python2-0.0.1-py2-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.8.3 requests/2.27.1 setuptools/44.1.1 requests-toolbelt/1.0.0 tqdm/4.64.1 CPython/2.7.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4edef790478c25516a93ecb197034e40b87e40815633d2af07422df73abbbd27 |
|
MD5 | 0b224df10e24d4f87155049baefcfe9d |
|
BLAKE2b-256 | 98f0c63d0988343d7fa847bfb5760492bbea14ba7c6f59d5e8ba442a471320b8 |