From f66596d1a763366d01e5b7ad561f5d8b3045a71e Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sun, 17 Jul 2022 22:43:49 -0700 Subject: [PATCH] [BACKEND] Move files from working to output directory instead of copying (#104) * [BACKEND] Move files from working to output directory instead of copying * still copy split video thumbnails --- src/ytdl_sub/subscriptions/subscription.py | 4 ++-- src/ytdl_sub/utils/file_handler.py | 20 +++++++++++++++---- .../enhanced_download_archive.py | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/ytdl_sub/subscriptions/subscription.py b/src/ytdl_sub/subscriptions/subscription.py index eb7476c9..b171ed20 100644 --- a/src/ytdl_sub/subscriptions/subscription.py +++ b/src/ytdl_sub/subscriptions/subscription.py @@ -148,7 +148,7 @@ class Subscription: and self.downloader_class.supports_download_archive ) - def _copy_entry_files_to_output_directory( + def _move_entry_files_to_output_directory( self, entry: Entry, entry_metadata: Optional[FileMetadata] = None ): """ @@ -288,7 +288,7 @@ class Subscription: if optional_plugin_entry_metadata: entry_metadata.extend(optional_plugin_entry_metadata) - self._copy_entry_files_to_output_directory( + self._move_entry_files_to_output_directory( entry=entry, entry_metadata=entry_metadata ) diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index 0f749b4d..3d9f55f1 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -1,6 +1,6 @@ import os +import shutil from pathlib import Path -from shutil import copyfile from typing import Any from typing import Dict from typing import List @@ -186,7 +186,19 @@ class FileHandler: dst_file_path Destination file """ - copyfile(src=src_file_path, dst=dst_file_path) + shutil.copyfile(src=src_file_path, dst=dst_file_path) + + @classmethod + def move(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]): + """ + Parameters + ---------- + src_file_path + Source file + dst_file_path + Destination file + """ + shutil.move(src=src_file_path, dst=dst_file_path) @classmethod def delete(cls, file_path: Union[str, Path]): @@ -199,7 +211,7 @@ class FileHandler: if os.path.isfile(file_path): os.remove(file_path) - def copy_file_to_output_directory( + def move_file_to_output_directory( self, file_name: str, output_file_name: str, file_metadata: Optional[FileMetadata] = None ): """ @@ -223,7 +235,7 @@ class FileHandler: if not self.dry_run: output_file_path = Path(self.output_directory) / output_file_name os.makedirs(os.path.dirname(output_file_path), exist_ok=True) - self.copy( + self.move( src_file_path=Path(self.working_directory) / file_name, dst_file_path=output_file_path, ) diff --git a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py index 43eb4346..4776e734 100644 --- a/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py +++ b/src/ytdl_sub/ytdl_additions/enhanced_download_archive.py @@ -321,7 +321,7 @@ class EnhancedDownloadArchive: a. self._load() Checks the output directory to see if an existing enhanced download archive file exists. If so, load it into the class. Otherwise, initialize an empty instance of one. - b. self._copy_to_working_directory() + b. self._move_to_working_directory() If the download archive was loaded successfully, create a ytdl download archive in the working directory. This will let ytdl know which files are already downloaded. 2. ( Perform the ytdlp download using a download archive with the same name ) @@ -576,7 +576,7 @@ class EnhancedDownloadArchive: if entry: self.mapping.add_entry(entry=entry, entry_file_path=output_file_name) - self._file_handler.copy_file_to_output_directory( + self._file_handler.move_file_to_output_directory( file_name=file_name, file_metadata=file_metadata, output_file_name=output_file_name )