commit fcd005b8624023547f28b7b28e59e6099bcfc7d4
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 20:24:07 2025 +1100
Openapi tightening
commit f93bd8f641d561052c7bd075bae321b4ff3b676d
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 19:03:52 2025 +1100
Removed refactor strategy doc
commit 0c5a61092f522be0c47cbbe86917c8a7e4e2d339
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 17:48:33 2025 +1100
mypy & ruff checks
commit 23d66d6b18984127e17c73c3063f6120385935e9
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 16:49:35 2025 +1100
Final removal of db.py files
commit f454aed1ca9783cc558cc203f29a7fe31b62a975
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:42:31 2025 +1100
Finalise restructure, remove db.py files
commit 7187f6dd89489521538791c6bdebb426514beb99
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:34:54 2025 +1100
commit 6fea227ae20d32b8eb1e7a4885006a620fcc7bb1
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:32:53 2025 +1100
commit 27415e7e02d89195ad514cb017a9dbbf84d7a5e4
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:31:10 2025 +1100
commit b773428033d855f9ad82005602e049c1a2e3c585
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:28:58 2025 +1100
commit 116592c95278d995f4c516e87f2cea43cf5b7735
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:25:21 2025 +1100
commit 03ec565faea088971968ee2f9bb83e2de16b21f3
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 19 15:21:29 2025 +1100
Plan
93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
import re
|
|
from typing import List, Optional
|
|
|
|
from ingredient_parser import parse_ingredient
|
|
|
|
import units
|
|
from ingredients.models import Ingredient
|
|
from ingredients.repository import (
|
|
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,
|
|
find_ingredients_by_recipe_ids as find_ingredients_by_recipe_ids,
|
|
insert_ingredient as insert_ingredient,
|
|
)
|
|
from products import Product, add_missing_tags, find_product_by_tag, get_or_create
|
|
|
|
|
|
async def parse_ingredient_from_link(conn, link: str) -> Optional[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,
|
|
)
|
|
return None
|
|
|
|
|
|
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
|
|
for amount in ingredient.amount:
|
|
if quantity is None and amount.quantity:
|
|
quantity = amount.quantity
|
|
|
|
if unit is None and amount.unit:
|
|
real_unit = units.get_unit(str(amount.unit))
|
|
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
|
|
|
|
return Ingredient(
|
|
id=-1,
|
|
line=ingredient.sentence,
|
|
name=name,
|
|
quantity=quantity,
|
|
unit=unit,
|
|
preparation=ingredient.preparation.text if ingredient.preparation else "",
|
|
product_id=-1,
|
|
)
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
return ingredients
|