This commit is contained in:
jableader 2024-01-17 17:53:27 +11:00
parent b9ea237f44
commit 77ce3658bf
3 changed files with 13 additions and 4 deletions

View file

@ -41,7 +41,7 @@ async def parse_ingredients(lines: Annotated[
title="Array of ingredients to parse")],
conn: sqlite3.Connection = Depends(get_db)) -> List[recipes.Ingredient]:
ingredients = recipes.parse_ingredient_from_nlp(lines)
recipes.match_existing_products(conn, ingredients)
await recipes.match_existing_products(conn, ingredients)
return ingredients
class ProductUrl(BaseModel):

View file

@ -26,7 +26,7 @@ async def create(conn):
await conn.execute('''
CREATE TABLE IF NOT EXISTS ProductTag (
food_item_id INTEGER,
tag TEXT,
tag TEXT COLLATE NOCASE,
PRIMARY KEY (food_item_id, tag),
FOREIGN KEY (food_item_id) REFERENCES Product(id)
);''')

View file

@ -27,8 +27,15 @@ def parse_ingredient_from_nlp(ingredients: List[str]) -> Ingredient:
if real_unit:
unit = real_unit.name
if quantity is None:
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
@ -48,7 +55,9 @@ async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[I
if not ingredient.product:
existing = await find_existing_product(conn, ingredient.name)
if existing:
ingredient.product_id = existing.id
ingredient.product = existing
return ingredients
async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict:
@ -57,7 +66,7 @@ async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict:
name = ldata['name'] if 'name' in ldata else url
images = ldata['image'] if 'image' in ldata else []
if isinstance(images, list) and len(list) > 0 and isinstance(images[0], dict):
if isinstance(images, list) and len(images) > 0 and isinstance(images[0], dict):
images = [image['url'] for image in images]
if isinstance(images, dict):