sky_download.py — SkyShowtime Widevine downloader
===================================================

REQUIREMENTS
------------
- Python 3.9+
- L3/          directory containing a valid .wvd Widevine device file
- tools/       directory containing:
    N_m3u8DL-RE   (DASH downloader)
    mp4decrypt     (decryption)
- pip packages: requests, pywidevine

Install dependencies:
    pip install requests pywidevine


BASIC USAGE
-----------
    python3 sky_download.py <URL>

Where <URL> is any SkyShowtime watch URL, for example:

  Movie:
    python3 sky_download.py https://www.skyshowtime.com/watch/asset/movies/dune/abc123...

  TV series:
    python3 sky_download.py https://www.skyshowtime.com/watch/asset/tv/star-trek-enterprise/abc123...

On first run you will be prompted for your SkyShowtime email and password.
Credentials are saved to .sky_creds (chmod 600) so you are not asked again.


LOGIN & SESSION
---------------
After the first successful login a session is saved to .sky_session (chmod 600).
Subsequent runs reuse the saved session — no new login, no new device registration,
no notification emails from SkyShowtime.

The session is refreshed automatically when the token expires.

To force a fresh login (e.g. after a password change):
    rm .sky_session

To force re-entering credentials:
    rm .sky_creds .sky_session


CREDENTIALS VIA ENVIRONMENT VARIABLES
--------------------------------------
    export SKY_USER=me@email.com
    read -rs SKY_PASS && export SKY_PASS
    python3 sky_download.py <URL>

This avoids the password appearing in shell history.


COMMAND-LINE OPTIONS
--------------------
  -u USERNAME   SkyShowtime email (or set SKY_USER env var)
  -p PASSWORD   SkyShowtime password (or set SKY_PASS env var)
  -o OUTPUT     Output filename without extension (default: video title)
  -v RES        Video quality: e.g. "1920x1080", "best", "worst"
  -a LANGS      Audio tracks: comma-separated lang codes, "all", or "none"
  -s LANGS      Subtitle tracks: comma-separated lang codes, "all", or "none"

When -v / -a / -s are omitted and the terminal is interactive, a curses
picker is shown to choose quality, audio tracks, and subtitles.


INTERACTIVE PICKERS (terminal mode)
------------------------------------
- Video quality:    single-select, highest resolution pre-selected
- Audio tracks:     multi-select by language, then format (AAC / E-AC3)
- Subtitles:        multi-select by language (none pre-selected)
- Episode (series): single-select with "[All episodes]" pre-selected

Controls in all pickers:
  Arrow keys / j/k   Navigate
  Space               Toggle selection
  a / n               Select all / none (multi-select only)
  Enter               Confirm


TV SERIES
---------
When a series URL is given the script lists all available seasons and
episodes. You can download a single episode or all at once.

The quality, audio, and subtitle settings are chosen once (from the first
episode's track list) and applied to every episode downloaded.

Output filenames follow Plex naming: s01e03_Episode_Title.mkv


OUTPUT FILES
------------
All files are saved to the downloads/ directory next to the script.

  Movie:   Title.mkv
  Series:  s01e03_Episode_Title.mkv

Subtitles are saved as external .srt files (never embedded in the MKV),
compatible with Plex automatic subtitle matching:

  Title.pl.srt              regular subtitle
  Title.pl.forced.srt       forced subtitle
  Title.en.srt
  s01e03_Title.pl.srt       series episode subtitle


NON-INTERACTIVE (BATCH) EXAMPLES
----------------------------------
Download best video, all audio, no subtitles:
    python3 sky_download.py -v best -a all -s none <URL>

Polish + English audio, Polish subtitles, 1080p:
    python3 sky_download.py -a pl-PL,en-US -s pl-PL -v 1920x1080 <URL>

Download all episodes of a series, all audio, all subs:
    python3 sky_download.py -a all -s all <SERIES_URL>

Custom output name:
    python3 sky_download.py -o "Dune_2021" <URL>


FILES CREATED BY THE SCRIPT
-----------------------------
  .sky_creds      Saved login credentials (chmod 600)
  .sky_session    Saved session token / device ID (chmod 600)
  downloads/      Downloaded .mkv and .srt files
