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
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
import re
|
|
from typing import Optional
|
|
|
|
from ingredients import match_existing_products, parse_ingredient_from_nlp
|
|
from persons.models import Person
|
|
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,
|
|
find_recipe_by_id as find_recipe_by_id,
|
|
find_recipes_by_name as find_recipes_by_name,
|
|
find_recipes_by_name_paged as find_recipes_by_name_paged,
|
|
get_all as get_all,
|
|
get_all_paged as get_all_paged,
|
|
hide_recipe as hide_recipe,
|
|
insert_recipe as insert_recipe,
|
|
load_recipe_ingredients as load_recipe_ingredients,
|
|
row_to_recipe as row_to_recipe,
|
|
)
|
|
from recipes.scraping import scrape_recipe_ldata as _scrape_recipe_ldata
|
|
|
|
|
|
async def parse_recipe(conn, created_by: Person, url: str) -> Optional[Recipe]:
|
|
ldata = await _scrape_recipe_ldata(url)
|
|
if ldata:
|
|
return await _get_recipe_from_ldata(conn, url, ldata, created_by)
|
|
return None
|
|
|
|
|
|
def find_yield(recipe_ldata: dict) -> int:
|
|
if "recipeYield" in recipe_ldata:
|
|
yield_vals = recipe_ldata["recipeYield"]
|
|
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:
|
|
match = re.match(r"(\d+)", val)
|
|
if match:
|
|
return int(match.group(1))
|
|
|
|
return 4
|
|
|
|
|
|
async def _get_recipe_from_ldata(conn, url: str, ldata: dict, created_by: Person) -> Recipe:
|
|
ingredients = [
|
|
parse_ingredient_from_nlp(ingredient) for ingredient in ldata["recipeIngredient"]
|
|
]
|
|
ingredients = await match_existing_products(conn, ingredients)
|
|
name = ldata["name"] if "name" in ldata else url
|
|
images = ldata["image"] if "image" in ldata else []
|
|
serves = find_yield(ldata)
|
|
|
|
if isinstance(images, list) and len(images) > 0 and isinstance(images[0], dict):
|
|
images = [image["url"] for image in images]
|
|
|
|
if isinstance(images, dict):
|
|
images = [images["url"]]
|
|
|
|
if isinstance(images, str):
|
|
images = [images]
|
|
|
|
return Recipe(
|
|
id=-1,
|
|
name=name,
|
|
link=url,
|
|
serves=serves,
|
|
image_urls=images,
|
|
ingredients=ingredients,
|
|
created_by=created_by,
|
|
created_by_id=created_by.id,
|
|
)
|