CDA.PL VIDEO DOWNLOADER
=======================

Downloads DRM-protected videos from cda.pl. Requires a CDA account (free or premium,
depending on the content). Uses Widevine L3 decryption — no browser or emulator needed.


REQUIREMENTS
------------
Python packages:   pip install requests beautifulsoup4 pywidevine
System tool:       ffmpeg  (must be in PATH, used for muxing)
Included tools:    tools/N_m3u8DL-RE   (DASH downloader)
                   tools/mp4decrypt     (segment decryptor)
Device file:       L3/*.wvd            (Widevine L3 key — already extracted)


BASIC USAGE
-----------
  python3 cda_download.py <url>

The first time you run it, you will be prompted for your CDA login (email) and
password. These are saved to .cda_creds in this directory (chmod 600, readable
only by you) so you won't be asked again on subsequent runs.

Example:
  python3 cda_download.py https://www.cda.pl/video/56706683e/vfilm


FINDING THE VIDEO URL
---------------------
Open the video page on cda.pl in your browser. Copy the URL from the address bar.
It looks like:

  https://www.cda.pl/video/56706683e/vfilm

The video ID is the alphanumeric segment after /video/ (here: 56706683e).


OPTIONS
-------
  -u, --username EMAIL    CDA login email (skips the prompt)
  -p, --password PASS     CDA password (skips the prompt)
  -o, --output NAME       Output filename without extension (default: video title)

Credentials can also be provided via environment variables to avoid passing them
on the command line:

  export CDA_USER=your@email.com
  read -rs CDA_PASS && export CDA_PASS
  python3 cda_download.py <url>

Priority order: command-line flags > environment variables > saved .cda_creds file > interactive prompt.


OUTPUT
------
Files are saved to the downloads/ subdirectory as .mkv:

  downloads/Goscie_S01E01_Lektor_PL.mkv

The filename is taken from the video page title and sanitised (spaces and special
characters replaced with underscores). Use -o to override.


HOW IT WORKS
------------
1. Logs in to cda.pl to obtain a session cookie.
2. Fetches the video page and extracts the DASH manifest URL and Widevine
   license URL from the embedded player_data JSON.
3. Downloads the DASH manifest (MPD) and extracts the Widevine PSSH box.
4. Sends a license challenge to DRMtoday (cda.pl's DRM provider) using the
   Widevine L3 device file — receives the content decryption key.
5. Passes the key to N_m3u8DL-RE, which downloads the encrypted DASH segments,
   decrypts them with mp4decrypt, and muxes the result into an MKV file.


TROUBLESHOOTING
---------------
"Wrong CDA username or password."
    Check your credentials. If your password contains special characters, use
    the interactive prompt rather than passing -p on the command line.

"Login blocked by CAPTCHA"
    CDA sometimes requires a CAPTCHA after repeated failed logins. Wait a few
    minutes and try again, or log in manually via the browser first.

"No DASH manifest in player_data"
    The video may be unavailable in your region, not yet released, or requires
    a higher account tier than you have.

"License server returned no content keys."
    The DRMtoday license request failed. Your session token may have expired —
    delete .cda_creds and re-enter your credentials.

To update saved credentials (e.g. after a password change):
    rm .cda_creds
    python3 cda_download.py <url>   # will prompt again and save fresh credentials
