more refactoring

This commit is contained in:
Jesse Bannon 2023-03-15 22:52:58 -07:00
parent f46a1f3e98
commit b97fb3b7f9
3 changed files with 41 additions and 27 deletions

View file

@ -11,7 +11,6 @@ from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.downloaders.ytdl_options_builder import YTDLOptionsBuilder from ytdl_sub.downloaders.ytdl_options_builder import YTDLOptionsBuilder
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import Plugin
from ytdl_sub.plugins.plugin import PluginOptions
from ytdl_sub.ytdl_additions.enhanced_download_archive import DownloadArchiver from ytdl_sub.ytdl_additions.enhanced_download_archive import DownloadArchiver
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
@ -19,20 +18,20 @@ BaseDownloaderValidator = OptionsValidator
BaseDownloaderOptionsT = TypeVar("BaseDownloaderOptionsT", bound=BaseDownloaderValidator) BaseDownloaderOptionsT = TypeVar("BaseDownloaderOptionsT", bound=BaseDownloaderValidator)
class BaseDownloaderPluginOptions(PluginOptions): class BaseDownloaderPlugin(Plugin[BaseDownloaderOptionsT], ABC):
_optional_keys = {"no-op"} """
Plugins that get added automatically by using a downloader. Downloader options
are the plugin options.
class BaseDownloaderPlugin(Plugin[BaseDownloaderOptionsT], Generic[BaseDownloaderOptionsT], ABC): """
def __init__( def __init__(
self, self,
downloader_options: BaseDownloaderOptionsT,
overrides: Overrides, overrides: Overrides,
enhanced_download_archive: EnhancedDownloadArchive, enhanced_download_archive: EnhancedDownloadArchive,
): ):
super().__init__( super().__init__(
# Downloader plugins do not have exposed YAML options, so keep it blank. # Downloader plugins use download options as their plugin options
# Use init instead. plugin_options=downloader_options,
plugin_options=BaseDownloaderPluginOptions(name=self.__class__.__name__, value={}),
overrides=overrides, overrides=overrides,
enhanced_download_archive=enhanced_download_archive, enhanced_download_archive=enhanced_download_archive,
) )
@ -63,9 +62,14 @@ class BaseDownloader(DownloadArchiver, Generic[BaseDownloaderOptionsT], ABC):
def download(self, entry: Entry) -> Entry: def download(self, entry: Entry) -> Entry:
"""The function to perform the download of all media entries""" """The function to perform the download of all media entries"""
# pylint: disable=no-self-use # pylint: disable=unused-argument
def added_plugins(self) -> List[BaseDownloaderPlugin]: @classmethod
def added_plugins(
cls,
downloader_options: BaseDownloaderOptionsT,
enhanced_download_archive: EnhancedDownloadArchive,
overrides: Overrides,
) -> List[BaseDownloaderPlugin]:
"""Add these plugins from the Downloader to the subscription""" """Add these plugins from the Downloader to the subscription"""
return [] return []
# pylint: enable=unused-argument
# pylint: enable=no-self-use

View file

