431ab7f08d50a50aea0ee30be0f0b97727081cd0
[youtube-dl] / youtube_dl / postprocessor / execafterdownload.py
1 # ExecAfterDownload written by AaronM / mcd1992.
2 # If there are any issues with this postprocessor please contact me via github or admin@fgthou.se
3
4 import os, re, shlex
5 from ..utils import PostProcessingError
6
7 class ExecAfterDownload( object ):
8     _downloader = None
9
10     def __init__( self, downloader = None, commandString = None ):
11         self._downloader = downloader
12         self.commandString = commandString
13
14     def set_downloader( self, downloader ):
15         """Sets the downloader for this PP."""
16         self._downloader = downloader
17
18     def run( self, information ):
19         self.targetFile = information["filepath"]
20         self.finalCommand = None;
21
22         if( re.search( '{}', self.commandString ) ): # Find and replace all occurrences of {} with the file name.
23             self.finalCommand = re.sub( "{}", '\'' + self.targetFile + '\'', self.commandString )
24         else:
25             self.finalCommand = self.commandString + ' \'' + self.targetFile + '\''
26
27         if( self.finalCommand ):
28             print( "[exec] Executing command: " + self.finalCommand )
29             os.system( self.finalCommand )
30         else:
31             raise PostProcessingExecError( "Invalid syntax for --exec post processor" )
32
33         return None, information  # by default, keep file and do nothing
34
35 class PostProcessingExecError( PostProcessingError ):
36     pass