refactor(drm): Simplify decrypt method by removing unused parameter and streamline logic

This commit is contained in:
Andy
2025-08-20 05:10:38 +00:00
parent ed0f03eca3
commit 19ff200617
3 changed files with 17 additions and 15 deletions

View File

@@ -248,7 +248,9 @@ class dl:
) )
@click.option("--downloads", type=int, default=1, help="Amount of tracks to download concurrently.") @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("--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 @click.pass_context
def cli(ctx: click.Context, **kwargs: Any) -> dl: def cli(ctx: click.Context, **kwargs: Any) -> dl:
return dl(ctx, **kwargs) return dl(ctx, **kwargs)
@@ -294,6 +296,9 @@ class dl:
if getattr(config, "downloader_map", None): if getattr(config, "downloader_map", None):
config.downloader = config.downloader_map.get(self.service, config.downloader) 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"): with console.status("Loading DRM CDM...", spinner="dots"):
try: try:
self.cdm = self.get_cdm(self.service, self.profile) self.cdm = self.get_cdm(self.service, self.profile)
@@ -531,7 +536,7 @@ class dl:
else: else:
console.print(Padding("Search -> [bright_black]No match found[/]", (0, 5))) 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" kind = "tv" if isinstance(title, Episode) else "movie"
tags.external_ids(self.tmdb_id, kind) tags.external_ids(self.tmdb_id, kind)
if self.tmdb_year: if self.tmdb_year:
@@ -1001,12 +1006,7 @@ class dl:
# Handle DRM decryption BEFORE repacking (must decrypt first!) # Handle DRM decryption BEFORE repacking (must decrypt first!)
service_name = service.__class__.__name__.upper() service_name = service.__class__.__name__.upper()
decryption_method = config.decryption_map.get(service_name, config.decryption) decryption_method = config.decryption_map.get(service_name, config.decryption)
use_mp4decrypt = decryption_method.lower() == "mp4decrypt" decrypt_tool = "mp4decrypt" if decryption_method.lower() == "mp4decrypt" else "Shaka Packager"
if use_mp4decrypt:
decrypt_tool = "mp4decrypt"
else:
decrypt_tool = "Shaka Packager"
drm_tracks = [track for track in title.tracks if track.drm] drm_tracks = [track for track in title.tracks if track.drm]
if drm_tracks: if drm_tracks:
@@ -1015,7 +1015,7 @@ class dl:
for track in drm_tracks: for track in drm_tracks:
drm = track.get_drm_for_cdm(self.cdm) drm = track.get_drm_for_cdm(self.cdm)
if drm and hasattr(drm, "decrypt"): if drm and hasattr(drm, "decrypt"):
drm.decrypt(track.path, use_mp4decrypt=use_mp4decrypt) drm.decrypt(track.path)
has_decrypted = True has_decrypted = True
events.emit(events.Types.TRACK_REPACKED, track=track) events.emit(events.Types.TRACK_REPACKED, track=track)
else: else:

View File

@@ -253,12 +253,11 @@ class PlayReady:
if not self.content_keys: if not self.content_keys:
raise PlayReady.Exceptions.EmptyLicense("No Content Keys were within the License") 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. Decrypt a Track with PlayReady DRM.
Args: Args:
path: Path to the encrypted file to decrypt path: Path to the encrypted file to decrypt
use_mp4decrypt: If True, use mp4decrypt instead of Shaka Packager
Raises: Raises:
EnvironmentError if the required decryption executable could not be found. EnvironmentError if the required decryption executable could not be found.
ValueError if the track has not yet been downloaded. ValueError if the track has not yet been downloaded.
@@ -270,7 +269,9 @@ class PlayReady:
if not path or not path.exists(): if not path or not path.exists():
raise ValueError("Tried to decrypt a file that does not exist.") 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) return self._decrypt_with_mp4decrypt(path)
else: else:
return self._decrypt_with_shaka_packager(path) return self._decrypt_with_shaka_packager(path)

View File

@@ -227,12 +227,11 @@ class Widevine:
finally: finally:
cdm.close(session_id) 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. Decrypt a Track with Widevine DRM.
Args: Args:
path: Path to the encrypted file to decrypt path: Path to the encrypted file to decrypt
use_mp4decrypt: If True, use mp4decrypt instead of Shaka Packager
Raises: Raises:
EnvironmentError if the required decryption executable could not be found. EnvironmentError if the required decryption executable could not be found.
ValueError if the track has not yet been downloaded. ValueError if the track has not yet been downloaded.
@@ -244,7 +243,9 @@ class Widevine:
if not path or not path.exists(): if not path or not path.exists():
raise ValueError("Tried to decrypt a file that does not exist.") 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) return self._decrypt_with_mp4decrypt(path)
else: else:
return self._decrypt_with_shaka_packager(path) return self._decrypt_with_shaka_packager(path)