commit merge from Patralos/recipes
This commit is contained in:
parent
9930789aa8
commit
3a48d0e580
@ -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
|
@ -132,6 +132,7 @@ def parse_ingredients(ingredients):
|
||||
|
||||
for x in ingredients:
|
||||
if x.replace(' ', '') != '':
|
||||
x = x.replace('½', "0.5").replace('¼', "0.25").replace('¾', "0.75")
|
||||
try:
|
||||
amount, unit, ingredient, note = parse_single_ingredient(x)
|
||||
if ingredient:
|
||||
|
@ -28,8 +28,8 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest,
|
||||
CustomIsOwner, CustomIsShare,
|
||||
CustomIsShared, CustomIsUser,
|
||||
group_required)
|
||||
from cookbook.helper.recipe_url_import import get_from_html
|
||||
from cookbook.helper.recipe_raw_import import get_from_raw
|
||||
from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json
|
||||
from cookbook.helper.recipe_html_import import get_from_html
|
||||
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
|
||||
MealType, Recipe, RecipeBook, ShoppingList,
|
||||
ShoppingListEntry, ShoppingListRecipe, Step,
|
||||
@ -608,9 +608,9 @@ def recipe_from_url_old(request):
|
||||
|
||||
|
||||
@group_required('user')
|
||||
def recipe_from_raw(request):
|
||||
raw_text = request.POST['raw_text']
|
||||
recipe_json, recipe_tree = get_from_raw(raw_text)
|
||||
def recipe_from_html(request):
|
||||
html_data = request.POST['html_data']
|
||||
recipe_json, recipe_tree = get_from_html(html_data)
|
||||
return JsonResponse({
|
||||
'recipe_tree': recipe_tree,
|
||||
'recipe_json': recipe_json
|
||||
|
@ -101,12 +101,3 @@ def export_recipe(request):
|
||||
|
||||
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')
|
||||
|
@ -224,10 +224,10 @@ def latest_shopping_list(request):
|
||||
|
||||
@group_required('user')
|
||||
def shopping_list(request, pk=None):
|
||||
raw_list = request.GET.getlist('r')
|
||||
html_list = request.GET.getlist('r')
|
||||
|
||||
recipes = []
|
||||
for r in raw_list:
|
||||
for r in html_list:
|
||||
r = r.replace('[', '').replace(']', '')
|
||||
if re.match(r'^([0-9])+,([0-9])+[.]*([0-9])*$', r):
|
||||
rid, multiplier = r.split(',')
|
||||
|
Loading…
Reference in New Issue
Block a user