2025-10-18 03:26:42 +00:00
|
|
|
import re
|
|
|
|
|
from typing import List, Optional
|
2024-01-17 07:21:16 +00:00
|
|
|
|
2024-05-21 12:12:09 +00:00
|
|
|
from ingredient_parser import parse_ingredient
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
import units
|
|
|
|
|
from ingredients.db import (
|
|
|
|
|
Ingredient,
|
|
|
|
|
delete_ingredients_by_meal_id as delete_ingredients_by_meal_id,
|
|
|
|
|
find_ingredient_by_id as find_ingredient_by_id,
|
|
|
|
|
find_ingredients_by_meal_id as find_ingredients_by_meal_id,
|
|
|
|
|
find_ingredients_by_recipe_id as find_ingredients_by_recipe_id,
|
|
|
|
|
insert_ingredient as insert_ingredient,
|
|
|
|
|
)
|
|
|
|
|
from products import Product, add_missing_tags, find_product_by_tag, get_or_create
|
|
|
|
|
|
2024-01-17 07:21:16 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
async def parse_ingredient_from_link(conn, link: str) -> Optional[Ingredient]:
|
|
|
|
|
match = re.match(r"^(\d+)?\s*(http.*)$", link)
|
2024-05-21 12:12:09 +00:00
|
|
|
if not match:
|
|
|
|
|
return None
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-05-21 12:12:09 +00:00
|
|
|
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])
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
return Ingredient(
|
|
|
|
|
id=-1,
|
2024-05-21 12:12:09 +00:00
|
|
|
name=product.name,
|
|
|
|
|
line=f"{quantity}x {product.name}",
|
|
|
|
|
unit=units.ITEMS.name,
|
2024-01-17 07:21:16 +00:00
|
|
|
quantity=quantity,
|
2025-10-18 03:26:42 +00:00
|
|
|
preparation="",
|
2024-05-21 12:12:09 +00:00
|
|
|
product_id=product.id,
|
2025-10-18 03:26:42 +00:00
|
|
|
product=product,
|
2024-05-21 12:12:09 +00:00
|
|
|
)
|
2025-10-18 03:26:42 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:09 +00:00
|
|
|
def parse_ingredient_from_nlp(ingredient_string: str) -> Ingredient:
|
|
|
|
|
ingredient = parse_ingredient(ingredient_string)
|
2025-10-18 03:26:42 +00:00
|
|
|
name = ingredient.name.text if ingredient.name else ""
|
2024-05-21 12:12:09 +00:00
|
|
|
|
|
|
|
|
quantity, unit = None, None
|
|
|
|
|
for amount in ingredient.amount:
|
|
|
|
|
if quantity is None and amount.quantity:
|
|
|
|
|
quantity = amount.quantity
|
|
|
|
|
|
|
|
|
|
if unit is None and amount.unit:
|
2024-09-22 02:09:52 +00:00
|
|
|
real_unit = units.get_unit(str(amount.unit))
|
2024-05-21 12:12:09 +00:00
|
|
|
if real_unit:
|
|
|
|
|
unit = real_unit.name
|
|
|
|
|
|
|
|
|
|
if isinstance(quantity, str):
|
|
|
|
|
try:
|
|
|
|
|
quantity = float(quantity)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if quantity is None or not isinstance(quantity, (int, float)):
|
|
|
|
|
quantity = 1
|
|
|
|
|
|
|
|
|
|
if unit is None:
|
|
|
|
|
unit = units.ITEMS.name
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
return Ingredient(
|
|
|
|
|
id=-1,
|
2024-05-21 12:12:09 +00:00
|
|
|
line=ingredient.sentence,
|
|
|
|
|
name=name,
|
|
|
|
|
quantity=quantity,
|
|
|
|
|
unit=unit,
|
2025-10-18 03:26:42 +00:00
|
|
|
preparation=ingredient.preparation.text if ingredient.preparation else "",
|
|
|
|
|
product_id=-1,
|
2024-05-21 12:12:09 +00:00
|
|
|
)
|
2024-01-17 07:21:16 +00:00
|
|
|
|
2025-07-29 07:15:38 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
async def _find_existing_product(conn, ingredient: str) -> Optional[Product]:
|
|
|
|
|
items = [item async for item in find_product_by_tag(conn, ingredient)]
|
|
|
|
|
return items[0] if items else None
|
|
|
|
|
|
2024-01-17 07:21:16 +00:00
|
|
|
|
|
|
|
|
async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[Ingredient]:
|
|
|
|
|
for ingredient in ingredients:
|
|
|
|
|
if not ingredient.product:
|
|
|
|
|
existing = await _find_existing_product(conn, ingredient.name)
|
|
|
|
|
if existing:
|
|
|
|
|
ingredient.product_id = existing.id
|
|
|
|
|
ingredient.product = existing
|
|
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
return ingredients
|