[ie/mlbtv] Fix extractor (#10515)
[yt-dlp.git] / yt_dlp / postprocessor / movefilesafterdownload.py
blob35e87051b4254039fede78a2b7757aa9603aadb9
1 import os
3 from .common import PostProcessor
4 from ..compat import shutil
5 from ..utils import (
6 PostProcessingError,
7 decodeFilename,
8 encodeFilename,
9 make_dir,
13 class MoveFilesAfterDownloadPP(PostProcessor):
15 def __init__(self, downloader=None, downloaded=True):
16 PostProcessor.__init__(self, downloader)
17 self._downloaded = downloaded
19 @classmethod
20 def pp_key(cls):
21 return 'MoveFiles'
23 def run(self, info):
24 dl_path, dl_name = os.path.split(encodeFilename(info['filepath']))
25 finaldir = info.get('__finaldir', dl_path)
26 finalpath = os.path.join(finaldir, dl_name)
27 if self._downloaded:
28 info['__files_to_move'][info['filepath']] = decodeFilename(finalpath)
30 make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
31 for oldfile, newfile in info['__files_to_move'].items():
32 if not newfile:
33 newfile = make_newfilename(oldfile)
34 if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
35 continue
36 if not os.path.exists(encodeFilename(oldfile)):
37 self.report_warning(f'File "{oldfile}" cannot be found')
38 continue
39 if os.path.exists(encodeFilename(newfile)):
40 if self.get_param('overwrites', True):
41 self.report_warning(f'Replacing existing file "{newfile}"')
42 os.remove(encodeFilename(newfile))
43 else:
44 self.report_warning(
45 f'Cannot move file "{oldfile}" out of temporary directory since "{newfile}" already exists. ')
46 continue
47 make_dir(newfile, PostProcessingError)
48 self.to_screen(f'Moving file "{oldfile}" to "{newfile}"')
49 shutil.move(oldfile, newfile) # os.rename cannot move between volumes
51 info['filepath'] = finalpath
52 return [], info