From d9b33b205299d2ba7e04ea64fbf1b54ddfa4c25f Mon Sep 17 00:00:00 2001 From: jbannon Date: Sun, 17 Jul 2022 06:29:14 +0000 Subject: [PATCH] sanitized vars created --- src/ytdl_sub/plugins/regex.py | 32 ++++++++++++++++++- tests/e2e/plugins/test_regex.py | 4 +++ .../plugins/test_regex.txt | 6 ++++ .../youtube/test_playlist.txt | 2 +- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/ytdl_sub/plugins/regex.py b/src/ytdl_sub/plugins/regex.py index dcb2b5b2..d584e058 100644 --- a/src/ytdl_sub/plugins/regex.py +++ b/src/ytdl_sub/plugins/regex.py @@ -2,6 +2,8 @@ from typing import Dict from typing import List from typing import Optional +from yt_dlp.utils import sanitize_filename + from ytdl_sub.entries.entry import Entry from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import PluginOptions @@ -119,6 +121,7 @@ class RegexOptions(PluginOptions): # Perform this regex match on it to act as a filter. # This will only download videos with "Official Video" in it. match: '\[Official Video\]' + # For each entry's `description` value... description: # Match with capture groups and defaults. @@ -126,7 +129,7 @@ class RegexOptions(PluginOptions): match: "([0-9]{4})-([0-9]{2})-([0-9]{2})" # Each capture group creates these new source variables, respectively, as well - # a sanitized version, i.e. `sanitized_captured_upload_year` + # a sanitized version, i.e. `captured_upload_year_sanitized` capture_group_names: - "captured_upload_year" - "captured_upload_month" @@ -209,6 +212,10 @@ class RegexOptions(PluginOptions): added_source_vars: List[str] = [] for regex_options in self.source_variable_capture_dict.values(): added_source_vars.extend(regex_options.capture_group_names) + added_source_vars.extend( + f"{capture_group_name}_sanitized" + for capture_group_name in regex_options.capture_group_names + ) return added_source_vars @@ -260,6 +267,8 @@ class RegexPlugin(Plugin[RegexOptions]): source_variables_and_overrides_dict = dict( entry_variable_dict, **self.overrides.dict_with_format_strings ) + + # add both the default... entry.add_variables( variables_to_add={ regex_options.capture_group_names[i]: default.apply_formatter( @@ -268,14 +277,35 @@ class RegexPlugin(Plugin[RegexOptions]): 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 # {source_var}_capture_1, {source_var}_capture_2, ... else: + # Add the value... entry.add_variables( variables_to_add={ 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 diff --git a/tests/e2e/plugins/test_regex.py b/tests/e2e/plugins/test_regex.py index 404ced41..1b12e8ac 100644 --- a/tests/e2e/plugins/test_regex.py +++ b/tests/e2e/plugins/test_regex.py @@ -56,16 +56,20 @@ def regex_subscription_dict(output_directory): "nfo_tags": { "tags": { "title_cap_1": "{title_type}", + "title_cap_1_sanitized": "{title_type_sanitized}", "title_cap_2": "{title_date}", "desc_cap": "{description_website}", "upload_date_both_caps": "{upload_captured_year} and {upload_captured_month}", "artist_cap_always_default": "{always_default}", + "artist_cap_always_default_sanitized": "{always_default_sanitized}", "override_with_capture_variable": "{contains_regex_default}", + "override_with_capture_variable_sanitized": "{contains_regex_sanitized_default}", } }, "overrides": { "in_regex_default": "in regex default", "contains_regex_default": "contains {title_type}", + "contains_regex_sanitized_default": "contains {title_type_sanitized}", }, } diff --git a/tests/e2e/resources/transaction_log_summaries/plugins/test_regex.txt b/tests/e2e/resources/transaction_log_summaries/plugins/test_regex.txt index 7465ae89..97fd64dc 100644 --- a/tests/e2e/resources/transaction_log_summaries/plugins/test_regex.txt +++ b/tests/e2e/resources/transaction_log_summaries/plugins/test_regex.txt @@ -9,10 +9,13 @@ Project Zombie - Jesse's Minecraft Server [Trailer - Feb.1].nfo album: Music Videos artist: Project Zombie artist_cap_always_default: Always default + artist_cap_always_default_sanitized: Always default desc_cap: www.jesseminecraft.webs override_with_capture_variable: contains Trailer + override_with_capture_variable_sanitized: contains Trailer title: Jesse's Minecraft Server [Trailer - Feb.1] title_cap_1: Trailer + title_cap_1_sanitized: Trailer title_cap_2: Feb.1 upload_date_both_caps: First and Second containing in regex default year: 2011 @@ -24,10 +27,13 @@ Project Zombie - Jesse's Minecraft Server [Trailer - Feb.27].nfo album: Music Videos artist: Project Zombie artist_cap_always_default: Always default + artist_cap_always_default_sanitized: Always default desc_cap: jesseminecraft.webs override_with_capture_variable: contains Trailer + override_with_capture_variable_sanitized: contains Trailer title: Jesse's Minecraft Server [Trailer - Feb.27] title_cap_1: Trailer + title_cap_1_sanitized: Trailer title_cap_2: Feb.27 upload_date_both_caps: 2011 and 02 year: 2011 \ No newline at end of file diff --git a/tests/e2e/resources/transaction_log_summaries/youtube/test_playlist.txt b/tests/e2e/resources/transaction_log_summaries/youtube/test_playlist.txt index 3d31bea7..603789f6 100644 --- a/tests/e2e/resources/transaction_log_summaries/youtube/test_playlist.txt +++ b/tests/e2e/resources/transaction_log_summaries/youtube/test_playlist.txt @@ -1,6 +1,6 @@ Files created in '{output_directory}' ---------------------------------------- -.ytdl-sub-jmc-download-archive.json +.ytdl-sub-music_video_playlist_test-download-archive.json JMC - Jesse's Minecraft Server [Trailer - Feb.1]-thumb.jpg JMC - Jesse's Minecraft Server [Trailer - Feb.1].mp4 JMC - Jesse's Minecraft Server [Trailer - Feb.1].nfo