Skip to content

Commit

Permalink
[FEATURE] Create output_directory_nfo_tags on first entry instead of …
Browse files Browse the repository at this point in the history
…last (#809)
  • Loading branch information
jmbannon authored Nov 6, 2023
1 parent e257210 commit 395691f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
19 changes: 14 additions & 5 deletions src/ytdl_sub/plugins/nfo_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ def _get_xml_element_dict(self, entry: Entry) -> Dict[str, List[XmlElement]]:
nfo_tags: Dict[str, List[XmlElement]] = defaultdict(list)

for key, string_tags in self.plugin_options.tags.string_tags.items():
nfo_tags[key].extend(
tags = [
XmlElement(
text=self.overrides.apply_formatter(formatter=string_tag, entry=entry),
attributes={},
)
for string_tag in string_tags
)
]
# Do not add tags with empty text
nfo_tags[key].extend(tag for tag in tags if tag.text)

for key, attribute_tags in self.plugin_options.tags.attribute_tags.items():
nfo_tags[key].extend(
tags = [
XmlElement(
text=self.overrides.apply_formatter(formatter=attribute_tag.tag, entry=entry),
attributes={
Expand All @@ -117,9 +119,12 @@ def _get_xml_element_dict(self, entry: Entry) -> Dict[str, List[XmlElement]]:
},
)
for attribute_tag in attribute_tags
)
]
# Do not add tags with empty text
nfo_tags[key].extend(tag for tag in tags if tag.text)

return nfo_tags
# Do not add tags with empty lists
return {key: tags for key, tags in nfo_tags.items() if len(tags) > 0}

def _create_nfo(self, entry: Entry, save_to_entry: bool = True) -> None:
# Write the nfo tags to XML with the nfo_root
Expand All @@ -128,6 +133,10 @@ def _create_nfo(self, entry: Entry, save_to_entry: bool = True) -> None:
)
nfo_tags = self._get_xml_element_dict(entry=entry)

# If the nfo tags are empty, then stop continuing
if not nfo_tags:
return

if self.plugin_options.kodi_safe:
nfo_root = to_max_3_byte_utf8_string(nfo_root)
nfo_tags = {
Expand Down
23 changes: 7 additions & 16 deletions src/ytdl_sub/plugins/output_directory_nfo_tags.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.entries.entry import Entry
from ytdl_sub.plugins.nfo_tags import NfoTagsValidator
Expand Down Expand Up @@ -95,23 +93,16 @@ def __init__(
enhanced_download_archive: EnhancedDownloadArchive,
):
super().__init__(options, overrides, enhanced_download_archive)
self._last_entry: Optional[Entry] = None
self._created_output_nfo = False

def post_process_entry(self, entry: Entry) -> None:
"""
Tracks the last entry processed
"""
self._last_entry = entry

def post_process_subscription(self):
"""
Creates an NFO file in the root of the output directory using the last entry
Creates output NFO using the first entry, and only creates it once
"""
if (
self.plugin_options.nfo_name is None
or self.plugin_options.nfo_root is None
or self._last_entry is None
not self._created_output_nfo
and self.plugin_options.nfo_name is not None
and self.plugin_options.nfo_root is not None
):
return

self._create_nfo(entry=self._last_entry, save_to_entry=False)
self._create_nfo(entry=entry, save_to_entry=False)
self._created_output_nfo = True
24 changes: 22 additions & 2 deletions tests/e2e/plugins/test_nfo_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def subscription_dict(output_directory):
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "the \n tag 🎸🎸",
},
"kodi_safe_multi_title 🎸": ["value 1 🎸", "value 2 🎸"],
# should not show third empty
"kodi_safe_multi_title 🎸": ["value 1 🎸", "value 2 🎸", ""],
"kodi_safe_multi_title_with_attrs": [
{
"attributes": {"🎸?": "value\nnewlines 🎸"},
Expand All @@ -29,7 +30,16 @@ def subscription_dict(output_directory):
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "the \n tag 2 🎸🎸",
},
{
"attributes": {"🎸?": "EMPTY TAG SHOULD NOT SHOW"},
"tag": "",
},
],
"empty_attribute_tag_SHOULD_NOT_SHOW": {
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "",
},
"empty_tag_SHOULD_NOT_SHOW": "",
},
},
"output_directory_nfo_tags": {
Expand All @@ -41,7 +51,8 @@ def subscription_dict(output_directory):
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "the \n tag 🎸🎸",
},
"kodi_safe_multi_title 🎸": ["value 1 🎸", "value 2 🎸"],
# should not show third empty
"kodi_safe_multi_title 🎸": ["value 1 🎸", "value 2 🎸", ""],
"kodi_safe_multi_title_with_attrs": [
{
"attributes": {"🎸?": "value\nnewlines 🎸"},
Expand All @@ -51,7 +62,16 @@ def subscription_dict(output_directory):
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "the \n tag 2 🎸🎸",
},
{
"attributes": {"🎸?": "EMPTY TAG SHOULD NOT SHOW"},
"tag": "",
},
],
"empty_attribute_tag_SHOULD_NOT_SHOW": {
"attributes": {"🎸?": "value\nnewlines 🎸"},
"tag": "",
},
"empty_tag_SHOULD_NOT_SHOW": "",
},
},
}
Expand Down

0 comments on commit 395691f

Please sign in to comment.