From 77ce3658bfde06487424b06eb4b25e2ac012947e Mon Sep 17 00:00:00 2001 From: jableader Date: Wed, 17 Jan 2024 17:53:27 +1100 Subject: [PATCH] Misc --- main.py | 2 +- products/db.py | 2 +- recipes/__init__.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 3f3cb43..516f8ca 100644 --- a/main.py +++ b/main.py @@ -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): diff --git a/products/db.py b/products/db.py index aad5046..ebd32b3 100644 --- a/products/db.py +++ b/products/db.py @@ -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) );''') diff --git a/recipes/__init__.py b/recipes/__init__.py index a26ca9d..2ff39bf 100644 --- a/recipes/__init__.py +++ b/recipes/__init__.py @@ -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):