]> jfr.im git - yt-dlp.git/blob - youtube_dlc/postprocessor/movefilesafterdownload.py
7f34ac5c5ec8c1d5d1baf6000b85166986ca8205
[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 dl_path, dl_name = os.path.split(encodeFilename(info['filepath']))
26 finaldir = info.get('__finaldir', dl_path)
27 finalpath = os.path.join(finaldir, dl_name)
28 self.files_to_move.update(info['__files_to_move'])
29 self.files_to_move[info['filepath']] = finalpath
30
31 for oldfile, newfile in self.files_to_move.items():
32 if not newfile:
33 newfile = os.path.join(finaldir, os.path.basename(encodeFilename(oldfile)))
34 oldfile, newfile = compat_str(oldfile), compat_str(newfile)
35 if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
36 continue
37 if not os.path.exists(encodeFilename(oldfile)):
38 self.report_warning('File "%s" cannot be found' % oldfile)
39 continue
40 if os.path.exists(encodeFilename(newfile)):
41 if self.get_param('overwrites', True):
42 self.report_warning('Replacing existing file "%s"' % newfile)
43 os.remove(encodeFilename(newfile))
44 else:
45 self.report_warning(
46 'Cannot move file "%s" out of temporary directory since "%s" already exists. '
47 % (oldfile, newfile))
48 continue
49 make_dir(newfile, PostProcessingError)
50 self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile))
51 shutil.move(oldfile, newfile) # os.rename cannot move between volumes
52
53 info['filepath'] = compat_str(finalpath)
54 return [], info