-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Filter Keywords prebuilt preset (#1086)
Adds ``Filter Keywords``, a preset that can include or exclude media with any of the listed keywords. Both keywords and title/description are lower-cased before filtering. Supports the following override variables: * ``title_include_keywords`` * ``title_exclude_keywords`` * ``description_include_keywords`` * ``description_exclude_keywords`` For best usage, use the `~` tilda subscription mode to set a subscription's list override variables. Tilda mode allows override variables to be set directly underneath it. ``` Plex TV Show by Date | Filter Keywords: = Documentaries: "~NOVA PBS": url: "https://www.youtube.com/@novapbs" title_exclude_keywords: - "preview" - "trailer" "~To Catch a Smuggler": url: "https://www.youtube.com/@NatGeo" title_include_keywords: - "To Catch a Smuggler" ```
- Loading branch information
Showing
13 changed files
with
487 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
presets: | ||
|
||
############################################################################# | ||
# Include Keywords | ||
# Include or exclude media with any of the listed keywords in their titles | ||
# Keywords will check a lower-cased title or description | ||
Filter Keywords: | ||
overrides: | ||
# default filter lists to be empty | ||
title_include_keywords: "{ [] }" | ||
title_exclude_keywords: "{ [] }" | ||
description_include_keywords: "{ [] }" | ||
description_exclude_keywords: "{ [] }" | ||
|
||
"%ensure_string": >- | ||
{ | ||
%assert_then( | ||
%is_string($0), | ||
%lower($0), | ||
"filter keywords must be strings" | ||
) | ||
} | ||
"%ensure_lower_array": >- | ||
{ | ||
%assert_then( | ||
%is_array($0), | ||
%array_apply( | ||
$0, | ||
%ensure_string | ||
), | ||
%concat($1," must be an array") | ||
) | ||
} | ||
# $0 - var to evaluate | ||
# $1 - keyword list | ||
# $2 - variable name for error messages | ||
# $3 - default return if keyword list is empty | ||
"%contains_keywords": >- | ||
{ | ||
%if( | ||
%bool( $1 ), | ||
%contains_any( %lower($0), %ensure_lower_array($1, $2) ), | ||
$3 | ||
) | ||
} | ||
filter_exclude: | ||
- "{ %not( %contains_keywords(title, title_include_keywords, 'title_include_keywords', true) ) }" | ||
- "{ %not( %contains_keywords(description, description_include_keywords, 'description_include_keywords', true) ) }" | ||
- "{ %contains_keywords(title, title_exclude_keywords, 'title_exclude_keywords', false) }" | ||
- "{ %contains_keywords(description, description_exclude_keywords, 'description_exclude_keywords',false) }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
tests/integration/prebuilt_presets/test_filter_keywords.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import re | ||
|
||
import pytest | ||
from expected_transaction_log import assert_transaction_log_matches | ||
|
||
from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError | ||
from ytdl_sub.subscriptions.subscription import Subscription | ||
from ytdl_sub.utils.exceptions import ValidationException | ||
|
||
|
||
@pytest.fixture | ||
def filter_subscription_dict(output_directory): | ||
return { | ||
"preset": [ | ||
"Plex TV Show by Date", | ||
"Filter Keywords", | ||
], | ||
"overrides": {"url": "https://your.name.here", "tv_show_directory": output_directory}, | ||
} | ||
|
||
|
||
class TestFilterKeywords: | ||
|
||
def test_no_overrides( | ||
self, | ||
config, | ||
filter_subscription_dict, | ||
output_directory, | ||
subscription_name, | ||
mock_download_collection_entries, | ||
): | ||
subscription = Subscription.from_dict( | ||
config=config, | ||
preset_name=subscription_name, | ||
preset_dict=filter_subscription_dict, | ||
) | ||
|
||
with mock_download_collection_entries( | ||
is_youtube_channel=False, num_urls=1, is_dry_run=True | ||
): | ||
transaction_log = subscription.download(dry_run=True) | ||
|
||
assert_transaction_log_matches( | ||
output_directory=output_directory, | ||
transaction_log=transaction_log, | ||
transaction_log_summary_file_name=f"integration/prebuilt_presets/filter_keywords_empty.txt", | ||
) | ||
|
||
@pytest.mark.parametrize("filter_mode", ["include", "exclude"]) | ||
def test_title( | ||
self, | ||
config, | ||
filter_subscription_dict, | ||
output_directory, | ||
subscription_name, | ||
mock_download_collection_entries, | ||
filter_mode: str, | ||
): | ||
filter_subscription_dict["overrides"][f"title_{filter_mode}_keywords"] = [ | ||
"not included", | ||
"MOCK ENTRY 20-3", | ||
] | ||
subscription = Subscription.from_dict( | ||
config=config, | ||
preset_name=subscription_name, | ||
preset_dict=filter_subscription_dict, | ||
) | ||
|
||
with mock_download_collection_entries( | ||
is_youtube_channel=False, num_urls=1, is_dry_run=True | ||
): | ||
transaction_log = subscription.download(dry_run=True) | ||
|
||
assert_transaction_log_matches( | ||
output_directory=output_directory, | ||
transaction_log=transaction_log, | ||
transaction_log_summary_file_name=f"integration/prebuilt_presets/title_filter_keywords_{filter_mode}.txt", | ||
) | ||
|
||
@pytest.mark.parametrize("filter_mode", ["include", "exclude"]) | ||
def test_description( | ||
self, | ||
config, | ||
filter_subscription_dict, | ||
output_directory, | ||
subscription_name, | ||
mock_download_collection_entries, | ||
filter_mode: str, | ||
): | ||
filter_subscription_dict["overrides"][f"description_{filter_mode}_keywords"] = [ | ||
"no filter here", | ||
"description", | ||
] | ||
subscription = Subscription.from_dict( | ||
config=config, | ||
preset_name=subscription_name, | ||
preset_dict=filter_subscription_dict, | ||
) | ||
|
||
with mock_download_collection_entries( | ||
is_youtube_channel=False, num_urls=1, is_dry_run=True | ||
): | ||
transaction_log = subscription.download(dry_run=True) | ||
|
||
assert_transaction_log_matches( | ||
output_directory=output_directory, | ||
transaction_log=transaction_log, | ||
transaction_log_summary_file_name=f"integration/prebuilt_presets/description_filter_keywords_{filter_mode}.txt", | ||
) | ||
|
||
@pytest.mark.parametrize( | ||
"keyword_variable", | ||
[ | ||
"title_include_keywords", | ||
"title_exclude_keywords", | ||
"description_include_keywords", | ||
"description_exclude_keywords", | ||
], | ||
) | ||
def test_error_not_list_type( | ||
self, | ||
config, | ||
filter_subscription_dict, | ||
output_directory, | ||
subscription_name, | ||
mock_download_collection_entries, | ||
keyword_variable, | ||
): | ||
filter_subscription_dict["overrides"][keyword_variable] = "not array" | ||
subscription = Subscription.from_dict( | ||
config=config, | ||
preset_name=subscription_name, | ||
preset_dict=filter_subscription_dict, | ||
) | ||
|
||
with ( | ||
mock_download_collection_entries(is_youtube_channel=False, num_urls=1, is_dry_run=True), | ||
pytest.raises(UserThrownRuntimeError, match=f"{keyword_variable} must be an array"), | ||
): | ||
_ = subscription.download(dry_run=True) | ||
|
||
@pytest.mark.parametrize( | ||
"keyword_variable", | ||
[ | ||
"title_include_keywords", | ||
"title_exclude_keywords", | ||
"description_include_keywords", | ||
"description_exclude_keywords", | ||
], | ||
) | ||
def test_error_not_string_keyword( | ||
self, | ||
config, | ||
filter_subscription_dict, | ||
output_directory, | ||
subscription_name, | ||
mock_download_collection_entries, | ||
keyword_variable, | ||
): | ||
filter_subscription_dict["overrides"][keyword_variable] = "{['str', ['nested array not']]}" | ||
subscription = Subscription.from_dict( | ||
config=config, | ||
preset_name=subscription_name, | ||
preset_dict=filter_subscription_dict, | ||
) | ||
|
||
with ( | ||
mock_download_collection_entries(is_youtube_channel=False, num_urls=1, is_dry_run=True), | ||
pytest.raises(UserThrownRuntimeError, match="filter keywords must be strings"), | ||
): | ||
_ = subscription.download(dry_run=True) |
1 change: 1 addition & 0 deletions
1
...action_log_summaries/integration/prebuilt_presets/description_filter_keywords_exclude.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No new, modified, or removed files in '{output_directory}' |
Oops, something went wrong.