@ -87,18 +87,19 @@ class URLDownloadState:
class UrlDownloaderThumbnailPlugin(BaseDownloaderPlugin): class UrlDownloaderThumbnailPlugin(BaseDownloaderPlugin):
def __init__( def __init__(
self, self,
downloader_options: DownloaderValidator,
overrides: Overrides, overrides: Overrides,
enhanced_download_archive: EnhancedDownloadArchive, enhanced_download_archive: EnhancedDownloadArchive,
collection_urls: List[UrlValidator],
): ):
super().__init__( super().__init__(
downloader_options=downloader_options,
overrides=overrides, overrides=overrides,
enhanced_download_archive=enhanced_download_archive, enhanced_download_archive=enhanced_download_archive,
) )
self._thumbnails_downloaded: Set[str] = set() self._thumbnails_downloaded: Set[str] = set()
self._collection_url_mapping: Dict[str, UrlValidator] = { self._collection_url_mapping: Dict[str, UrlValidator] = {
self.overrides.apply_formatter(collection_url.url): collection_url self.overrides.apply_formatter(collection_url.url): collection_url
for collection_url in collection_urls for collection_url in downloader_options.collection_validator.urls.list
} }
def _download_parent_thumbnails( def _download_parent_thumbnails(
@ -185,18 +186,19 @@ class UrlDownloaderThumbnailPlugin(BaseDownloaderPlugin):
class UrlDownloaderCollectionVariablePlugin(BaseDownloaderPlugin): class UrlDownloaderCollectionVariablePlugin(BaseDownloaderPlugin):
def __init__( def __init__(
self, self,
downloader_options: DownloaderValidator,
overrides: Overrides, overrides: Overrides,
enhanced_download_archive: EnhancedDownloadArchive, enhanced_download_archive: EnhancedDownloadArchive,
collection_urls: List[UrlValidator],
): ):
super().__init__( super().__init__(
downloader_options=downloader_options,
overrides=overrides, overrides=overrides,
enhanced_download_archive=enhanced_download_archive, enhanced_download_archive=enhanced_download_archive,
) )
self._thumbnails_downloaded: Set[str] = set() self._thumbnails_downloaded: Set[str] = set()
self._collection_url_mapping: Dict[str, UrlValidator] = { self._collection_url_mapping: Dict[str, UrlValidator] = {
self.overrides.apply_formatter(collection_url.url): collection_url self.overrides.apply_formatter(collection_url.url): collection_url
for collection_url in collection_urls for collection_url in downloader_options.collection_validator.urls.list
} }
def modify_entry_metadata(self, entry: Entry) -> Optional[Entry]: def modify_entry_metadata(self, entry: Entry) -> Optional[Entry]:
@ -218,7 +220,13 @@ class BaseUrlDownloader(BaseDownloader[BaseDownloaderOptionsT], ABC):
and should translate that to list of Entry objects. and should translate that to list of Entry objects.
""" """
def added_plugins(self) -> List[Plugin]: @classmethod
def added_plugins(
cls,
downloader_options: BaseDownloaderOptionsT,
enhanced_download_archive: EnhancedDownloadArchive,
overrides: Overrides,
) -> List[Plugin]:
""" """
Adds Adds
1. URL thumbnail download plugin 1. URL thumbnail download plugin
@ -226,14 +234,14 @@ class BaseUrlDownloader(BaseDownloader[BaseDownloaderOptionsT], ABC):
""" """
return [ return [
UrlDownloaderThumbnailPlugin( UrlDownloaderThumbnailPlugin(
overrides=self.overrides, downloader_options=downloader_options,
enhanced_download_archive=self._enhanced_download_archive, overrides=overrides,
collection_urls=self.collection.urls.list, enhanced_download_archive=enhanced_download_archive,
), ),
UrlDownloaderCollectionVariablePlugin( UrlDownloaderCollectionVariablePlugin(
overrides=self.overrides, downloader_options=downloader_options,
enhanced_download_archive=self._enhanced_download_archive, overrides=overrides,
collection_urls=self.collection.urls.list, enhanced_download_archive=enhanced_download_archive,
), ),
] ]

View file

@ -275,7 +275,11 @@ class SubscriptionDownload(BaseSubscription, ABC):
directory. directory.
""" """
self._enhanced_download_archive.reinitialize(dry_run=dry_run) self._enhanced_download_archive.reinitialize(dry_run=dry_run)
plugins = self._initialize_plugins() plugins = self._initialize_plugins() + self.downloader_class.added_plugins(
downloader_options=self.downloader_options,
enhanced_download_archive=self._enhanced_download_archive,
overrides=self.overrides,
)
subscription_ytdl_options = SubscriptionYTDLOptions( subscription_ytdl_options = SubscriptionYTDLOptions(
preset=self._preset_options, preset=self._preset_options,
@ -292,8 +296,6 @@ class SubscriptionDownload(BaseSubscription, ABC):
metadata_ytdl_options=subscription_ytdl_options.metadata_builder(), metadata_ytdl_options=subscription_ytdl_options.metadata_builder(),
overrides=self.overrides, overrides=self.overrides,
) )
# This could be cleaned up....
plugins.extend(downloader.added_plugins())
with self._subscription_download_context_managers(): with self._subscription_download_context_managers():
for entry in downloader.download_metadata(): for entry in downloader.download_metadata():