Skip to main content

main.py

import datetime
import os
from py_secure_edge import custom_token


def get_timedelta(str_val = ''):
    if str_val.endswith("m"):
        return datetime.timedelta(minutes=int(str_val[0:-1]))
    if str_val.endswith("h"):
        return datetime.timedelta(hours=int(str_val[0:-1]))
    if str_val.endswith("d"):
        return datetime.timedelta(days=int(str_val[0:-1]))
    return int(str_val)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    url = os.environ["URL"]
    key = os.environ["KEY"]
    key_id = os.environ["KEY_ID"]
    exp = os.environ.get("EXP", "24h")
    firstExp = os.environ.get("FIRST_EXP", "5m")
    country = os.environ.get("CO", "")
    region = os.environ.get("REG", "")
    no_session = os.environ.get("NO_SSN", False)
    user_agent = os.environ.get("UA", "")
    referer = os.environ.get("REF", "")

    token_policy = custom_token.TokenPolicy()
    token_policy.expiry = get_timedelta(exp)
    token_policy.first_access_expiry = get_timedelta(firstExp)
    token_policy.session = not no_session
    token_policy.country = len(country) > 0
    token_policy.region = len(region) > 0
    token_policy.headers = []
    if len(user_agent) > 0:
        token_policy.headers.append("user-agent")
    if len(referer) > 0:
        token_policy.headers.append("referer")

    viewer_attrs = custom_token.ViewerAttributes()
    viewer_attrs.country = country
    viewer_attrs.region = region
    viewer_attrs.headers = {
        "user-agent": user_agent,
        "referer": referer,
    }

    custom_token = custom_token.CustomToken(secrets={key_id: key})

    url_with_token = custom_token.generate_url(url, key_id, token_policy, viewer_attrs)
    print(url_with_token)
URL=https://your-media-endpoint.com/master.m3u8 \
  KEY_ID=your-key-id KEY=your-key \
  UA="your-user-agent" \
  REF=your-referrer \
  python3 main.py

Metadata

Release files for py-secure-edge 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-secure-edge 0.0.2
File Size Uploaded
py_secure_edge-0.0.2.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-secure-edge 0.0.2
File Interpreter ABI Platform
py_secure_edge-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.2 kB

Release files / py_secure_edge-0.0.2.tar.gz

Download URL py_secure_edge-0.0.2.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a3472333b64de0d4a1b42263b5135d9826f4ae85e5e3eec051dfbf92e86674d1
BLAKE2b-256 checksum
How to use checksums
d1e46258c5653975ef8a8a9717d3874310097c62c1731e6428c8dbd6496da070
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / py_secure_edge-0.0.2-py3-none-any.whl

Download URL py_secure_edge-0.0.2-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a79a5e9a133871a69723c910f2ab099b695286ea70e723137b80e5c1598bdf38
BLAKE2b-256 checksum
How to use checksums
0b8bf400e738835de3f5697be047af4e14a624e3fad93aec7cbb67588a98e60b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page