basic importing working

This commit is contained in:
vabene1111
2020-06-23 10:34:04 +02:00
parent dc91e1e8ed
commit 8594346488
4 changed files with 139 additions and 33 deletions

View File

@@ -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('<p>', '')
ld_json['recipeInstructions'] = ld_json['recipeInstructions'].replace('</p>', '')
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)