diff --git a/mealie/services/scraper/cleaner.py b/mealie/services/scraper/cleaner.py index e0eee3ce4..5d26eace5 100644 --- a/mealie/services/scraper/cleaner.py +++ b/mealie/services/scraper/cleaner.py @@ -220,11 +220,14 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N # }, # } # + # Some sites (e.g. NYT Cooking) emit empty HowToSection placeholders + # with no itemListElement key, or use "item" per the schema.org spec. + # Use .get() with both fallbacks so those sections are skipped gracefully. steps_object = typing.cast(list[dict[str, str]], steps_object) return clean_instructions( functools.reduce( operator.concat, # type: ignore - [x["itemListElement"] for x in steps_object], + [x.get("itemListElement", x.get("item", [])) for x in steps_object], [], ) ) diff --git a/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py b/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py index c984bc173..e611f1329 100644 --- a/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py +++ b/tests/unit_tests/services_tests/scraper_tests/test_cleaner_parts.py @@ -224,6 +224,55 @@ instruction_test_cases = ( ], expected=None, ), + CleanerCase( + test_id="how to steps with empty section (e.g. NYT Cooking)", + input=[ + { + "@type": "HowToSection", + }, + { + "@type": "HowToSection", + "itemListElement": [ + { + "@type": "HowToStep", + "text": "Instruction A", + }, + { + "@type": "HowToStep", + "text": "Instruction B", + }, + { + "@type": "HowToStep", + "text": "Instruction C", + }, + ], + }, + ], + expected=None, + ), + CleanerCase( + test_id="how to steps using 'item' key (schema.org alternate)", + input=[ + { + "@type": "HowToSection", + "item": [ + { + "@type": "HowToStep", + "text": "Instruction A", + }, + { + "@type": "HowToStep", + "text": "Instruction B", + }, + { + "@type": "HowToStep", + "text": "Instruction C", + }, + ], + }, + ], + expected=None, + ), CleanerCase( test_id="excessive whitespace str (1)", input="Instruction A\n\nInstruction B\n\nInstruction C\n\n",