Write text imports through the recipe API, not the scraper
import_recipe_text posted plain Swedish text to /api/recipes/create/html-or-json. That endpoint is a scraper entry point: Mealie runs recipe_scrapers over whatever `data` holds and answers 400 for anything without schema.org markup. A text block never had any, so the tool returned 400 on every call it has ever made — including a minimal one-line test recipe. The seven session logs of the importing agent contain no successful text import; every recipe that landed came in through import_recipe_url. The existing test mocked _request away, so it asserted the payload shape and never that Mealie would accept it. Green test, broken tool. Create the recipe with POST /api/recipes and fill it with a PATCH instead. Both are already exercised against this instance. Ingredients are stored as readable `note` lines, which is what parse_ingredients expects to structure afterwards. Also accept a parenthetical on a section heading. `Gör så här (REKONSTRUERAD):` marks where the steps came from, and the old exact-match check turned that import away before it reached Mealie. A PATCH that fails now leaves a name-only recipe, so the error names the slug rather than leaving a silent empty stub. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+73
-9
@@ -307,6 +307,33 @@ def import_recipe_url(url: str, check_duplicates: bool = True) -> dict[str, Any]
|
||||
return {"imported": True, "recipe": recipe, "report": _import_report(recipe)}
|
||||
|
||||
|
||||
_SECTION_HEADING_RE = re.compile(
|
||||
r"(?im)^[ \t]*(Titel|Portioner|Ingredienser|Gör så här)[ \t]*(?:\([^)]*\))?[ \t]*:[ \t]*(.*)$"
|
||||
)
|
||||
|
||||
_STEP_NUMBER_RE = re.compile(r"^\s*\d+[.)]\s*")
|
||||
|
||||
|
||||
def _split_recipe_text(text: str) -> dict[str, str]:
|
||||
"""Split the documented Swedish text block into its four sections.
|
||||
|
||||
A heading may carry a parenthetical marking — ``Gör så här (REKONSTRUERAD):``
|
||||
— which belongs to the caller's note about the source, not to the recipe. It
|
||||
is accepted on the heading line and dropped from the body.
|
||||
"""
|
||||
matches = list(_SECTION_HEADING_RE.finditer(text))
|
||||
sections: dict[str, str] = {}
|
||||
for index, match in enumerate(matches):
|
||||
end = matches[index + 1].start() if index + 1 < len(matches) else len(text)
|
||||
body = f"{match.group(2)}\n{text[match.end():end]}"
|
||||
sections[match.group(1).lower()] = body.strip()
|
||||
return sections
|
||||
|
||||
|
||||
def _text_lines(body: str) -> list[str]:
|
||||
return [line.strip() for line in body.splitlines() if line.strip()]
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def import_recipe_text(text: str, source_url: str | None = None) -> dict[str, Any]:
|
||||
"""Import a normalized recipe text block (OCR, transcript, or hand-built Swedish).
|
||||
@@ -314,23 +341,60 @@ def import_recipe_text(text: str, source_url: str | None = None) -> dict[str, An
|
||||
Use this when the source is not a clean single-recipe page — an editorial
|
||||
roundup, a YouTube description, or a social caption. Build the text in Swedish
|
||||
with ``Titel`` / ``Portioner`` / ``Ingredienser`` / ``Gör så här`` sections.
|
||||
|
||||
The block is written with ``POST /api/recipes`` plus a ``PATCH``, not through
|
||||
``/api/recipes/create/html-or-json``. That endpoint is a scraper entry point:
|
||||
it runs recipe_scrapers over whatever ``data`` holds and answers 400 for
|
||||
anything without schema.org markup, so plain text never survived it.
|
||||
|
||||
Ingredients are stored as readable ``note`` lines. Run ``parse_ingredients``
|
||||
afterwards to add structure without changing how they read.
|
||||
"""
|
||||
sections = _split_recipe_text(text)
|
||||
required = ("Titel", "Ingredienser", "Gör så här")
|
||||
missing = [
|
||||
heading
|
||||
for heading in required
|
||||
if not re.search(rf"(?im)^\s*{re.escape(heading)}\s*:", text)
|
||||
]
|
||||
missing = [heading for heading in required if heading.lower() not in sections]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Text import requires explicit Swedish section headings; missing: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
payload: dict[str, Any] = {"data": text}
|
||||
|
||||
title = sections["titel"]
|
||||
ingredients = _text_lines(sections["ingredienser"])
|
||||
steps = [_STEP_NUMBER_RE.sub("", line) for line in _text_lines(sections["gör så här"])]
|
||||
if not title:
|
||||
raise ValueError("Text import requires a non-empty Titel")
|
||||
if not ingredients:
|
||||
raise ValueError("Text import requires at least one line under Ingredienser")
|
||||
|
||||
created = _request("POST", "/api/recipes", json={"name": title})
|
||||
slug = created if isinstance(created, str) else (created or {}).get("slug")
|
||||
if not slug:
|
||||
raise RuntimeError(f"Mealie did not return a slug for the created recipe: {created!r}")
|
||||
|
||||
patch: dict[str, Any] = {
|
||||
"recipeIngredient": [
|
||||
{"quantity": 0, "unit": None, "food": None, "note": line} for line in ingredients
|
||||
],
|
||||
"recipeInstructions": [{"text": step} for step in steps],
|
||||
}
|
||||
if sections.get("portioner"):
|
||||
patch["recipeYield"] = sections["portioner"]
|
||||
if source_url:
|
||||
payload["url"] = source_url
|
||||
created = _request("POST", "/api/recipes/create/html-or-json", json=payload)
|
||||
recipe = get_recipe(created) if isinstance(created, str) else created
|
||||
patch["orgURL"] = source_url
|
||||
|
||||
try:
|
||||
_request("PATCH", f"/api/recipes/{slug}", json=patch)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
# The name-only recipe already exists at this point; say so instead of
|
||||
# leaving an empty stub nobody knows about.
|
||||
raise RuntimeError(
|
||||
f"Recipe {slug!r} was created but the content patch failed "
|
||||
f"({exc.response.status_code}); it is empty in Mealie and needs "
|
||||
"filling in or removing by hand"
|
||||
) from exc
|
||||
|
||||
recipe = get_recipe(slug)
|
||||
return {"imported": True, "recipe": recipe, "report": _import_report(recipe)}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user