From a7cacbca2b140f668785d57264914de5585ed699 Mon Sep 17 00:00:00 2001 From: mcd1992 Date: Fri, 22 Aug 2014 16:40:43 -0500 Subject: Implemented --exec option. --- README.md | 2 ++ youtube_dl/.__init__.py.swp | Bin 0 -> 16384 bytes youtube_dl/__init__.py | 15 ++++++++++- youtube_dl/postprocessor/__init__.py | 2 ++ youtube_dl/postprocessor/execafterdownload.py | 36 ++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 youtube_dl/.__init__.py.swp create mode 100644 youtube_dl/postprocessor/execafterdownload.py diff --git a/README.md b/README.md index ba9fec8a7..30bdac189 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,8 @@ which means you can modify it, redistribute it or use it however you like. postprocessors (default) --prefer-ffmpeg Prefer ffmpeg over avconv for running the postprocessors + --exec Execute a command after the file is finished downloading, similar to find's -exec format + Example: --exec 'adb push {} /sdcard/Music/ && rm {}' # CONFIGURATION diff --git a/youtube_dl/.__init__.py.swp b/youtube_dl/.__init__.py.swp new file mode 100644 index 000000000..0586277fd Binary files /dev/null and b/youtube_dl/.__init__.py.swp differ diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index a96bf9b5c..189a8ff35 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -71,8 +71,12 @@ __authors__ = ( 'Sebastian Haas', 'Alexander Kirk', 'Erik Johnson', +<<<<<<< HEAD 'Keith Beckman', 'Ole Ernst', +======= + 'Aaron McDaniel (mcd1992)', +>>>>>>> Implemented --exec option. ) __license__ = 'Public Domain' @@ -119,6 +123,7 @@ from .postprocessor import ( FFmpegExtractAudioPP, FFmpegEmbedSubtitlePP, XAttrMetadataPP, + ExecAfterDownload, ) @@ -550,7 +555,8 @@ def parseOpts(overrideArguments=None): help='Prefer avconv over ffmpeg for running the postprocessors (default)') postproc.add_option('--prefer-ffmpeg', action='store_true', dest='prefer_ffmpeg', help='Prefer ffmpeg over avconv for running the postprocessors') - + postproc.add_option('--exec', metavar='', action='store', dest='execstring', + help='Execute a command on the file after downloading, similar to find\'s -exec syntax. Must be enclosed in quotes. Example: --exec \'adb push {} /sdcard/Music/ && rm {}\'' ) parser.add_option_group(general) parser.add_option_group(selection) @@ -831,6 +837,7 @@ def _real_main(argv=None): 'default_search': opts.default_search, 'youtube_include_dash_manifest': opts.youtube_include_dash_manifest, 'encoding': opts.encoding, + 'execstring': opts.execstring, } with YoutubeDL(ydl_opts) as ydl: @@ -854,6 +861,12 @@ def _real_main(argv=None): ydl.add_post_processor(FFmpegAudioFixPP()) ydl.add_post_processor(AtomicParsleyPP()) + + # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way. + # So if the user is able to remove the file before your postprocessor runs it might cause a few problems. + if opts.execstring: + ydl.add_post_processor(ExecAfterDownload(commandString=opts.execstring)) + # Update version if opts.update_self: update_self(ydl.to_screen, opts.verbose) diff --git a/youtube_dl/postprocessor/__init__.py b/youtube_dl/postprocessor/__init__.py index 08e6ddd00..59ab49e6d 100644 --- a/youtube_dl/postprocessor/__init__.py +++ b/youtube_dl/postprocessor/__init__.py @@ -9,6 +9,7 @@ from .ffmpeg import ( FFmpegEmbedSubtitlePP, ) from .xattrpp import XAttrMetadataPP +from .execafterdownload import ExecAfterDownload __all__ = [ 'AtomicParsleyPP', @@ -19,4 +20,5 @@ __all__ = [ 'FFmpegExtractAudioPP', 'FFmpegEmbedSubtitlePP', 'XAttrMetadataPP', + 'ExecAfterDownload', ] diff --git a/youtube_dl/postprocessor/execafterdownload.py b/youtube_dl/postprocessor/execafterdownload.py new file mode 100644 index 000000000..431ab7f08 --- /dev/null +++ b/youtube_dl/postprocessor/execafterdownload.py @@ -0,0 +1,36 @@ +# ExecAfterDownload written by AaronM / mcd1992. +# If there are any issues with this postprocessor please contact me via github or admin@fgthou.se + +import os, re, shlex +from ..utils import PostProcessingError + +class ExecAfterDownload( object ): + _downloader = None + + 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 + + def run( self, information ): + self.targetFile = information["filepath"] + self.finalCommand = None; + + 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 + '\'' + + if( self.finalCommand ): + print( "[exec] Executing command: " + self.finalCommand ) + os.system( self.finalCommand ) + else: + raise PostProcessingExecError( "Invalid syntax for --exec post processor" ) + + return None, information # by default, keep file and do nothing + +class PostProcessingExecError( PostProcessingError ): + pass -- cgit v1.2.3 From a2360a4c80700824f4160505fcdb1ca14261c031 Mon Sep 17 00:00:00 2001 From: mcd1992 Date: Sat, 23 Aug 2014 14:30:13 -0500 Subject: Moved from os.system to subprocess.call --- .gitignore | 1 + youtube_dl/.__init__.py.swp | Bin 16384 -> 0 bytes youtube_dl/__init__.py | 4 +- youtube_dl/postprocessor/__init__.py | 4 +- youtube_dl/postprocessor/execafterdownload.py | 51 ++++++++++++++------------ 5 files changed, 32 insertions(+), 28 deletions(-) delete mode 100644 youtube_dl/.__init__.py.swp diff --git a/.gitignore b/.gitignore index 37b2fa8d3..b8128fab1 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,6 @@ updates_key.pem *.m4a *.m4v *.part +*.swp test/testdata .tox diff --git a/youtube_dl/.__init__.py.swp b/youtube_dl/.__init__.py.swp deleted file mode 100644 index 0586277fd..000000000 Binary files a/youtube_dl/.__init__.py.swp and /dev/null differ diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 189a8ff35..4eae88d6c 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -123,7 +123,7 @@ from .postprocessor import ( FFmpegExtractAudioPP, FFmpegEmbedSubtitlePP, XAttrMetadataPP, - ExecAfterDownload, + ExecAfterDownloadPP, ) @@ -865,7 +865,7 @@ def _real_main(argv=None): # Please keep ExecAfterDownload towards the bottom as it allows the user to modify the final file in any way. # So if the user is able to remove the file before your postprocessor runs it might cause a few problems. if opts.execstring: - ydl.add_post_processor(ExecAfterDownload(commandString=opts.execstring)) + ydl.add_post_processor(ExecAfterDownloadPP(verboseOutput=opts.verbose,commandString=opts.execstring)) # Update version if opts.update_self: diff --git a/youtube_dl/postprocessor/__init__.py b/youtube_dl/postprocessor/__init__.py index 59ab49e6d..15aa0daa9 100644 --- a/youtube_dl/postprocessor/__init__.py +++ b/youtube_dl/postprocessor/__init__.py @@ -9,7 +9,7 @@ from .ffmpeg import ( FFmpegEmbedSubtitlePP, ) from .xattrpp import XAttrMetadataPP -from .execafterdownload import ExecAfterDownload +from .execafterdownload import ExecAfterDownloadPP __all__ = [ 'AtomicParsleyPP', @@ -20,5 +20,5 @@ __all__ = [ 'FFmpegExtractAudioPP', 'FFmpegEmbedSubtitlePP', 'XAttrMetadataPP', - 'ExecAfterDownload', + 'ExecAfterDownloadPP', ] diff --git a/youtube_dl/postprocessor/execafterdownload.py b/youtube_dl/postprocessor/execafterdownload.py index 431ab7f08..e6f3cdfd2 100644 --- a/youtube_dl/postprocessor/execafterdownload.py +++ b/youtube_dl/postprocessor/execafterdownload.py @@ -1,36 +1,39 @@ -# ExecAfterDownload written by AaronM / mcd1992. -# If there are any issues with this postprocessor please contact me via github or admin@fgthou.se - -import os, re, shlex +from __future__ import unicode_literals +from .common import PostProcessor from ..utils import PostProcessingError +import subprocess +import shlex -class ExecAfterDownload( object ): - _downloader = None - def __init__( self, downloader = None, commandString = None ): - self._downloader = downloader +class ExecAfterDownloadPP(PostProcessor): + def __init__(self, downloader=None, verboseOutput=None, commandString=None): + self.verboseOutput = verboseOutput self.commandString = commandString - def set_downloader( self, downloader ): - """Sets the downloader for this PP.""" - self._downloader = downloader + def run(self, information): + self.targetFile = information['filepath'].replace('\'', '\'\\\'\'') # Replace single quotes with '\'' + self.commandList = shlex.split(self.commandString) + self.commandString = '' - def run( self, information ): - self.targetFile = information["filepath"] - self.finalCommand = None; + # Replace all instances of '{}' with the file name and convert argument list to single string. + for index, arg in enumerate(self.commandList): + if(arg == '{}'): + self.commandString += '\'' + self.targetFile + '\' ' + else: + self.commandString += arg + ' ' - 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 + '\'' + if self.targetFile not in self.commandString: # Assume user wants the file appended to the end of the command if no {}'s were given. + self.commandString += '\'' + self.targetFile + '\'' - if( self.finalCommand ): - print( "[exec] Executing command: " + self.finalCommand ) - os.system( self.finalCommand ) - else: - raise PostProcessingExecError( "Invalid syntax for --exec post processor" ) + print("[exec] Executing command: " + self.commandString) + self.retCode = subprocess.call(self.commandString, shell=True) + if(self.retCode < 0): + print("[exec] WARNING: Command exited with a negative return code, the process was killed externally. Your command may not of completed succesfully!") + elif(self.verboseOutput): + print("[exec] Command exited with return code: " + str(self.retCode)) return None, information # by default, keep file and do nothing -class PostProcessingExecError( PostProcessingError ): + +class PostProcessingExecError(PostProcessingError): pass -- cgit v1.2.3 From 7833d941bb736a8dbd237f1c7bc268d671cabb68 Mon Sep 17 00:00:00 2001 From: mcd1992 Date: Sun, 24 Aug 2014 15:04:50 -0500 Subject: Rebased with upstream/master --- README.md | 2 -- youtube_dl/__init__.py | 3 --- 2 files changed, 5 deletions(-) diff --git a/README.md b/README.md index 30bdac189..ba9fec8a7 100644 --- a/README.md +++ b/README.md @@ -288,8 +288,6 @@ which means you can modify it, redistribute it or use it however you like. postprocessors (default) --prefer-ffmpeg Prefer ffmpeg over avconv for running the postprocessors - --exec Execute a command after the file is finished downloading, similar to find's -exec format - Example: --exec 'adb push {} /sdcard/Music/ && rm {}' # CONFIGURATION diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 4eae88d6c..7cf5de43f 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -71,12 +71,9 @@ __authors__ = ( 'Sebastian Haas', 'Alexander Kirk', 'Erik Johnson', -<<<<<<< HEAD 'Keith Beckman', 'Ole Ernst', -======= 'Aaron McDaniel (mcd1992)', ->>>>>>> Implemented --exec option. ) __license__ = 'Public Domain' -- cgit v1.2.3