Skip to main content

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


Download files

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

Source Distribution

ldc-urlshort-python2-0.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

ldc_urlshort_python2-0.0.1-py2-none-any.whl (7.6 kB view details)

Uploaded Python 2

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

Hashes for ldc-urlshort-python2-0.0.1.tar.gz
Algorithm Hash digest
SHA256 61c4fe26734e353a6ef9642df9e14e7c910328489fb61d40590538b72759ec5c
MD5 5ce8296d8e2de02fed3b7509716a826a
BLAKE2b-256 1036fdc55a0c184dfec3ec326b218d10ab8b92ded9fbad351c702c36c889d474

See more details on using hashes here.

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

Hashes for ldc_urlshort_python2-0.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 4edef790478c25516a93ecb197034e40b87e40815633d2af07422df73abbbd27
MD5 0b224df10e24d4f87155049baefcfe9d
BLAKE2b-256 98f0c63d0988343d7fa847bfb5760492bbea14ba7c6f59d5e8ba442a471320b8

See more details on using hashes here.

Supported by

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