From 748b91bb8a8908636684a1334b4f3877045b307e Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 11 Nov 2025 14:44:09 +0100 Subject: [PATCH] improve servings parsing and AI failure logging --- cookbook/helper/recipe_url_import.py | 14 +++----------- cookbook/views/api.py | 7 +++++++ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index e9404447e..a133091e9 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -69,16 +69,8 @@ def get_from_scraper(scrape, request): recipe_json['description'] = parse_description(description) recipe_json['description'] = automation_engine.apply_regex_replace_automation(recipe_json['description'], Automation.DESCRIPTION_REPLACE) - # assign servings attributes - try: - # dont use scrape.yields() as this will always return "x servings" or "x items", should be improved in scrapers directly - # max(x,1) to prevent 0 servings which breaks scaling - servings = max(scrape.schema.data.get('recipeYield') or 1, 1) - except Exception: - servings = 1 - - recipe_json['servings'] = parse_servings(servings) - recipe_json['servings_text'] = parse_servings_text(servings) + recipe_json['servings'] = parse_servings(scrape.schema.data.get('recipeYield')) + recipe_json['servings_text'] = parse_servings_text(scrape.schema.data.get('recipeYield')) # assign time attributes try: @@ -407,7 +399,7 @@ def parse_servings(servings): def parse_servings_text(servings): if isinstance(servings, str): try: - servings = re.sub("\\d+", '', servings).strip() + servings = re.sub("\\d+", '', servings, 1).strip() except Exception: servings = '' if isinstance(servings, list): diff --git a/cookbook/views/api.py b/cookbook/views/api.py index b8dbe4134..c44459f37 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -2562,6 +2562,13 @@ class AiImportView(APIView): 'msg': "Error parsing AI results. Response Text:\n\n" + response_text } return Response(RecipeFromSourceResponseSerializer(context={'request': request}).to_representation(response), status=status.HTTP_400_BAD_REQUEST) + except Exception: + traceback.print_exc() + response = { + 'error': True, + 'msg': "Error processing AI results. Response Text:\n\n" + response_text + "\n\n" + traceback.format_exc() + } + return Response(RecipeFromSourceResponseSerializer(context={'request': request}).to_representation(response), status=status.HTTP_400_BAD_REQUEST) else: response = { 'error': True,