regex fixed but failing bandcamp

This commit is contained in:
Jesse Bannon 2023-12-12 11:02:42 -08:00
parent aca7a461b0
commit 2f1bd49922
3 changed files with 22 additions and 68 deletions

View file

@ -15,6 +15,7 @@ from ytdl_sub.entries.script.variable_scripts import VARIABLE_SCRIPTS
from ytdl_sub.entries.variables.kwargs import YTDL_SUB_REGEX_SOURCE_VARS from ytdl_sub.entries.variables.kwargs import YTDL_SUB_REGEX_SOURCE_VARS
from ytdl_sub.script.parser import parse from ytdl_sub.script.parser import parse
from ytdl_sub.script.script import Script from ytdl_sub.script.script import Script
from ytdl_sub.script.utils.exceptions import ScriptVariableNotResolved
from ytdl_sub.utils.exceptions import RegexNoMatchException from ytdl_sub.utils.exceptions import RegexNoMatchException
from ytdl_sub.utils.exceptions import StringFormattingVariableNotFoundException from ytdl_sub.utils.exceptions import StringFormattingVariableNotFoundException
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
@ -290,34 +291,16 @@ class RegexPlugin(Plugin[RegexOptions]):
# Otherwise, error # Otherwise, error
raise RegexNoMatchException(f"Regex failed to match '{variable_name}' from '{entry.title}'") raise RegexNoMatchException(f"Regex failed to match '{variable_name}' from '{entry.title}'")
def _can_process_at_metadata_stage(self, entry: Entry, variable_name: str) -> bool: @classmethod
# If the variable is an override... def _can_process_at_metadata_stage(cls, entry: Entry, variable_name: str) -> bool:
if variable_name in self.overrides.dict: # Try to see if it can resolve
# Try to see if it can resolve try:
try: _ = entry.script.get(variable_name)
self.overrides.apply_formatter( return True
formatter=self.overrides.dict[variable_name], # If it can not from missing variables (from post-metadata stage), return False
entry=entry, except ScriptVariableNotResolved:
)
# If it can not from missing variables (from post-metadata stage), return False
except StringFormattingVariableNotFoundException:
return False
# If it is a source variable and not present, return false
elif variable_name not in entry.to_dict():
return False return False
return True
def _get_regex_input_string(self, entry: Entry, variable_name: str) -> str:
# Apply override formatter if it's an override
if variable_name in self.overrides.dict:
return self.overrides.apply_formatter(
formatter=self.overrides.dict[variable_name],
entry=entry,
)
# Otherwise pluck from the entry's source variable
return entry.to_dict()[variable_name]
def _modify_entry_metadata(self, entry: Entry, is_metadata_stage: bool) -> Optional[Entry]: def _modify_entry_metadata(self, entry: Entry, is_metadata_stage: bool) -> Optional[Entry]:
""" """
Parameters Parameters
@ -356,10 +339,7 @@ class RegexPlugin(Plugin[RegexOptions]):
self._add_processed_regex_variable_name(entry, variable_name) self._add_processed_regex_variable_name(entry, variable_name)
regex_input_str = self._get_regex_input_string( regex_input_str = str(entry.script.get(variable_name))
entry=entry,
variable_name=variable_name,
)
if ( if (
regex_options.exclude is not None regex_options.exclude is not None
@ -377,50 +357,22 @@ class RegexPlugin(Plugin[RegexOptions]):
if not regex_options.has_defaults: if not regex_options.has_defaults:
return self._try_skip_entry(entry=entry, variable_name=variable_name) return self._try_skip_entry(entry=entry, variable_name=variable_name)
# otherwise, use defaults (apply them using the original entry source dict)
source_variables_and_overrides_dict = dict(
entry.to_dict(), **self.overrides.dict_with_format_strings
)
# add both the default... # add both the default...
entry.add_variables( entry.add({
variables_to_add={ regex_options.capture_group_names[i]: self.overrides.apply_formatter(
regex_options.capture_group_names[i]: default.apply_formatter( formatter=default,
variable_dict=source_variables_and_overrides_dict entry=entry
) )
for i, default in enumerate(regex_options.capture_group_defaults) for i, default in enumerate(regex_options.capture_group_defaults)
}, }
)
# and sanitized default
entry.add_variables(
variables_to_add={
f"{regex_options.capture_group_names[i]}_sanitized": sanitize_filename(
default.apply_formatter(
variable_dict=source_variables_and_overrides_dict
)
)
for i, default in enumerate(regex_options.capture_group_defaults)
},
) )
# There is a capture, add the source variables to the entry as # There is a capture, add the source variables to the entry as
# {source_var}_capture_1, {source_var}_capture_2, ... # {source_var}_capture_1, {source_var}_capture_2, ...
else: else:
# Add the value... entry.add({
entry.add_variables( regex_options.capture_group_names[i]: capture
variables_to_add={ for i, capture in enumerate(maybe_capture)
regex_options.capture_group_names[i]: capture })
for i, capture in enumerate(maybe_capture)
},
)
# And the sanitized value
entry.add_variables(
variables_to_add={
f"{regex_options.capture_group_names[i]}_sanitized": sanitize_filename(
capture
)
for i, capture in enumerate(maybe_capture)
},
)
return entry return entry

View file

@ -71,6 +71,8 @@ UNREACHABLE = _UnreachableSyntaxException(
class RuntimeException(ValueError, ABC): class RuntimeException(ValueError, ABC):
"""Exception thrown at runtime during resolution""" """Exception thrown at runtime during resolution"""
class ScriptVariableNotResolved(RuntimeException):
"""Tried to get a variable's resolved value from a script, but has not resolved yet"""
class FunctionRuntimeException(RuntimeException): class FunctionRuntimeException(RuntimeException):
"""Exception thrown when a ytdl-sub function has an error occur at runtime""" """Exception thrown when a ytdl-sub function has an error occur at runtime"""

View file

@ -42,7 +42,7 @@ class LogEntriesDownloadedListener(threading.Thread):
# swallow the error since this is only printing logs # swallow the error since this is only printing logs
return None return None
return file_json.get_str("title") return file_json.get("title")
@classmethod @classmethod
def _is_info_json(cls, path: Path) -> bool: def _is_info_json(cls, path: Path) -> bool: