[iprima] Improve extraction (closes #25138)
[youtube-dl] / youtube_dl / postprocessor / execafterdownload.py
index 431ab7f08d50a50aea0ee30be0f0b97727081cd0..64dabe790bcb74e88b4959f3c20e0eb23a0bcf8c 100644 (file)
@@ -1,36 +1,31 @@
-# ExecAfterDownload written by AaronM / mcd1992.
-# If there are any issues with this postprocessor please contact me via github or admin@fgthou.se
+from __future__ import unicode_literals
 
-import os, re, shlex
-from ..utils import PostProcessingError
+import subprocess
 
-class ExecAfterDownload( object ):
-    _downloader = None
+from .common import PostProcessor
+from ..compat import compat_shlex_quote
+from ..utils import (
+    encodeArgument,
+    PostProcessingError,
+)
 
-    def __init__( self, downloader = None, commandString = None ):
-        self._downloader = downloader
-        self.commandString = commandString
 
-    def set_downloader( self, downloader ):
-        """Sets the downloader for this PP."""
-        self._downloader = downloader
+class ExecAfterDownloadPP(PostProcessor):
+    def __init__(self, downloader, exec_cmd):
+        super(ExecAfterDownloadPP, self).__init__(downloader)
+        self.exec_cmd = exec_cmd
 
-    def run( self, information ):
-        self.targetFile = information["filepath"]
-        self.finalCommand = None;
+    def run(self, information):
+        cmd = self.exec_cmd
+        if '{}' not in cmd:
+            cmd += ' {}'
 
-        if( re.search( '{}', self.commandString ) ): # Find and replace all occurrences of {} with the file name.
-            self.finalCommand = re.sub( "{}", '\'' + self.targetFile + '\'', self.commandString )
-        else:
-            self.finalCommand = self.commandString + ' \'' + self.targetFile + '\''
+        cmd = cmd.replace('{}', compat_shlex_quote(information['filepath']))
 
-        if( self.finalCommand ):
-            print( "[exec] Executing command: " + self.finalCommand )
-            os.system( self.finalCommand )
-        else:
-            raise PostProcessingExecError( "Invalid syntax for --exec post processor" )
+        self._downloader.to_screen('[exec] Executing command: %s' % cmd)
+        retCode = subprocess.call(encodeArgument(cmd), shell=True)
+        if retCode != 0:
+            raise PostProcessingError(
+                'Command returned error code %d' % retCode)
 
-        return None, information  # by default, keep file and do nothing
-
-class PostProcessingExecError( PostProcessingError ):
-    pass
+        return [], information