[REFACTOR] simplify enhanced download archive (#551)

This commit is contained in:
Jesse Bannon 2023-03-15 23:57:12 -07:00 committed by GitHub
parent 14ddd750d9
commit ac2bb4c018
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -349,6 +349,16 @@ class EnhancedDownloadArchive:
6. ( Delete the working directory ) 6. ( Delete the working directory )
""" """
@classmethod
def _maybe_load_download_mappings(cls, mapping_file_path: str) -> DownloadMappings:
"""
Tries to load download mappings if a file exists. Otherwise returns empty mappings.
"""
# If a mapping file exists in the output directory, load it up.
if os.path.isfile(mapping_file_path):
return DownloadMappings.from_file(json_file_path=mapping_file_path)
return DownloadMappings()
def __init__( def __init__(
self, self,
subscription_name: str, subscription_name: str,
@ -360,7 +370,9 @@ class EnhancedDownloadArchive:
self._file_handler = FileHandler( self._file_handler = FileHandler(
working_directory=working_directory, output_directory=output_directory, dry_run=dry_run working_directory=working_directory, output_directory=output_directory, dry_run=dry_run
) )
self._download_mapping = DownloadMappings() self._download_mapping = self._maybe_load_download_mappings(
mapping_file_path=self._mapping_output_file_path
)
self._logger = Logger.get(name=subscription_name) self._logger = Logger.get(name=subscription_name)
@ -396,7 +408,9 @@ class EnhancedDownloadArchive:
output_directory=self.output_directory, output_directory=self.output_directory,
dry_run=dry_run, dry_run=dry_run,
) )
self._download_mapping = DownloadMappings() self._download_mapping = self._maybe_load_download_mappings(
mapping_file_path=self._mapping_output_file_path
)
return self return self
@property @property
@ -488,22 +502,7 @@ class EnhancedDownloadArchive:
raise ValueError("Tried to use download mapping before it was loaded") raise ValueError("Tried to use download mapping before it was loaded")
return self._download_mapping return self._download_mapping
def _load(self) -> "EnhancedDownloadArchive": def prepare_download_archive(self) -> "EnhancedDownloadArchive":
"""
Tries to load download mappings if they are present in the output directory.
Returns
-------
self
"""
# If a mapping file exists in the output directory, load it up.
if os.path.isfile(self._mapping_output_file_path):
self._download_mapping = DownloadMappings.from_file(
json_file_path=self._mapping_output_file_path
)
return self
def _copy_mapping_to_working_directory(self) -> "EnhancedDownloadArchive":
""" """
If the mapping is not empty, create a download archive from it and save it into the If the mapping is not empty, create a download archive from it and save it into the
working directory. This will tell YTDL to not redownload already downloaded entries. working directory. This will tell YTDL to not redownload already downloaded entries.
@ -522,19 +521,6 @@ class EnhancedDownloadArchive:
return self return self
def prepare_download_archive(self) -> "EnhancedDownloadArchive":
"""
Helper function to load mappings and create a YTDL download archive file in the
working directory if mappings exist.
Returns
-------
self
"""
self._load()
self._copy_mapping_to_working_directory()
return self
def remove_stale_files(self, date_range: DateRange) -> "EnhancedDownloadArchive": def remove_stale_files(self, date_range: DateRange) -> "EnhancedDownloadArchive":
""" """
Checks all entries within the mappings. If any entries' upload dates are not within the Checks all entries within the mappings. If any entries' upload dates are not within the