improve servings parsing and AI failure logging

This commit is contained in:
vabene1111
2025-11-11 14:44:09 +01:00
parent bd2e9cc3d9
commit 748b91bb8a
2 changed files with 10 additions and 11 deletions

View File

@@ -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):

View File

@@ -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,