]> jfr.im git - yt-dlp.git/blame - yt_dlp/postprocessor/movefilesafterdownload.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / yt_dlp / postprocessor / movefilesafterdownload.py
CommitLineData
0202b52a 1from __future__ import unicode_literals
2import os
3import shutil
4
5from .common import PostProcessor
6from ..utils import (
8a0b9322 7 decodeFilename,
0202b52a 8 encodeFilename,
9 make_dir,
10 PostProcessingError,
11)
0202b52a 12
13
14class 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):
c571435f 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)
de6000d9 28 self.files_to_move.update(info['__files_to_move'])
8a0b9322 29 self.files_to_move[info['filepath']] = decodeFilename(finalpath)
0202b52a 30
8a0b9322 31 make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
0202b52a 32 for oldfile, newfile in self.files_to_move.items():
0202b52a 33 if not newfile:
8a0b9322 34 newfile = make_newfilename(oldfile)
0202b52a 35 if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
36 continue
98820640 37 if not os.path.exists(encodeFilename(oldfile)):
38 self.report_warning('File "%s" cannot be found' % oldfile)
39 continue
0202b52a 40 if os.path.exists(encodeFilename(newfile)):
41 if self.get_param('overwrites', True):
42 self.report_warning('Replacing existing file "%s"' % newfile)
de6000d9 43 os.remove(encodeFilename(newfile))
0202b52a 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
8a0b9322 53 info['filepath'] = finalpath
0202b52a 54 return [], info