]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/movefilesafterdownload.py
[ffmpeg] Cache version data
[yt-dlp.git] / yt_dlp / 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 decodeFilename,
8 encodeFilename,
9 make_dir,
10 PostProcessingError,
11 )
12
13
14 class MoveFilesAfterDownloadPP(PostProcessor):
15
16 def __init__(self, downloader=None, downloaded=True):
17 PostProcessor.__init__(self, downloader)
18 self._downloaded = downloaded
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 if self._downloaded:
29 info['__files_to_move'][info['filepath']] = decodeFilename(finalpath)
30
31 make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
32 for oldfile, newfile in info['__files_to_move'].items():
33 if not newfile:
34 newfile = make_newfilename(oldfile)
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'] = finalpath
54 return [], info