From 19ff20061716ec0dbf7cb43b485eab3ad079f121 Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 20 Aug 2025 05:10:38 +0000 Subject: [PATCH] refactor(drm): Simplify decrypt method by removing unused parameter and streamline logic --- unshackle/commands/dl.py | 18 +++++++++--------- unshackle/core/drm/playready.py | 7 ++++--- unshackle/core/drm/widevine.py | 7 ++++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index 8a8fc11..72752fc 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -248,7 +248,9 @@ class dl: ) @click.option("--downloads", type=int, default=1, help="Amount of tracks to download concurrently.") @click.option("--no-cache", "no_cache", is_flag=True, default=False, help="Bypass title cache for this download.") - @click.option("--reset-cache", "reset_cache", is_flag=True, default=False, help="Clear title cache before fetching.") + @click.option( + "--reset-cache", "reset_cache", is_flag=True, default=False, help="Clear title cache before fetching." + ) @click.pass_context def cli(ctx: click.Context, **kwargs: Any) -> dl: return dl(ctx, **kwargs) @@ -294,6 +296,9 @@ class dl: if getattr(config, "downloader_map", None): config.downloader = config.downloader_map.get(self.service, config.downloader) + if getattr(config, "decryption_map", None): + config.decryption = config.decryption_map.get(self.service, config.decryption) + with console.status("Loading DRM CDM...", spinner="dots"): try: self.cdm = self.get_cdm(self.service, self.profile) @@ -531,7 +536,7 @@ class dl: else: console.print(Padding("Search -> [bright_black]No match found[/]", (0, 5))) - if self.tmdb_id and getattr(self, 'search_source', None) != 'simkl': + if self.tmdb_id and getattr(self, "search_source", None) != "simkl": kind = "tv" if isinstance(title, Episode) else "movie" tags.external_ids(self.tmdb_id, kind) if self.tmdb_year: @@ -1001,12 +1006,7 @@ class dl: # Handle DRM decryption BEFORE repacking (must decrypt first!) service_name = service.__class__.__name__.upper() decryption_method = config.decryption_map.get(service_name, config.decryption) - use_mp4decrypt = decryption_method.lower() == "mp4decrypt" - - if use_mp4decrypt: - decrypt_tool = "mp4decrypt" - else: - decrypt_tool = "Shaka Packager" + decrypt_tool = "mp4decrypt" if decryption_method.lower() == "mp4decrypt" else "Shaka Packager" drm_tracks = [track for track in title.tracks if track.drm] if drm_tracks: @@ -1015,7 +1015,7 @@ class dl: for track in drm_tracks: drm = track.get_drm_for_cdm(self.cdm) if drm and hasattr(drm, "decrypt"): - drm.decrypt(track.path, use_mp4decrypt=use_mp4decrypt) + drm.decrypt(track.path) has_decrypted = True events.emit(events.Types.TRACK_REPACKED, track=track) else: diff --git a/unshackle/core/drm/playready.py b/unshackle/core/drm/playready.py index 38d77c4..dee91ca 100644 --- a/unshackle/core/drm/playready.py +++ b/unshackle/core/drm/playready.py @@ -253,12 +253,11 @@ class PlayReady: if not self.content_keys: raise PlayReady.Exceptions.EmptyLicense("No Content Keys were within the License") - def decrypt(self, path: Path, use_mp4decrypt: bool = False) -> None: + def decrypt(self, path: Path) -> None: """ Decrypt a Track with PlayReady DRM. Args: path: Path to the encrypted file to decrypt - use_mp4decrypt: If True, use mp4decrypt instead of Shaka Packager Raises: EnvironmentError if the required decryption executable could not be found. ValueError if the track has not yet been downloaded. @@ -270,7 +269,9 @@ class PlayReady: if not path or not path.exists(): raise ValueError("Tried to decrypt a file that does not exist.") - if use_mp4decrypt: + decrypter = str(getattr(config, "decryption", "")).lower() + + if decrypter == "mp4decrypt": return self._decrypt_with_mp4decrypt(path) else: return self._decrypt_with_shaka_packager(path) diff --git a/unshackle/core/drm/widevine.py b/unshackle/core/drm/widevine.py index b08076a..825b1b1 100644 --- a/unshackle/core/drm/widevine.py +++ b/unshackle/core/drm/widevine.py @@ -227,12 +227,11 @@ class Widevine: finally: cdm.close(session_id) - def decrypt(self, path: Path, use_mp4decrypt: bool = False) -> None: + def decrypt(self, path: Path) -> None: """ Decrypt a Track with Widevine DRM. Args: path: Path to the encrypted file to decrypt - use_mp4decrypt: If True, use mp4decrypt instead of Shaka Packager Raises: EnvironmentError if the required decryption executable could not be found. ValueError if the track has not yet been downloaded. @@ -244,7 +243,9 @@ class Widevine: if not path or not path.exists(): raise ValueError("Tried to decrypt a file that does not exist.") - if use_mp4decrypt: + decrypter = str(getattr(config, "decryption", "")).lower() + + if decrypter == "mp4decrypt": return self._decrypt_with_mp4decrypt(path) else: return self._decrypt_with_shaka_packager(path)