From fa475857f01bdcc55401fd6e7d33fa3295771e6e Mon Sep 17 00:00:00 2001 From: Balasankar C Date: Sun, 26 Feb 2017 12:48:29 +0530 Subject: [PATCH] Add post processor to move after completion --- youtube_dl/__init__.py | 5 +++++ youtube_dl/options.py | 3 +++ youtube_dl/postprocessor/__init__.py | 2 ++ youtube_dl/postprocessor/move.py | 27 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 youtube_dl/postprocessor/move.py diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 0c401baa6..5c8a99d41 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -290,6 +290,11 @@ def _real_main(argv=None): 'key': 'ExecAfterDownload', 'exec_cmd': opts.exec_cmd, }) + if opts.move: + postprocessors.append({ + 'key': 'Move', + 'destination': opts.move, + }) external_downloader_args = None if opts.external_downloader_args: external_downloader_args = compat_shlex_split(opts.external_downloader_args) diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 8b51d3c6f..0000c62a4 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -845,6 +845,9 @@ def parseOpts(overrideArguments=None): '--convert-subs', '--convert-subtitles', metavar='FORMAT', dest='convertsubtitles', default=None, help='Convert the subtitles to other format (currently supported: srt|ass|vtt)') + postproc.add_option( + '--move', metavar="DESTINATION", dest='move', + help="Specify directory to move completed files to") parser.add_option_group(general) parser.add_option_group(network) diff --git a/youtube_dl/postprocessor/__init__.py b/youtube_dl/postprocessor/__init__.py index 3ea518399..4121be253 100644 --- a/youtube_dl/postprocessor/__init__.py +++ b/youtube_dl/postprocessor/__init__.py @@ -16,6 +16,7 @@ from .ffmpeg import ( from .xattrpp import XAttrMetadataPP from .execafterdownload import ExecAfterDownloadPP from .metadatafromtitle import MetadataFromTitlePP +from .move import MovePP def get_postprocessor(key): @@ -25,6 +26,7 @@ def get_postprocessor(key): __all__ = [ 'EmbedThumbnailPP', 'ExecAfterDownloadPP', + 'MovePP', 'FFmpegEmbedSubtitlePP', 'FFmpegExtractAudioPP', 'FFmpegFixupM3u8PP', diff --git a/youtube_dl/postprocessor/move.py b/youtube_dl/postprocessor/move.py new file mode 100644 index 000000000..994333ce7 --- /dev/null +++ b/youtube_dl/postprocessor/move.py @@ -0,0 +1,27 @@ +from __future__ import unicode_literals + +import subprocess +import os +import shutil + +from .common import PostProcessor +from ..utils import PostProcessingError + + +class MovePP(PostProcessor): + + def __init__(self, downloader, destination): + super(MovePP, self).__init__(downloader) + self.destination = destination + + def run(self, information): + source = os.path.abspath(information['filepath']) + destination = os.path.abspath(self.destination) + if not os.path.exists(source): + raise PostProcessingError('Source file not available') + if not os.path.exists(destination): + raise PostProcessingError('Destination not available') + self._downloader.to_screen( + '[exec] Moving %s to %s' % (source, destination)) + shutil.move(source, destination) + return [], information