handle steps

This commit is contained in:
Marcus Wolschon
2023-01-13 22:21:31 +01:00
parent a4bf967f65
commit 5a0f07a6b2

View File

@@ -25,25 +25,22 @@ class Cookidoo(AbstractScraper):
if isinstance(instructions, list): if isinstance(instructions, list):
instructions_gist = [] instructions_gist = []
step_number = 1
for schema_instruction_item in instructions: for schema_instruction_item in instructions:
instructions_gist += self.extract_instructions_text(schema_instruction_item, "#", step_number) # combine lists of instructions per section into a flat list
step_number = step_number + 1 instructions_gist += self.extract_instructions_text(schema_instruction_item, "",)
# join all steps into a recipe steps = []
return "".join(self.normalize_instruction(instruction) for instruction in instructions_gist:
for instruction in instructions_gist) steps.append(self.normalize_instruction(instruction))
return steps
return instructions return instructions
def extract_instructions_text(self, schema_item, prefix, start_step_number): def extract_instructions_text(self, schema_item, prefix):
step_number = start_step_number
step_format = "\n\n" + prefix + _("Step {}") + "\n\n{}"
section_format = "\n\n{}\n\n"
instructions_gist = [] instructions_gist = []
if type(schema_item) is str: if type(schema_item) is str:
instructions_gist.append(step_format.format(step_number, schema_item)) instructions_gist.append(prefix + schema_item)
step_number = step_number + 1
elif schema_item.get("@type") == "HowToStep": elif schema_item.get("@type") == "HowToStep":
# steps make up simple recipes or a section of a more complex recipe # steps make up simple recipes or a section of a more complex recipe
if schema_item.get("name", False): if schema_item.get("name", False):
@@ -51,16 +48,14 @@ class Cookidoo(AbstractScraper):
if not schema_item.get("text").startswith( if not schema_item.get("text").startswith(
schema_item.get("name").rstrip(".") schema_item.get("name").rstrip(".")
): ):
instructions_gist.append(step_format.format(step_number, schema_item.get("name"))) instructions_gist.append(schema_item.get("name"))
instructions_gist.append(step_format.format(step_number, schema_item.get("text"))) instructions_gist.append(schema_item.get("text"))
elif schema_item.get("@type") == "HowToSection": elif schema_item.get("@type") == "HowToSection":
# complex recipes are made up of named sections that are made up of steps # complex recipes are made up of named sections that are made up of steps
section_name = schema_item.get("name") or schema_item.get("Name") or _("Instructions") section_name = schema_item.get("name") or schema_item.get("Name") or _("Instructions")
instructions_gist.append(section_format.format(section_name)) instructions_gist.append("**" + section_name + "**")
step_number = 1
for item in schema_item.get("itemListElement"): for item in schema_item.get("itemListElement"):
instructions_gist += self.extract_instructions_text(item, "#" + prefix, step_number) instructions_gist += self.extract_instructions_text(item, "#" + prefix)
step_number = step_number + 1
return instructions_gist return instructions_gist
def ingredients(self): def ingredients(self):