32 lines
No EOL
1.2 KiB
Python
32 lines
No EOL
1.2 KiB
Python
from recipes.db import Recipe, insert_recipe, find_recipe_by_id, get_all, find_recipes_by_name, row_to_recipe, load_recipe_ingredients
|
|
from recipes.scraping import scrape_recipe_ldata as _scrape_recipe_ldata
|
|
from ingredients import parse_ingredient_from_nlp as _parse_ingredient_from_nlp, match_existing_products as _match_existing_products
|
|
|
|
async def parse_recipe(conn, url: str) -> Recipe:
|
|
ldata = await _scrape_recipe_ldata(url)
|
|
if ldata:
|
|
return await _get_recipe_from_ldata(conn, url, ldata)
|
|
return None
|
|
|
|
async def _get_recipe_from_ldata(conn, url: str, ldata: dict) -> dict:
|
|
ingredients = _parse_ingredient_from_nlp(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 []
|
|
|
|
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=0,
|
|
name=name,
|
|
link=url,
|
|
image_urls=images,
|
|
ingredients=ingredients
|
|
) |