X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Fpostprocessor%2Fexecafterdownload.py;h=08419a3d4b6990c83cd0d0c844a61c2abe5c2f0e;hb=cc7fec5818254f4679896823c7de9d17f50201ca;hp=431ab7f08d50a50aea0ee30be0f0b97727081cd0;hpb=a7cacbca2b140f668785d57264914de5585ed699;p=youtube-dl diff --git a/youtube_dl/postprocessor/execafterdownload.py b/youtube_dl/postprocessor/execafterdownload.py index 431ab7f08..08419a3d4 100644 --- a/youtube_dl/postprocessor/execafterdownload.py +++ b/youtube_dl/postprocessor/execafterdownload.py @@ -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 ..utils import ( + shlex_quote, + 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=None, verboseOutput=None, exec_cmd=None): + self.verboseOutput = verboseOutput + 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('{}', 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(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