better info json handler

This commit is contained in:
Jesse Bannon 2024-03-30 16:47:56 -07:00
parent 412532a3b8
commit d2b1079cf4
3 changed files with 31 additions and 8 deletions

View file

@ -17,7 +17,7 @@ from ytdl_sub.entries.entry import Entry
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
class SourcePluginExtension(Plugin[TOptionsValidator], ABC): class SourcePluginExtension(Plugin[TOptionsValidator], Generic[TOptionsValidator], ABC):
""" """
Plugins that get added automatically by using a downloader. Downloader options Plugins that get added automatically by using a downloader. Downloader options
are the plugin options. are the plugin options.

View file

@ -42,7 +42,30 @@ class URLDownloadState:
self.entries_downloaded = 0 self.entries_downloaded = 0
class UrlDownloaderThumbnailPlugin(SourcePluginExtension): class UrlDownloaderBasePluginExtension(SourcePluginExtension[MultiUrlValidator]):
def _match_entry_to_url_validator(self, entry: Entry) -> UrlValidator:
"""
Handle matching a URL to its original validator. This is for .info.json updates
when older entries have missing variables
"""
input_url_idx = entry.get(v.ytdl_sub_input_url_index, int)
entry_input_url = entry.get(v.ytdl_sub_input_url, str)
if 0 <= input_url_idx < len(self.plugin_options.urls.list):
validator = self.plugin_options.urls.list[input_url_idx]
if self.overrides.apply_formatter(validator.url) == entry_input_url:
return validator
# Match the first validator based on the URL, if one exists
for validator in self.plugin_options.urls.list:
if self.overrides.apply_formatter(validator.url) == entry_input_url:
return validator
# Return the first validator if none exist
return self.plugin_options.urls.list[0]
class UrlDownloaderThumbnailPlugin(UrlDownloaderBasePluginExtension):
def __init__( def __init__(
self, self,
options: MultiUrlValidator, options: MultiUrlValidator,
@ -134,15 +157,13 @@ class UrlDownloaderThumbnailPlugin(SourcePluginExtension):
try_convert_download_thumbnail(entry=entry) try_convert_download_thumbnail(entry=entry)
self._download_url_thumbnails( self._download_url_thumbnails(
collection_url=self.plugin_options.urls.list[ collection_url=self._match_entry_to_url_validator(entry=entry),
entry.get(v.ytdl_sub_input_url_index, int)
],
entry=entry, entry=entry,
) )
return entry return entry
class UrlDownloaderCollectionVariablePlugin(SourcePluginExtension): class UrlDownloaderCollectionVariablePlugin(UrlDownloaderBasePluginExtension):
def __init__( def __init__(
self, self,
options: MultiUrlValidator, options: MultiUrlValidator,
@ -160,7 +181,7 @@ class UrlDownloaderCollectionVariablePlugin(SourcePluginExtension):
""" """
Add collection variables to the entry Add collection variables to the entry
""" """
collection_url = self.plugin_options.urls.list[entry.get(v.ytdl_sub_input_url_index, int)] collection_url = self._match_entry_to_url_validator(entry=entry)
entry.add(collection_url.variables.dict_with_format_strings) entry.add(collection_url.variables.dict_with_format_strings)
return entry return entry

View file

@ -756,7 +756,9 @@ class YtdlSubVariableDefinitions(ABC):
:description: :description:
The index of the input URL as defined in the subscription, top-most being the 0th index. The index of the input URL as defined in the subscription, top-most being the 0th index.
""" """
return IntegerVariable(variable_name="ytdl_sub_input_url_index", definition="{ %int(0) }") # init as -1 so if prior downloaded entries are known when they do not have this value
# in their .info.json
return IntegerVariable(variable_name="ytdl_sub_input_url_index", definition="{ %int(-1) }")
@cached_property @cached_property
def ytdl_sub_input_url_count(self: "VariableDefinitions") -> IntegerVariable: def ytdl_sub_input_url_count(self: "VariableDefinitions") -> IntegerVariable: