ytdl-sub/src/ytdl_sub/config/plugin/plugin.py
Jesse Bannon e92b1cd12a
[BACKEND][HUGE] Function Support in variable syntax (#838)
A complete gutting of the internals of ytdl-sub to support functions in our variable syntax, in addition to being able to access a yt-dlp entry's .info.json fields using functions. Functionally, ytdl-sub should still look and behave the same from a user-perspective.

With so many lines of code changed (+8927, -2708), no doubt there will be new issues. Please make a GH issue or reach out on Discord if your config/subscriptions break in any way/shape/form.

Details on how to use function support will come soon in the form of proper documentation in our readthedocs.
2023-12-18 16:08:15 -08:00

130 lines
3.6 KiB
Python

from abc import ABC
from abc import abstractmethod
from typing import Dict
from typing import Generic
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from ytdl_sub.config.overrides import Overrides
from ytdl_sub.config.validators.options import TOptionsValidator
from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.file_handler import FileMetadata
from ytdl_sub.ytdl_additions.enhanced_download_archive import DownloadArchiver
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
# pylint: disable=no-self-use,unused-argument
class BasePlugin(DownloadArchiver, Generic[TOptionsValidator], ABC):
"""
Shared code amongst all SourcePlugins (downloaders) and Plugins (post-download modification)
"""
plugin_options_type: Type[TOptionsValidator]
def __init__(
self,
options: TOptionsValidator,
overrides: Overrides,
enhanced_download_archive: EnhancedDownloadArchive,
):
DownloadArchiver.__init__(self=self, enhanced_download_archive=enhanced_download_archive)
self.plugin_options = options
self.overrides = overrides
class Plugin(BasePlugin[TOptionsValidator], Generic[TOptionsValidator], ABC):
"""
Class to define the new plugin functionality
"""
def ytdl_options_match_filters(self) -> Tuple[List[str], List[str]]:
"""
Returns
-------
Tuple of match-filters to apply, first one being non-breaking, second breaking
"""
return [], []
def ytdl_options(self) -> Optional[Dict]:
"""
Returns
-------
ytdl options to enable/disable when downloading entries for this specific plugin
"""
def modify_entry_metadata(self, entry: Entry) -> Optional[Entry]:
"""
After entry metadata has been gathered, perform preprocessing on the metadata
Parameters
----------
entry
Entry metadata to modify
Returns
-------
The entry or None, indicating not to download it.
"""
return entry
def modify_entry(self, entry: Entry) -> Optional[Entry]:
"""
After each entry is downloaded, modify the entry in some way before sending it to
post-processing.
Parameters
----------
entry
Entry to modify
Returns
-------
The entry or None, indicating not to move it to the output directory
"""
return entry
def post_process_entry(self, entry: Entry) -> Optional[FileMetadata]:
"""
For each entry downloaded, apply post processing to it.
Parameters
----------
entry
Entry to post process
Returns
-------
Optional file metadata for the entry media file.
"""
return None
def post_process_subscription(self):
"""
After all downloaded files have been post-processed, apply a subscription-wide post process
"""
class SplitPlugin(Plugin[TOptionsValidator], Generic[TOptionsValidator], ABC):
"""
Plugin that splits entries into zero or more entries
"""
@abstractmethod
def split(self, entry: Entry) -> List[Tuple[Entry, FileMetadata]]:
"""
Very specialized function that takes an entry and creates multiple entries from it.
Should mark ``is_split_plugin`` on the plugin class.
Parameters
----------
entry
Entry to create multiple entries from
Returns
-------
List of entries and metadata created from the source entry
"""
return []