]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/postprocessor/sponskrub.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / postprocessor / sponskrub.py
index 932555a0ee8c5bf9334805b69e91c610338f2b59..525b6392a45b1ab262cc9e952b0912aec9c4686c 100644 (file)
@@ -1,19 +1,18 @@
-from __future__ import unicode_literals
 import os
+import shlex
 import subprocess
 
 from .common import PostProcessor
-from ..compat import compat_shlex_split
 from ..utils import (
+    Popen,
+    PostProcessingError,
     check_executable,
     cli_option,
     encodeArgument,
     encodeFilename,
+    prepend_extension,
     shell_quote,
     str_or_none,
-    PostProcessingError,
-    prepend_extension,
-    process_communicate_or_kill,
 )
 
 
@@ -22,16 +21,21 @@ class SponSkrubPP(PostProcessor):
     _temp_ext = 'spons'
     _exe_name = 'sponskrub'
 
-    def __init__(self, downloader, path='', args=None, ignoreerror=False, cut=False, force=False):
+    def __init__(self, downloader, path='', args=None, ignoreerror=False, cut=False, force=False, _from_cli=False):
         PostProcessor.__init__(self, downloader)
         self.force = force
         self.cutout = cut
         self.args = str_or_none(args) or ''  # For backward compatibility
         self.path = self.get_exe(path)
 
+        if not _from_cli:
+            self.deprecation_warning(
+                'yt_dlp.postprocessor.SponSkrubPP support is deprecated and may be removed in a future version. '
+                'Use yt_dlp.postprocessor.SponsorBlock and yt_dlp.postprocessor.ModifyChaptersPP instead')
+
         if not ignoreerror and self.path is None:
             if path:
-                raise PostProcessingError('sponskrub not found in "%s"' % path)
+                raise PostProcessingError(f'sponskrub not found in "{path}"')
             else:
                 raise PostProcessingError('sponskrub not found. Please install or provide the path using --sponskrub-path')
 
@@ -74,23 +78,21 @@ def run(self, information):
         if not self.cutout:
             cmd += ['-chapter']
         cmd += cli_option(self._downloader.params, '-proxy', 'proxy')
-        cmd += compat_shlex_split(self.args)  # For backward compatibility
+        cmd += shlex.split(self.args)  # For backward compatibility
         cmd += self._configuration_args(self._exe_name, use_compat=False)
         cmd += ['--', information['id'], filename, temp_filename]
         cmd = [encodeArgument(i) for i in cmd]
 
-        self.write_debug('sponskrub command line: %s' % shell_quote(cmd))
-        pipe = None if self.get_param('verbose') else subprocess.PIPE
-        p = subprocess.Popen(cmd, stdout=pipe)
-        stdout = process_communicate_or_kill(p)[0]
+        self.write_debug(f'sponskrub command line: {shell_quote(cmd)}')
+        stdout, _, returncode = Popen.run(cmd, text=True, stdout=None if self.get_param('verbose') else subprocess.PIPE)
 
-        if p.returncode == 0:
+        if not returncode:
             os.replace(temp_filename, filename)
             self.to_screen('Sponsor sections have been %s' % ('removed' if self.cutout else 'marked'))
-        elif p.returncode == 3:
+        elif returncode == 3:
             self.to_screen('No segments in the SponsorBlock database')
         else:
-            msg = stdout.decode('utf-8', 'replace').strip() if stdout else ''
-            msg = msg.split('\n')[0 if msg.lower().startswith('unrecognised') else -1]
-            raise PostProcessingError(msg if msg else 'sponskrub failed with error code %s' % p.returncode)
+            raise PostProcessingError(
+                stdout.strip().splitlines()[0 if stdout.strip().lower().startswith('unrecognised') else -1]
+                or f'sponskrub failed with error code {returncode}')
         return [], information