Paste product link straight in
This commit is contained in:
parent
72a705140b
commit
c098cd8fd0
4 changed files with 80 additions and 43 deletions
|
|
@ -1,14 +1,36 @@
|
|||
from ingredients.db import Ingredient, find_ingredients_by_meal_id, find_ingredients_by_recipe_id, insert_ingredient, delete_ingredients_by_meal_id
|
||||
|
||||
import units
|
||||
from products import Product, find_product_by_tag
|
||||
from products import Product, find_product_by_tag, get_or_create, add_missing_tags
|
||||
|
||||
from ingredient_parser import parse_multiple_ingredients
|
||||
from ingredient_parser import parse_ingredient
|
||||
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
def parse_ingredient_from_nlp(ingredients: List[str]) -> Ingredient:
|
||||
results = []
|
||||
for ingredient in parse_multiple_ingredients(ingredients):
|
||||
async def parse_ingredient_from_link(conn, link: str) -> Ingredient:
|
||||
match = re.match(r'^(\d+)?\s*(http.*)$', link)
|
||||
if not match:
|
||||
return None
|
||||
|
||||
quantity = int(match.group(1)) if match.group(1) else 1
|
||||
url = match.group(2)
|
||||
product = await get_or_create(conn, url, [])
|
||||
if product:
|
||||
await add_missing_tags(conn, product, [product.name])
|
||||
|
||||
return Ingredient(id=-1,
|
||||
name=product.name,
|
||||
line=f"{quantity}x {product.name}",
|
||||
unit=units.ITEMS.name,
|
||||
quantity=quantity,
|
||||
preparation='',
|
||||
product_id=product.id,
|
||||
product=product
|
||||
)
|
||||
|
||||
def parse_ingredient_from_nlp(ingredient_string: str) -> Ingredient:
|
||||
ingredient = parse_ingredient(ingredient_string)
|
||||
name = ingredient.name.text if ingredient.name else ''
|
||||
|
||||
quantity, unit = None, None
|
||||
|
|
@ -33,16 +55,14 @@ def parse_ingredient_from_nlp(ingredients: List[str]) -> Ingredient:
|
|||
if unit is None:
|
||||
unit = units.ITEMS.name
|
||||
|
||||
results.append(Ingredient(id=0,
|
||||
return Ingredient(id=0,
|
||||
line=ingredient.sentence,
|
||||
name=name,
|
||||
quantity=quantity,
|
||||
unit=unit,
|
||||
preparation=ingredient.preparation.text if ingredient.preparation else '',
|
||||
product_id=-1
|
||||
))
|
||||
|
||||
return results
|
||||
)
|
||||
|
||||
async def _find_existing_product(conn, ingredient: str) -> Product:
|
||||
async for item in find_product_by_tag(conn, ingredient):
|
||||
|
|
|
|||
19
main.py
19
main.py
|
|
@ -44,7 +44,24 @@ async def parse_ingredients(lines: Annotated[
|
|||
Query(alias="ingredients",
|
||||
title="Array of ingredients to parse")],
|
||||
conn: sqlite3.Connection = Depends(get_db)) -> List[ingredients.Ingredient]:
|
||||
result = ingredients.parse_ingredient_from_nlp(lines)
|
||||
|
||||
had_links = False
|
||||
result = []
|
||||
for line in lines:
|
||||
ingredient = await ingredients.parse_ingredient_from_link(conn, line)
|
||||
if ingredient:
|
||||
result.append(ingredient)
|
||||
had_links = True
|
||||
continue
|
||||
|
||||
ingredient = ingredients.parse_ingredient_from_nlp(line)
|
||||
if ingredient:
|
||||
result.append(ingredient)
|
||||
continue
|
||||
|
||||
if had_links:
|
||||
await conn.commit()
|
||||
|
||||
await ingredients.match_existing_products(conn, result)
|
||||
return result
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from persons import Person
|
|||
|
||||
from recipes.db import Recipe, insert_recipe, find_recipe_by_id, get_all, find_recipes_by_name, row_to_recipe, load_recipe_ingredients, hide_recipe
|
||||
from recipes.scraping import scrape_recipe_ldata as _scrape_recipe_ldata
|
||||
from ingredients import parse_ingredient_from_nlp as _parse_ingredient_from_nlp, match_existing_products as _match_existing_products
|
||||
from ingredients import parse_ingredient_from_nlp, match_existing_products
|
||||
|
||||
import re
|
||||
|
||||
|
|
@ -32,8 +32,8 @@ def find_yield(recipe_ldata: dict) -> int:
|
|||
return 4
|
||||
|
||||
async def _get_recipe_from_ldata(conn, url: str, ldata: dict, created_by: Person) -> dict:
|
||||
ingredients = _parse_ingredient_from_nlp(ldata['recipeIngredient'])
|
||||
ingredients = await _match_existing_products(conn, ingredients)
|
||||
ingredients = parse_ingredient_from_nlp(ldata['recipeIngredient'])
|
||||
ingredients = await match_existing_products(conn, ingredients)
|
||||
name = ldata['name'] if 'name' in ldata else url
|
||||
images = ldata['image'] if 'image' in ldata else []
|
||||
serves = find_yield(ldata)
|
||||
|
|
|
|||
Loading…
Reference in a new issue