[FEATURE] Ability to change download archive name (#643)

This commit is contained in:
Jesse Bannon 2023-06-30 11:51:52 -07:00 committed by GitHub
parent 7a43b0a90b
commit b8d1f1edbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 45 deletions

View file

@ -14,6 +14,7 @@ disable = [
"R0801", # similar lines
"R0913", # Too many arguments
"R0901", # too-many-ancestors
"R0902", # too-many-instance-attributes
"W0511", # TODO
]

View file

@ -95,7 +95,6 @@ class PersistLogsValidator(StrictDictValidator):
return self._keep_successful_logs.value
# pylint: disable=too-many-instance-attributes
class ConfigOptions(StrictDictValidator):
_required_keys = {"working_directory"}
_optional_keys = {

View file

@ -14,3 +14,7 @@ else:
DEFAULT_FFPROBE_PATH = "/usr/bin/ffprobe"
MAX_FILE_NAME_BYTES = os.pathconf("/", "PC_NAME_MAX")
# Historically was hardcoded to this value. Use this as the default
# if download_archive_path is not specified
DEFAULT_DOWNLOAD_ARCHIVE_NAME = ".ytdl-sub-{subscription_name}-download-archive.json"

View file

@ -6,6 +6,7 @@ from typing import Optional
from yt_dlp.utils import sanitize_filename
from ytdl_sub.config.defaults import DEFAULT_DOWNLOAD_ARCHIVE_NAME
from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.exceptions import ValidationException
from ytdl_sub.validators.file_path_validators import OverridesStringFormatterFilePathValidator
@ -235,6 +236,7 @@ class OutputOptions(StrictDictValidator):
# optional
thumbnail_name: "{title_sanitized}.{thumbnail_ext}"
info_json_name: "{title_sanitized}.{info_json_ext}"
download_archive_name: ".ytdl-sub-{subscription_name}-download-archive.txt"
maintain_download_archive: True
keep_files_before: now
keep_files_after: 19000101
@ -244,7 +246,7 @@ class OutputOptions(StrictDictValidator):
_optional_keys = {
"thumbnail_name",
"info_json_name",
"subtitles_name",
"download_archive_name",
"maintain_download_archive",
"keep_files_before",
"keep_files_after",
@ -282,6 +284,12 @@ class OutputOptions(StrictDictValidator):
key="info_json_name", validator=StringFormatterFileNameValidator
)
self._download_archive_name = self._validate_key_if_present(
key="download_archive_name",
validator=OverridesStringFormatterValidator,
default=DEFAULT_DOWNLOAD_ARCHIVE_NAME,
)
self._maintain_download_archive = self._validate_key_if_present(
key="maintain_download_archive", validator=BoolValidator, default=False
)
@ -333,6 +341,14 @@ class OutputOptions(StrictDictValidator):
"""
return self._info_json_name
@property
def download_archive_name(self) -> Optional[OverridesStringFormatterValidator]:
"""
Optional. The file name to store a subscriptions download archive placed relative to
the output directory. Defaults to ``.ytdl-sub-{subscription_name}-download-archive.txt``
"""
return self._download_archive_name
@property
def maintain_download_archive(self) -> bool:
"""

View file

@ -42,8 +42,6 @@ from ytdl_sub.utils.thumbnail import convert_download_thumbnail
from ytdl_sub.utils.thumbnail import download_and_convert_url_thumbnail
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
# pylint: disable=too-many-instance-attributes
download_logger = Logger.get(name="downloader")

View file

@ -46,7 +46,7 @@ class BaseSubscription(ABC):
self._preset_options = preset_options
self._enhanced_download_archive = EnhancedDownloadArchive(
subscription_name=name,
file_name=self.overrides.apply_formatter(self.output_options.download_archive_name),
working_directory=self.working_directory,
output_directory=self.output_directory,
)

View file

@ -144,8 +144,7 @@ class SubscriptionDownload(BaseSubscription, ABC):
self._enhanced_download_archive.remove_stale_files(date_range=date_range_to_keep)
self._enhanced_download_archive.save_download_mappings()
FileHandler.delete(self._enhanced_download_archive.archive_working_file_path)
FileHandler.delete(self._enhanced_download_archive.mapping_working_file_path)
FileHandler.delete(self._enhanced_download_archive.working_file_path)
@contextlib.contextmanager
def _remove_empty_directories_in_output_directory(self):

View file

@ -87,9 +87,7 @@ class SubscriptionYTDLOptions:
ytdl_options = {}
if self._preset.output_options.maintain_download_archive:
ytdl_options["download_archive"] = str(
Path(self._working_directory) / self._enhanced_download_archive.archive_file_name
)
ytdl_options["download_archive"] = self._enhanced_download_archive.working_file_path
return ytdl_options

View file

@ -18,7 +18,6 @@ from ytdl_sub.entries.variables.kwargs import SPLIT_BY_CHAPTERS_PARENT_ENTRY
from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
from ytdl_sub.utils.file_handler import FileMetadata
from ytdl_sub.utils.logger import Logger
@dataclass
@ -370,21 +369,19 @@ class EnhancedDownloadArchive:
def __init__(
self,
subscription_name: str,
file_name: str,
working_directory: str,
output_directory: str,
dry_run: bool = False,
):
self.subscription_name = subscription_name
self._file_name = file_name
self._file_handler = FileHandler(
working_directory=working_directory, output_directory=output_directory, dry_run=dry_run
)
self._download_mapping = self._maybe_load_download_mappings(
mapping_file_path=self._mapping_output_file_path
mapping_file_path=self.output_file_path
)
self._logger = Logger.get(name=subscription_name)
self.num_entries_added: int = 0
self.num_entries_modified: int = 0
self.num_entries_removed: int = 0
@ -418,7 +415,7 @@ class EnhancedDownloadArchive:
dry_run=dry_run,
)
self._download_mapping = self._maybe_load_download_mappings(
mapping_file_path=self._mapping_output_file_path
mapping_file_path=self.output_file_path
)
return self
@ -431,16 +428,6 @@ class EnhancedDownloadArchive:
"""
return self._file_handler.dry_run
@property
def archive_file_name(self) -> str:
"""
Returns
-------
The download archive's file name (no path)
Used to recreate yt-dlp's download archive in the working directory
"""
return f".ytdl-sub-{self.subscription_name}-download-archive.txt"
@property
def working_directory(self) -> str:
"""
@ -460,40 +447,31 @@ class EnhancedDownloadArchive:
return self._file_handler.output_directory
@property
def _mapping_file_name(self) -> str:
def file_name(self) -> str:
"""
Returns
-------
The download mapping's file name (no path)
"""
return f".ytdl-sub-{self.subscription_name}-download-archive.json"
return self._file_name
@property
def _mapping_output_file_path(self) -> str:
def output_file_path(self) -> str:
"""
Returns
-------
The download mapping's file path in the output directory.
"""
return str(Path(self.output_directory) / self._mapping_file_name)
return str(Path(self.output_directory) / self.file_name)
@property
def mapping_working_file_path(self) -> str:
def working_file_path(self) -> str:
"""
Returns
-------
The download mapping's file path in the working directory.
"""
return str(Path(self.working_directory) / self._mapping_file_name)
@property
def archive_working_file_path(self) -> str:
"""
Returns
-------
The download archive's file path in the working directory.
"""
return str(Path(self.working_directory) / self.archive_file_name)
return str(Path(self.working_directory) / self.file_name)
@property
def mapping(self) -> DownloadMappings:
@ -526,7 +504,7 @@ class EnhancedDownloadArchive:
return self
# Otherwise, create a ytdl download archive file in the working directory.
self.mapping.to_download_archive().to_file(self.archive_working_file_path)
self.mapping.to_download_archive().to_file(self.working_file_path)
return self
@ -566,8 +544,8 @@ class EnhancedDownloadArchive:
self
"""
if not self.get_file_handler_transaction_log().is_empty:
self._download_mapping.to_file(output_json_file=self.mapping_working_file_path)
self.save_file_to_output_directory(file_name=self._mapping_file_name)
self._download_mapping.to_file(output_json_file=self.working_file_path)
self.save_file_to_output_directory(file_name=self.file_name)
return self
def delete_file_from_output_directory(self, file_name: str):