aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/postprocessor/execafterdownload.py
blob: 431ab7f08d50a50aea0ee30be0f0b97727081cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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