commit merge from Patralos/recipes
This commit is contained in:
parent
91e36eb222
commit
34ff484830
@ -1,98 +0,0 @@
|
|||||||
|
|
||||||
# %%
|
|
||||||
import json
|
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
# from cookbook.helper.ingredient_parser import parse as parse_ingredient
|
|
||||||
from cookbook.helper import recipe_url_import as helper
|
|
||||||
from django.http import JsonResponse
|
|
||||||
from django.utils.dateparse import parse_duration
|
|
||||||
|
|
||||||
|
|
||||||
# %%
|
|
||||||
|
|
||||||
# %%
|
|
||||||
def get_from_raw(raw_text):
|
|
||||||
def build_node(k, v):
|
|
||||||
if isinstance(v, dict):
|
|
||||||
node = {
|
|
||||||
'name': k,
|
|
||||||
'value': k,
|
|
||||||
'children': get_children_dict(v)
|
|
||||||
}
|
|
||||||
elif isinstance(v, list):
|
|
||||||
node = {
|
|
||||||
'name': k,
|
|
||||||
'value': k,
|
|
||||||
'children': get_children_list(v)
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
node = {
|
|
||||||
'name': k + ": " + str(v),
|
|
||||||
'value': str(v)
|
|
||||||
}
|
|
||||||
return node
|
|
||||||
|
|
||||||
def get_children_dict(children):
|
|
||||||
kid_list = []
|
|
||||||
for k, v in children.items():
|
|
||||||
kid_list.append(build_node(k, v))
|
|
||||||
return kid_list
|
|
||||||
|
|
||||||
def get_children_list(children):
|
|
||||||
kid_list = []
|
|
||||||
for kid in children:
|
|
||||||
if type(kid) == list:
|
|
||||||
node = {
|
|
||||||
'name': "unknown list",
|
|
||||||
'value': "unknown list",
|
|
||||||
'children': get_children_list(kid)
|
|
||||||
}
|
|
||||||
kid_list.append(node)
|
|
||||||
elif type(kid) == dict:
|
|
||||||
for k, v in kid.items():
|
|
||||||
kid_list.append(build_node(k, v))
|
|
||||||
else:
|
|
||||||
kid_list.append({
|
|
||||||
'name': kid,
|
|
||||||
'value': kid
|
|
||||||
})
|
|
||||||
return kid_list
|
|
||||||
|
|
||||||
recipe_items = ['recipeIngredient', 'keywords', 'recipeInstructions', 'image',
|
|
||||||
'cookTime', 'prepTime', 'servings', 'name']
|
|
||||||
extra_items = ['recipeYield', 'title', 'recipeCategory', 'recipeCuisine']
|
|
||||||
|
|
||||||
soup = BeautifulSoup(raw_text, "html.parser")
|
|
||||||
recipe_json = {}
|
|
||||||
recipe_tree = []
|
|
||||||
# first try finding ld+json as its most common
|
|
||||||
for ld in soup.find_all('script', type='application/ld+json'):
|
|
||||||
ld_json = helper.find_recipe_json(json.loads(ld.string), '')
|
|
||||||
for item in recipe_items:
|
|
||||||
if item in ld_json:
|
|
||||||
recipe_json[item] = ld_json[item]
|
|
||||||
recipe_items.remove(item)
|
|
||||||
del ld_json[item]
|
|
||||||
for k, v in ld_json.items():
|
|
||||||
if isinstance(v, dict):
|
|
||||||
node = {
|
|
||||||
'name': k,
|
|
||||||
'value': k,
|
|
||||||
'children': get_children_dict(v)
|
|
||||||
}
|
|
||||||
elif isinstance(v, list):
|
|
||||||
node = {
|
|
||||||
'name': k,
|
|
||||||
'value': k,
|
|
||||||
'children': get_children_list(v)
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
node = {
|
|
||||||
'name': k + ": " + str(v),
|
|
||||||
'value': str(v)
|
|
||||||
}
|
|
||||||
recipe_tree.append(node)
|
|
||||||
# TODO put recipe_tree and json_recipe in the JSON response
|
|
||||||
print(recipe_tree)
|
|
||||||
return recipe_json, recipe_tree
|
|
@ -151,6 +151,7 @@ def parse_ingredients(ingredients):
|
|||||||
|
|
||||||
for x in ingredients:
|
for x in ingredients:
|
||||||
if x.replace(' ', '') != '':
|
if x.replace(' ', '') != '':
|
||||||
|
x = x.replace('½', "0.5").replace('¼', "0.25").replace('¾', "0.75")
|
||||||
try:
|
try:
|
||||||
amount, unit, ingredient, note = parse_single_ingredient(x)
|
amount, unit, ingredient, note = parse_single_ingredient(x)
|
||||||
if ingredient:
|
if ingredient:
|
||||||
|
@ -35,8 +35,8 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest,
|
|||||||
CustomIsOwner, CustomIsShare,
|
CustomIsOwner, CustomIsShare,
|
||||||
CustomIsShared, CustomIsUser,
|
CustomIsShared, CustomIsUser,
|
||||||
group_required)
|
group_required)
|
||||||
from cookbook.helper.recipe_url_import import get_from_html
|
from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json
|
||||||
from cookbook.helper.recipe_raw_import import get_from_raw
|
from cookbook.helper.recipe_html_import import get_from_html
|
||||||
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
|
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
|
||||||
MealType, Recipe, RecipeBook, ShoppingList,
|
MealType, Recipe, RecipeBook, ShoppingList,
|
||||||
ShoppingListEntry, ShoppingListRecipe, Step,
|
ShoppingListEntry, ShoppingListRecipe, Step,
|
||||||
@ -698,9 +698,9 @@ def recipe_from_url(request):
|
|||||||
|
|
||||||
|
|
||||||
@group_required('user')
|
@group_required('user')
|
||||||
def recipe_from_raw(request):
|
def recipe_from_html(request):
|
||||||
raw_text = request.POST['raw_text']
|
html_data = request.POST['html_data']
|
||||||
recipe_json, recipe_tree = get_from_raw(raw_text)
|
recipe_json, recipe_tree = get_from_html(html_data)
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
'recipe_tree': recipe_tree,
|
'recipe_tree': recipe_tree,
|
||||||
'recipe_json': recipe_json
|
'recipe_json': recipe_json
|
||||||
|
@ -107,12 +107,3 @@ def export_recipe(request):
|
|||||||
|
|
||||||
return render(request, 'export.html', {'form': form})
|
return render(request, 'export.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
@group_required('user')
|
|
||||||
def import_json(request):
|
|
||||||
if request.method == "POST":
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return render(request, 'import_json.html')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user