Compare commits

..
3 Commits
Author SHA1 Message Date
imSp4rky 5d4a27b8ca style: apply pre-commit auto-fixes (isort, whitespace, eof) 2026-06-25 11:06:55 -06:00
imSp4rky 06e0593da3 fix(drm): don't switch to a mismatched CDM type during licensing
Guard the CDM switch on the fetched CDM's actual DRM type and raise a clear error on mismatch instead of sending a mismatched challenge.
2026-06-25 11:05:18 -06:00
imSp4rky e96e9be143 feat(output): support nested directories in folder templates
Treat `/` in folder templates as a path separator (sanitize per segment) and let movies use a folder when a `movies` template is set.
2026-06-25 10:40:49 -06:00
8 changed files with 90 additions and 78 deletions
+26 -10
View File
@@ -33,7 +33,7 @@ filenames:
## output_template (dict)
Configure custom output filename templates for movies, series, songs, and albums.
This is **required** in your `unshackle.yaml` — a warning is shown if not configured.
This is **required** in your `unshackle.yaml` - a warning is shown if not configured.
Add `?` suffix to make a variable conditional (omitted when empty): `{year?}`, `{hdr?}`, `{repack?}`
@@ -123,6 +123,15 @@ If not configured, the default folder naming is used:
`folder` accepts either a single string (applies to all title kinds) or a mapping with per-kind
templates keyed by `movies`, `series`, `songs`, and/or `albums`. Unknown keys are warned about and ignored.
Use `/` in a folder template to create nested directories - each segment is sanitized
independently and joined as real path separators. For example `'{source}/Series/{title}.{year?}'`
produces `EXAMPLE/Series/The.Show.2024/`. Note `{source}` is the service **tag** (e.g. `EXAMPLE`,
`EXAMPLE2`), not the display name; it is blank when `--no-source` is used.
Movies only get their own folder when a `movies` folder template is set; without one the movie file
is written directly into the downloads directory (unchanged default behaviour). Series and songs
always get a folder unless `--no-folder` is passed.
Useful music variables for album folders: `album_artist`, `album`, `artist`, `year`, `genre`, `label`, `release_type`, `track_total`, `disc_total`.
```yaml
@@ -143,11 +152,18 @@ output_template:
# series: '{title} ({year?})'
# songs: '{artist} - {album} ({year?})'
# albums: '{album_artist} - {album} ({year?})'
# Nested per-service layout (EXAMPLE/Series/Title.Year/...)
# folder:
# series: '{source}/Series/{title}.{year?}'
# movies: '{source}/Movies/{title}.{year?}'
# songs: '{source}/Music/{album_artist}/{album}.{year?}'
```
Example outputs:
- Scene folder: `Example.Show.2024.S01.1080p.EXAMPLE.WEB-DL.DDP5.1.H.264-TAG/`
- Plex folder: `Example Show (2024)/`
- Nested folder: `EXAMPLE/Series/Example.Show.2024/`
---
@@ -278,7 +294,7 @@ Enable/disable tagging downloaded files with IMDB/TMDB/TVDB identifiers (when av
Override which track is flagged as the default in the muxed MKV, regardless
of the title's original language. Useful when you always want your player to
open on a specific language (e.g. always default to Polish audio even on
English originals). Only affects the MKV `--default-track` flag — track
English originals). Only affects the MKV `--default-track` flag - track
selection (`-l`, `--alang`, etc.) is unchanged. All keys are optional; each
track type falls back to its previous default rule when the configured
language isn't present in the manifest.
@@ -363,21 +379,21 @@ GitHub or any git host (GitLab, Gitea, self-hosted). Local paths and repo specs
```yaml
directories:
services:
- https://github.com/you/your-services # https repo (highest priority — listed first)
- https://github.com/you/your-services # https repo (highest priority - listed first)
- git@gitlab.com:you/your-services.git # ssh repo (private, via your git auth)
- you/your-services@main # owner/repo shorthand + optional @branch
- ~/my-local-services # local path (fallback — listed last)
- ~/my-local-services # local path (fallback - listed last)
```
How it works:
- On first use the repo is cloned (shallow) to `<your-services-dir>/_repos/<repo-name>/` — the first
- On first use the repo is cloned (shallow) to `<your-services-dir>/_repos/<repo-name>/` - the first
local `services` entry, or the bundled `unshackle/services` if you configured none. **Nothing is
written to the `cache` directory.**
- After that, unshackle does **not** hit the network on every run. It re-pulls at most once every 24h,
or immediately when you run `unshackle util refresh-services`.
- Requires `git` on your PATH. Private repos use your existing git credential helper — unshackle
stores no tokens. Git use is **read-only on the remote** — only `clone`, `fetch`, `pull`, and a
- Requires `git` on your PATH. Private repos use your existing git credential helper - unshackle
stores no tokens. Git use is **read-only on the remote** - only `clone`, `fetch`, `pull`, and a
local `reset` are run; nothing is ever pushed.
- **Local edits and refresh.** You can edit a clone under `_repos/` directly. The two refresh paths
treat your edits differently:
@@ -385,16 +401,16 @@ How it works:
changes to tracked files or unpushed local commits, unshackle **refuses to refresh and exits**,
naming the clone, so a background pull never clobbers work in progress. Commit and push it
upstream (or revert), then it refreshes normally.
- *Manual* (`unshackle util refresh-services`): an explicit "get upstream's latest" — it
- *Manual* (`unshackle util refresh-services`): an explicit "get upstream's latest" - it
**hard-resets the clone to upstream, discarding local changes**. Run it only when you want to
throw away local edits.
- Untracked files (new service folders, `__pycache__`) never block the automatic path — a
- Untracked files (new service folders, `__pycache__`) never block the automatic path - a
fast-forward pull doesn't touch them.
- The repo's **top level** must contain `<TAG>/__init__.py` service dirs (same layout as
`unshackle/services/`).
- **Priority is list order.** The first source to define a tag is the one that loads; if a later
source (repo or local) has the same tag, that copy is treated as a duplicate and ignored. So list
the sources you trust most first — e.g. repos first and local last to make local a fallback, or
the sources you trust most first - e.g. repos first and local last to make local a fallback, or
local first to let your local tweaks override a repo.
- **What you see on load.** A one-line summary is logged each run, e.g.:
+3 -11
View File
@@ -6,17 +6,9 @@ from typing import Any, Optional
import pytest
from unshackle.core.music.extract import (
build_music_from_songs,
classify_release_kind,
dedupe_track_options,
duration_seconds,
first_number,
first_text,
format_duration,
format_names,
year_from_value,
)
from unshackle.core.music.extract import (build_music_from_songs, classify_release_kind, dedupe_track_options,
duration_seconds, first_number, first_text, format_duration, format_names,
year_from_value)
from unshackle.core.music.models import MusicTrackOption
from unshackle.core.titles.music import Music, Song
+37 -18
View File
@@ -1422,8 +1422,8 @@ class dl:
titles.year = enrich_year
music_mode = isinstance(titles, Music)
music_collection_mode = isinstance(titles, list) and bool(titles) and all(
isinstance(title, Music) for title in titles
music_collection_mode = (
isinstance(titles, list) and bool(titles) and all(isinstance(title, Music) for title in titles)
)
music_titles = list(titles) if music_collection_mode else ([titles] if music_mode else [])
music_plans = {}
@@ -1558,13 +1558,10 @@ class dl:
and not no_audio
)
if music_group_download:
def download_music_title(titles: Music, plan: Any) -> bool:
music_items: list[tuple[Song, Audio, Callable[..., None]]] = []
music_song_plans = {
id(song_plan.song): song_plan
for disc in plan.discs
for song_plan in disc.songs
}
music_song_plans = {id(song_plan.song): song_plan for disc in plan.discs for song_plan in disc.songs}
music_renderer = MusicRenderer()
music_start_time = time.time()
@@ -1624,7 +1621,9 @@ class dl:
for language in audio_languages:
if language == "orig":
if song.language:
orig_lang = str(song.language) if hasattr(song.language, "__str__") else song.language
orig_lang = (
str(song.language) if hasattr(song.language, "__str__") else song.language
)
if orig_lang not in processed_lang:
processed_lang.append(orig_lang)
else:
@@ -1866,7 +1865,9 @@ class dl:
if not final_path:
continue
integrity_result = integrity_results.get(id(track))
metadata_result = metadata_results.get(id(track), MusicMetadataResult(skipped=True, reason="not processed"))
metadata_result = metadata_results.get(
id(track), MusicMetadataResult(skipped=True, reason="not processed")
)
manifest_records.append(
{
"track_number": song.track,
@@ -1949,7 +1950,9 @@ class dl:
"install mutagen to write music tags",
)
console.print(Padding(f"Metadata skipped: {reason}", (0, 5)))
console.print(Padding(f"{release_label} downloaded in [progress.elapsed]{album_time}[/]!", (0, 5, 1, 5)))
console.print(
Padding(f"{release_label} downloaded in [progress.elapsed]{album_time}[/]!", (0, 5, 1, 5))
)
return True
@@ -1982,9 +1985,7 @@ class dl:
continue
title_rule = (
f"Track {title.track:02}: {title.name}"
if music_mode and isinstance(title, Song)
else str(title)
f"Track {title.track:02}: {title.name}" if music_mode and isinstance(title, Song) else str(title)
)
console.print(Padding(Rule(f"[rule.text]{title_rule}"), (1, 2)))
temp_font_files = []
@@ -3273,7 +3274,14 @@ class dl:
base_filename = str(title)
sidecar_dir = self.output_dir or config.directories.downloads
if not no_folder and isinstance(title, (Episode, Song)) and media_info:
if (
not no_folder
and media_info
and (
isinstance(title, (Episode, Song))
or (isinstance(title, Movie) and config.get_folder_template("movies"))
)
):
sidecar_dir /= title.get_filename(
media_info, show_service=not no_source, folder=True
)
@@ -3327,7 +3335,10 @@ class dl:
if no_mux:
# Handle individual track files without muxing
final_dir = self.output_dir or config.directories.downloads
if not no_folder and isinstance(title, (Episode, Song)):
if not no_folder and (
isinstance(title, (Episode, Song))
or (isinstance(title, Movie) and config.get_folder_template("movies"))
):
# Create folder based on title
# Use first available track for filename generation
sample_track = (
@@ -3384,7 +3395,10 @@ class dl:
final_filename = title.get_filename(media_info, show_service=not no_source)
audio_codec_suffix = muxed_audio_codecs.get(muxed_path)
if not no_folder and isinstance(title, (Episode, Song)):
if not no_folder and (
isinstance(title, (Episode, Song))
or (isinstance(title, Movie) and config.get_folder_template("movies"))
):
final_dir /= title.get_filename(media_info, show_service=not no_source, folder=True)
final_dir.mkdir(parents=True, exist_ok=True)
@@ -3601,22 +3615,26 @@ class dl:
if isinstance(drm, Widevine):
if not is_widevine_cdm(self.cdm):
widevine_cdm = self.get_cdm(self.service, self.profile, drm="widevine", quality=track_quality)
if widevine_cdm:
if widevine_cdm and is_widevine_cdm(widevine_cdm):
if track_quality:
self.log.info(f"Switching to Widevine CDM for Widevine {track_quality}p content")
else:
self.log.info("Switching to Widevine CDM for Widevine content")
self.cdm = widevine_cdm
else:
raise ValueError(f"Title needs a Widevine CDM but {self.service} is configured with PlayReady.")
elif isinstance(drm, PlayReady):
if not is_playready_cdm(self.cdm):
playready_cdm = self.get_cdm(self.service, self.profile, drm="playready", quality=track_quality)
if playready_cdm:
if playready_cdm and is_playready_cdm(playready_cdm):
if track_quality:
self.log.info(f"Switching to PlayReady CDM for PlayReady {track_quality}p content")
else:
self.log.info("Switching to PlayReady CDM for PlayReady content")
self.cdm = playready_cdm
else:
raise ValueError(f"Title needs a PlayReady CDM but {self.service} is configured with Widevine.")
if isinstance(drm, Widevine):
if self.debug_logger:
@@ -4159,6 +4177,7 @@ class dl:
if ff_settings:
try:
from unshackle.core.utils.firefox_cookie_extractor import get_firefox_cookies
extracted_jar = get_firefox_cookies(ff_settings)
if extracted_jar:
return extracted_jar
+2 -17
View File
@@ -15,23 +15,8 @@ except ImportError: # pragma: no cover - optional artwork enhancement
try:
from mutagen.flac import FLAC, Picture
from mutagen.id3 import (
APIC,
COMM,
ID3NoHeaderError,
TALB,
TCOM,
TCON,
TCOP,
TDRC,
TIT2,
TPE1,
TPE2,
TPOS,
TRCK,
TSRC,
TXXX,
)
from mutagen.id3 import (APIC, COMM, TALB, TCOM, TCON, TCOP, TDRC, TIT2, TPE1, TPE2, TPOS, TRCK, TSRC, TXXX,
ID3NoHeaderError)
from mutagen.mp3 import MP3
from mutagen.mp4 import MP4, MP4Cover
except ImportError: # pragma: no cover - optional tagging dependency
+2 -2
View File
@@ -110,7 +110,7 @@ class Episode(Title):
separators = re.sub(r"\{[^}]*\}", "", template)
spacer = "." if "." in separators and " " not in separators else " "
return sanitize_filename(folder_name, spacer)
return "/".join(sanitize_filename(p, spacer) for p in folder_name.split("/") if p)
series_template = config.output_template.get("series")
if series_template:
@@ -160,7 +160,7 @@ class Series(SortedKeyList, ABC):
sum(seasons.values())
season_breakdown = ", ".join(f"S{season}({count})" for season, count in sorted(seasons.items()))
tree = Tree(
f"{num_seasons} season{'s'[:num_seasons^1]}, {season_breakdown}",
f"{num_seasons} season{'s'[: num_seasons ^ 1]}, {season_breakdown}",
guide_style="bright_black",
)
if verbose:
+1 -1
View File
@@ -68,7 +68,7 @@ class Movie(Title):
separators = re.sub(r"\{[^}]*\}", "", template)
spacer = "." if "." in separators and " " not in separators else " "
return sanitize_filename(folder_name, spacer)
return "/".join(sanitize_filename(p, spacer) for p in folder_name.split("/") if p)
name = f"{self.name}"
if self.year:
name += f" ({self.year})"
+1 -1
View File
@@ -103,7 +103,7 @@ class Song(Title):
separators = re.sub(r"\{[^}]*\}", "", template)
spacer = "." if "." in separators and " " not in separators else " "
return sanitize_filename(folder_name, spacer)
return "/".join(sanitize_filename(p, spacer) for p in folder_name.split("/") if p)
name = f"{self.artist} - {self.album}"
if self.year:
name += f" ({self.year})"
@@ -3,14 +3,14 @@ Firefox Cookie and LocalStorage Extractor Utility for unshackle.
Provides a secure, read-only mechanism to extract authentication tokens from Firefox.
"""
import sqlite3
import platform
import os
import platform
import shutil
import sqlite3
import tempfile
from http.cookiejar import Cookie, CookieJar
from pathlib import Path
from http.cookiejar import CookieJar, Cookie
from typing import Dict, Optional, List
from typing import Dict, List, Optional
def get_firefox_root() -> Path:
@@ -41,7 +41,7 @@ def get_latest_profile_path(ff_root: Path) -> Path:
for base in search_dirs:
if base.exists():
valid_profiles.extend([p for p in base.iterdir() if p.is_dir() and (p / 'cookies.sqlite').exists()])
if not valid_profiles:
raise FileNotFoundError("No active Firefox profiles detected.")
@@ -56,29 +56,29 @@ def get_local_storage_data(profile_path: Path, hosts: List[str], tmp_dir_path: P
ls_db = profile_path / 'webappsstore.sqlite'
if not ls_db.exists():
return {}
temp_ls = tmp_dir_path / "webappsstore.sqlite"
extracted_storage = {}
try:
shutil.copy2(ls_db, temp_ls)
conn = sqlite3.connect(temp_ls)
cursor = conn.cursor()
for host in hosts:
# Firefox stores LocalStorage as reversed host (e.g., moc.viki.:https:443)
# Use strict prefix matching to prevent over-broad harvesting
reversed_host = host[::-1]
query = "SELECT key, value FROM webappsstore2 WHERE originKey LIKE ?"
cursor.execute(query, (f"{reversed_host}.%",))
for key, value in cursor.fetchall():
extracted_storage[key] = value
conn.close()
except Exception:
pass
return extracted_storage
@@ -99,14 +99,14 @@ def get_firefox_cookies(service_settings: dict) -> Optional[CookieJar]:
profile_path = get_latest_profile_path(get_firefox_root())
except Exception:
return None
cookie_jar = CookieJar()
# Use a secure temporary directory with restricted permissions (Finding #5)
with tempfile.TemporaryDirectory(prefix="unshackle_ff_") as tmp_dir:
tmp_dir_path = Path(tmp_dir)
os.chmod(tmp_dir, 0o700)
os.chmod(tmp_dir, 0o700)
temp_db = tmp_dir_path / "cookies.sqlite"
temp_wal = tmp_dir_path / "cookies.sqlite-wal"
@@ -123,8 +123,8 @@ def get_firefox_cookies(service_settings: dict) -> Optional[CookieJar]:
for host in priority_hosts:
# Precise matching: exact host or dot-prefixed domain suffix (Finding #2)
query = """
SELECT host, name, value, path, expiry, isSecure, isHttpOnly
FROM moz_cookies
SELECT host, name, value, path, expiry, isSecure, isHttpOnly
FROM moz_cookies
WHERE host = ? OR host LIKE ?
"""
cursor.execute(query, (host, f"%.{host}"))
@@ -137,7 +137,7 @@ def get_firefox_cookies(service_settings: dict) -> Optional[CookieJar]:
version=0, name=n, value=v, port=None, port_specified=False,
domain=h, domain_specified=True, domain_initial_dot=h.startswith('.'),
path=p, path_specified=True, secure=bool(secure), expires=e,
discard=False, comment=None, comment_url=None,
discard=False, comment=None, comment_url=None,
rest={'HttpOnly': str(bool(httponly))}, rfc2109=False
)
cookie_jar.set_cookie(c)
@@ -158,4 +158,4 @@ def get_firefox_cookies(service_settings: dict) -> Optional[CookieJar]:
except Exception:
return None
return cookie_jar if len(list(cookie_jar)) > 0 else None
return cookie_jar if len(list(cookie_jar)) > 0 else None