diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index fc4dfbc66..4afca8061 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -1,6 +1,7 @@ import re from django.http import JsonResponse +from django.utils.dateparse import parse_datetime, parse_duration from cookbook.models import Keyword @@ -8,6 +9,12 @@ from cookbook.models import Keyword def find_recipe_json(ld_json): ld_json['org'] = str(ld_json) + if type(ld_json['name']) == list: + try: + ld_json['name'] = ld_json['name'][0] + except: + ld_json['name'] = 'ERROR' + # some sites use ingredients instead of recipeIngredients if 'recipeIngredient' not in ld_json and 'ingredients' in ld_json: ld_json['recipeIngredient'] = ld_json['ingredients'] @@ -22,13 +29,21 @@ def find_recipe_json(ld_json): for x in ld_json['recipeIngredient']: ingredient_split = x.split() if len(ingredient_split) > 2: - ingredients.append({'amount': ingredient_split[0], 'unit': ingredient_split[1], 'ingredient': " ".join(ingredient_split[2:])}) + try: + ingredients.append({'amount': float(ingredient_split[0].replace(',', '.')), 'unit': ingredient_split[1], 'ingredient': " ".join(ingredient_split[2:])}) + except ValueError: + ingredients.append({'amount': 0, 'unit': '', 'ingredient': " ".join(ingredient_split)}) if len(ingredient_split) == 2: - ingredients.append({'amount': ingredient_split[0], 'unit': '', 'ingredient': " ".join(ingredient_split[1:])}) + try: + ingredients.append({'amount': float(ingredient_split[0].replace(',', '.')), 'unit': '', 'ingredient': " ".join(ingredient_split[1:])}) + except ValueError: + ingredients.append({'amount': 0, 'unit': '', 'ingredient': " ".join(ingredient_split)}) if len(ingredient_split) == 1: ingredients.append({'amount': 0, 'unit': '', 'ingredient': " ".join(ingredient_split)}) ld_json['recipeIngredient'] = ingredients + else: + ld_json['recipeIngredient'] = [] if 'keywords' in ld_json: keywords = [] @@ -49,6 +64,8 @@ def find_recipe_json(ld_json): keywords.append({'id': "null", 'text': kw.strip()}) ld_json['keywords'] = keywords + else: + ld_json['keywords'] = [] if 'recipeInstructions' in ld_json: instructions = '' @@ -66,6 +83,8 @@ def find_recipe_json(ld_json): ld_json['recipeInstructions'] = re.sub(' +', ' ', ld_json['recipeInstructions']) ld_json['recipeInstructions'] = ld_json['recipeInstructions'].replace('
', '') ld_json['recipeInstructions'] = ld_json['recipeInstructions'].replace('
', '') + else: + ld_json['recipeInstructions'] = '' if 'image' in ld_json: # check if list of images is returned, take first if so @@ -79,4 +98,14 @@ def find_recipe_json(ld_json): if 'http' not in ld_json['image']: ld_json['image'] = '' + if 'cookTime' in ld_json: + if type(ld_json['cookTime']) == list and len(ld_json['cookTime']) > 0: + ld_json['cookTime'] = ld_json['cookTime'][0] + ld_json['cookTime'] = round(parse_duration(ld_json['cookTime']).seconds/60) + + if 'prepTime' in ld_json: + if type(ld_json['prepTime']) == list and len(ld_json['prepTime']) > 0: + ld_json['prepTime'] = ld_json['prepTime'][0] + ld_json['prepTime'] = round(parse_duration(ld_json['prepTime']).seconds/60) + return JsonResponse(ld_json) diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index ec0ae237e..e37888511 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -22,7 +22,7 @@+ {% blocktrans %} Only websites containing ld+json or microdata information can currently + be imported. Most big recipe pages support this. If you site cannot be imported but + you think + it probably has some kind of structured data feel free to post an example in the + github issues.{% endblocktrans %} +
+ {% trans 'Google ld+json Info' %} + {% trans 'GitHub Issues' %} + {% trans 'Recipe Markup Specification' %} + + +