mirror of https://github.com/yt-dlp/yt-dlp
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
4 years ago
|
from __future__ import unicode_literals
|
||
|
import os
|
||
|
import shutil
|
||
|
|
||
|
from .common import PostProcessor
|
||
|
from ..utils import (
|
||
4 years ago
|
decodeFilename,
|
||
4 years ago
|
encodeFilename,
|
||
|
make_dir,
|
||
|
PostProcessingError,
|
||
|
)
|
||
|
|
||
|
|
||
|
class MoveFilesAfterDownloadPP(PostProcessor):
|
||
|
|
||
|
def __init__(self, downloader, files_to_move):
|
||
|
PostProcessor.__init__(self, downloader)
|
||
|
self.files_to_move = files_to_move
|
||
|
|
||
|
@classmethod
|
||
|
def pp_key(cls):
|
||
|
return 'MoveFiles'
|
||
|
|
||
|
def run(self, info):
|
||
4 years ago
|
dl_path, dl_name = os.path.split(encodeFilename(info['filepath']))
|
||
|
finaldir = info.get('__finaldir', dl_path)
|
||
|
finalpath = os.path.join(finaldir, dl_name)
|
||
4 years ago
|
self.files_to_move.update(info['__files_to_move'])
|
||
4 years ago
|
self.files_to_move[info['filepath']] = decodeFilename(finalpath)
|
||
4 years ago
|
|
||
4 years ago
|
make_newfilename = lambda old: decodeFilename(os.path.join(finaldir, os.path.basename(encodeFilename(old))))
|
||
4 years ago
|
for oldfile, newfile in self.files_to_move.items():
|
||
|
if not newfile:
|
||
4 years ago
|
newfile = make_newfilename(oldfile)
|
||
4 years ago
|
if os.path.abspath(encodeFilename(oldfile)) == os.path.abspath(encodeFilename(newfile)):
|
||
|
continue
|
||
4 years ago
|
if not os.path.exists(encodeFilename(oldfile)):
|
||
|
self.report_warning('File "%s" cannot be found' % oldfile)
|
||
|
continue
|
||
4 years ago
|
if os.path.exists(encodeFilename(newfile)):
|
||
|
if self.get_param('overwrites', True):
|
||
|
self.report_warning('Replacing existing file "%s"' % newfile)
|
||
4 years ago
|
os.remove(encodeFilename(newfile))
|
||
4 years ago
|
else:
|
||
|
self.report_warning(
|
||
|
'Cannot move file "%s" out of temporary directory since "%s" already exists. '
|
||
|
% (oldfile, newfile))
|
||
|
continue
|
||
|
make_dir(newfile, PostProcessingError)
|
||
|
self.to_screen('Moving file "%s" to "%s"' % (oldfile, newfile))
|
||
|
shutil.move(oldfile, newfile) # os.rename cannot move between volumes
|
||
|
|
||
4 years ago
|
info['filepath'] = finalpath
|
||
4 years ago
|
return [], info
|