From b03fa4fdf2e839589e4a3476bfdd1f59c8e47cb4 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 11 Jul 2022 17:21:56 -0500 Subject: [PATCH] updated cookbookapp importer to handle multi-step recipes --- cookbook/integration/cookbookapp.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cookbook/integration/cookbookapp.py b/cookbook/integration/cookbookapp.py index 46e6d7bf3..c35de64f2 100644 --- a/cookbook/integration/cookbookapp.py +++ b/cookbook/integration/cookbookapp.py @@ -10,7 +10,8 @@ import validators import yaml from cookbook.helper.ingredient_parser import IngredientParser -from cookbook.helper.recipe_url_import import get_images_from_soup, iso_duration_to_minutes, get_from_scraper +from cookbook.helper.recipe_url_import import (get_from_scraper, get_images_from_soup, + iso_duration_to_minutes) from cookbook.helper.scrapers.scrapers import text_scraper from cookbook.integration.integration import Integration from cookbook.models import Ingredient, Keyword, Recipe, Step @@ -45,8 +46,8 @@ class CookBookApp(Integration): except Exception: pass - - step = Step.objects.create(instruction=recipe_json['recipeInstructions'], space=self.request.space, ) + # assuming import files only contain single step + step = Step.objects.create(instruction=recipe_json['steps'][0]['instruction'], space=self.request.space, ) if 'nutrition' in recipe_json: step.instruction = step.instruction + '\n\n' + recipe_json['nutrition'] @@ -55,11 +56,13 @@ class CookBookApp(Integration): recipe.steps.add(step) ingredient_parser = IngredientParser(self.request, True) - for ingredient in recipe_json['recipeIngredient']: - f = ingredient_parser.get_food(ingredient['ingredient']['text']) - u = ingredient_parser.get_unit(ingredient['unit']['text']) + for ingredient in recipe_json['steps'][0]['ingredients']: + f = ingredient_parser.get_food(ingredient['food']['name']) + u = None + if unit := ingredient.get('unit', None): + u = ingredient_parser.get_unit(unit.get('name', None)) step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'], space=self.request.space, + food=f, unit=u, amount=ingredient.get('amount', None), note=ingredient.get('note', None), original_text=ingredient.get('original_text', None), space=self.request.space, )) if len(images) > 0: