2025-10-18 03:26:42 +00:00
|
|
|
import re
|
|
|
|
|
from typing import Optional
|
2024-05-19 11:22:30 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
from ingredients import match_existing_products, parse_ingredient_from_nlp
|
2025-11-01 12:36:46 +00:00
|
|
|
from api.dtos import MemberRef
|
2025-10-19 09:24:23 +00:00
|
|
|
from recipes.models import Recipe as Recipe
|
|
|
|
|
from recipes.repository import (
|
|
|
|
|
compute_prev_cursor as compute_prev_cursor,
|
|
|
|
|
count_all as count_all,
|
|
|
|
|
count_by_name as count_by_name,
|
2025-11-01 02:58:46 +00:00
|
|
|
count_all_scoped as count_all_scoped,
|
|
|
|
|
count_by_name_scoped as count_by_name_scoped,
|
2025-10-18 03:26:42 +00:00
|
|
|
find_recipe_by_id as find_recipe_by_id,
|
2025-11-01 02:58:46 +00:00
|
|
|
find_recipe_by_id_scoped as find_recipe_by_id_scoped,
|
2025-10-18 03:26:42 +00:00
|
|
|
find_recipes_by_name as find_recipes_by_name,
|
2025-10-18 05:44:36 +00:00
|
|
|
find_recipes_by_name_paged as find_recipes_by_name_paged,
|
2025-11-01 02:58:46 +00:00
|
|
|
find_recipes_by_name_paged_scoped as find_recipes_by_name_paged_scoped,
|
2025-10-18 03:26:42 +00:00
|
|
|
get_all as get_all,
|
2025-10-18 05:44:36 +00:00
|
|
|
get_all_paged as get_all_paged,
|
2025-11-01 02:58:46 +00:00
|
|
|
get_all_paged_scoped as get_all_paged_scoped,
|
2025-10-18 03:26:42 +00:00
|
|
|
insert_recipe as insert_recipe,
|
2025-11-01 02:58:46 +00:00
|
|
|
insert_recipe_scoped as insert_recipe_scoped,
|
2025-10-18 03:26:42 +00:00
|
|
|
load_recipe_ingredients as load_recipe_ingredients,
|
|
|
|
|
row_to_recipe as row_to_recipe,
|
|
|
|
|
)
|
2024-01-17 07:21:16 +00:00
|
|
|
from recipes.scraping import scrape_recipe_ldata as _scrape_recipe_ldata
|
2024-01-13 03:21:38 +00:00
|
|
|
|
2024-05-19 11:22:30 +00:00
|
|
|
|
2025-11-01 08:58:33 +00:00
|
|
|
async def parse_recipe(conn, created_by, url: str) -> Optional[Recipe]:
|
2024-01-17 07:21:16 +00:00
|
|
|
ldata = await _scrape_recipe_ldata(url)
|
|
|
|
|
if ldata:
|
2024-05-19 11:22:30 +00:00
|
|
|
return await _get_recipe_from_ldata(conn, url, ldata, created_by)
|
2024-01-13 03:21:38 +00:00
|
|
|
return None
|
2024-05-19 11:22:30 +00:00
|
|
|
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-05-19 11:22:30 +00:00
|
|
|
def find_yield(recipe_ldata: dict) -> int:
|
2025-10-18 03:26:42 +00:00
|
|
|
if "recipeYield" in recipe_ldata:
|
|
|
|
|
yield_vals = recipe_ldata["recipeYield"]
|
2024-05-19 11:22:30 +00:00
|
|
|
if not isinstance(yield_vals, list):
|
|
|
|
|
yield_vals = [yield_vals]
|
|
|
|
|
|
|
|
|
|
for val in yield_vals:
|
|
|
|
|
try:
|
|
|
|
|
return int(val)
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
for val in yield_vals:
|
2025-10-18 03:26:42 +00:00
|
|
|
match = re.match(r"(\d+)", val)
|
2024-05-19 11:22:30 +00:00
|
|
|
if match:
|
|
|
|
|
return int(match.group(1))
|
|
|
|
|
|
|
|
|
|
return 4
|
2025-10-18 03:26:42 +00:00
|
|
|
|
|
|
|
|
|
2025-11-01 08:58:33 +00:00
|
|
|
async def _get_recipe_from_ldata(conn, url: str, ldata: dict, created_by) -> Recipe:
|
2025-10-18 03:26:42 +00:00
|
|
|
ingredients = [
|
|
|
|
|
parse_ingredient_from_nlp(ingredient) for ingredient in ldata["recipeIngredient"]
|
|
|
|
|
]
|
2024-05-21 12:12:09 +00:00
|
|
|
ingredients = await match_existing_products(conn, ingredients)
|
2025-10-18 03:26:42 +00:00
|
|
|
name = ldata["name"] if "name" in ldata else url
|
|
|
|
|
images = ldata["image"] if "image" in ldata else []
|
2024-05-19 11:22:30 +00:00
|
|
|
serves = find_yield(ldata)
|
2025-10-18 03:26:42 +00:00
|
|
|
|
2024-01-17 06:53:27 +00:00
|
|
|
if isinstance(images, list) and len(images) > 0 and isinstance(images[0], dict):
|
2025-10-18 03:26:42 +00:00
|
|
|
images = [image["url"] for image in images]
|
2024-01-13 23:41:15 +00:00
|
|
|
|
|
|
|
|
if isinstance(images, dict):
|
2025-10-18 03:26:42 +00:00
|
|
|
images = [images["url"]]
|
2024-01-13 23:41:15 +00:00
|
|
|
|
2024-01-13 11:37:39 +00:00
|
|
|
if isinstance(images, str):
|
|
|
|
|
images = [images]
|
|
|
|
|
|
2025-11-01 12:36:46 +00:00
|
|
|
# Ensure created_by is a MemberRef (not a full User) to satisfy model typing
|
|
|
|
|
mref = (
|
|
|
|
|
MemberRef(id=created_by.id, display_name=created_by.display_name)
|
|
|
|
|
if created_by is not None
|
|
|
|
|
else None
|
|
|
|
|
)
|
2024-01-13 03:21:38 +00:00
|
|
|
return Recipe(
|
2025-10-19 09:24:23 +00:00
|
|
|
id=-1,
|
2024-01-13 08:44:07 +00:00
|
|
|
name=name,
|
2024-01-13 03:21:38 +00:00
|
|
|
link=url,
|
2024-05-19 11:22:30 +00:00
|
|
|
serves=serves,
|
2024-01-13 08:44:07 +00:00
|
|
|
image_urls=images,
|
2024-05-19 11:22:30 +00:00
|
|
|
ingredients=ingredients,
|
2025-11-01 12:36:46 +00:00
|
|
|
created_by=mref,
|
|
|
|
|
created_by_id=created_by.id if created_by is not None else -1,
|
2025-10-18 03:26:42 +00:00
|
|
|
)
|