]> jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/movefilesafterdownload.py
#29 New option `-P`/`--paths` to give different paths for different types of files
[yt-dlp.git] / youtube_dlc / postprocessor / movefilesafterdownload.py
1 from __future__ import unicode_literals
2 import os
3 import shutil
4
5 from .common import PostProcessor
6 from ..utils import (
7 encodeFilename,
8 make_dir,
9 PostProcessingError,
10 )
11 from ..compat import compat_str
12
13
14 class MoveFilesAfterDownloadPP(PostProcessor):
15
16 def __init__(self, downloader, files_to_move):
17 PostProcessor.__init__(self, downloader)
18 self.files_to_move = files_to_move
19
20 @classmethod
21 def pp_key(cls):
22 return 'MoveFiles'
23
24 def run(self, info):
25 if info.get('__dl_filename') is None:
26 return [], info
27 self.files_to_move.setdefault(info['__dl_filename'], '')
28 outdir = os.path.dirname(os.path.abspath(encodeFilename(info['__final_filename'])))
29
30 for oldfile, newfile in self.files_to_move.items():
31 if not os.path.exists(encodeFilename(oldfile)):
32 self.report_warning('File "%s" cannot be found' % oldfile)
33 continue
34 if not newfile:
35 newfile = compat_str(os.path.join(outdir, os.path.basename(encodeFilename(oldfile))))
36 if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
37 continue
38 if os.path.exists(encodeFilename(newfile)):
39 if self.get_param('overwrites', True):
40 self.report_warning('Replacing existing file "%s"' % newfile)
41 os.path.remove(encodeFilename(newfile))
42 else:
43 self.report_warning(
44 'Cannot move file "%s" out of temporary directory since "%s" already exists. '
45 % (oldfile, newfile))
46 continue
47 make_dir(newfile, PostProcessingError)
48 self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile))
49 shutil.move(oldfile, newfile) # os.rename cannot move between volumes
50
51 info['filepath'] = info['__final_filename']
52 return [], info