Misc
This commit is contained in:
parent
b9ea237f44
commit
77ce3658bf
3 changed files with 13 additions and 4 deletions
2
main.py
2
main.py
|
|
@ -41,7 +41,7 @@ async def parse_ingredients(lines: Annotated[
|
||||||
title="Array of ingredients to parse")],
|
title="Array of ingredients to parse")],
|
||||||
conn: sqlite3.Connection = Depends(get_db)) -> List[recipes.Ingredient]:
|
conn: sqlite3.Connection = Depends(get_db)) -> List[recipes.Ingredient]:
|
||||||
ingredients = recipes.parse_ingredient_from_nlp(lines)
|
ingredients = recipes.parse_ingredient_from_nlp(lines)
|
||||||
recipes.match_existing_products(conn, ingredients)
|
await recipes.match_existing_products(conn, ingredients)
|
||||||
return ingredients
|
return ingredients
|
||||||
|
|
||||||
class ProductUrl(BaseModel):
|
class ProductUrl(BaseModel):
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ async def create(conn):
|
||||||
await conn.execute('''
|
await conn.execute('''
|
||||||
CREATE TABLE IF NOT EXISTS ProductTag (
|
CREATE TABLE IF NOT EXISTS ProductTag (
|
||||||
food_item_id INTEGER,
|
food_item_id INTEGER,
|
||||||
tag TEXT,
|
tag TEXT COLLATE NOCASE,
|
||||||
PRIMARY KEY (food_item_id, tag),
|
PRIMARY KEY (food_item_id, tag),
|
||||||
FOREIGN KEY (food_item_id) REFERENCES Product(id)
|
FOREIGN KEY (food_item_id) REFERENCES Product(id)
|
||||||
);''')
|
);''')
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,15 @@ def parse_ingredient_from_nlp(ingredients: List[str]) -> Ingredient:
|
||||||
if real_unit:
|
if real_unit:
|
||||||
unit = real_unit.name
|
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
|
quantity = 1
|
||||||
|
|
||||||
if unit is None:
|
if unit is None:
|
||||||
unit = units.ITEMS.name
|
unit = units.ITEMS.name
|
||||||
|
|
||||||
|
|
@ -48,7 +55,9 @@ async def match_existing_products(conn, ingredients: List[Ingredient]) -> List[I
|
||||||
if not ingredient.product:
|
if not ingredient.product:
|
||||||
existing = await find_existing_product(conn, ingredient.name)
|
existing = await find_existing_product(conn, ingredient.name)
|
||||||
if existing:
|
if existing:
|
||||||
|
ingredient.product_id = existing.id
|
||||||
ingredient.product = existing
|
ingredient.product = existing
|
||||||
|
|
||||||
return ingredients
|
return ingredients
|
||||||
|
|
||||||
async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict:
|
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
|
name = ldata['name'] if 'name' in ldata else url
|
||||||
images = ldata['image'] if 'image' in ldata else []
|
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]
|
images = [image['url'] for image in images]
|
||||||
|
|
||||||
if isinstance(images, dict):
|
if isinstance(images, dict):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue