mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-19 13:19:41 +02:00
fix: Allow scraping calories as number (#4854)
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
parent
eafb7b9ffc
commit
795c2cf575
2 changed files with 42 additions and 5 deletions
|
@ -120,7 +120,8 @@ def clean_image(image: str | list | dict | None = None, default: str = "no image
|
||||||
case str(image):
|
case str(image):
|
||||||
return [image]
|
return [image]
|
||||||
case [str(_), *_]:
|
case [str(_), *_]:
|
||||||
return [x for x in image if x] # Only return non-null strings in list
|
# Only return non-null strings in list
|
||||||
|
return [x for x in image if x]
|
||||||
case [{"url": str(_)}, *_]:
|
case [{"url": str(_)}, *_]:
|
||||||
return [x["url"] for x in image if "url" in x]
|
return [x["url"] for x in image if "url" in x]
|
||||||
case {"url": str(image)}:
|
case {"url": str(image)}:
|
||||||
|
@ -128,7 +129,10 @@ def clean_image(image: str | list | dict | None = None, default: str = "no image
|
||||||
case [{"@id": str(_)}, *_]:
|
case [{"@id": str(_)}, *_]:
|
||||||
return [x["@id"] for x in image if "@id" in x]
|
return [x["@id"] for x in image if "@id" in x]
|
||||||
case _:
|
case _:
|
||||||
logger.exception(f"Unexpected type for image: {type(image)}, {image}")
|
logger.exception(
|
||||||
|
f"Unexpected type for image: {
|
||||||
|
type(image)}, {image}"
|
||||||
|
)
|
||||||
return [default]
|
return [default]
|
||||||
|
|
||||||
|
|
||||||
|
@ -223,7 +227,10 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
case _:
|
case _:
|
||||||
raise TypeError(f"Unexpected type for instructions: {type(steps_object)}, {steps_object}")
|
raise TypeError(
|
||||||
|
f"Unexpected type for instructions: {
|
||||||
|
type(steps_object)}, {steps_object}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_instruction_text(line: str | dict) -> str:
|
def _sanitize_instruction_text(line: str | dict) -> str:
|
||||||
|
@ -283,7 +290,10 @@ def clean_ingredients(ingredients: list | str | None, default: list | None = Non
|
||||||
case str(ingredients):
|
case str(ingredients):
|
||||||
return [clean_string(ingredient) for ingredient in ingredients.splitlines() if ingredient.strip()]
|
return [clean_string(ingredient) for ingredient in ingredients.splitlines() if ingredient.strip()]
|
||||||
case _:
|
case _:
|
||||||
raise TypeError(f"Unexpected type for ingredients: {type(ingredients)}, {ingredients}")
|
raise TypeError(
|
||||||
|
f"Unexpected type for ingredients: {
|
||||||
|
type(ingredients)}, {ingredients}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def clean_int(val: str | int | None, min: int | None = None, max: int | None = None):
|
def clean_int(val: str | int | None, min: int | None = None, max: int | None = None):
|
||||||
|
@ -521,7 +531,10 @@ def clean_categories(category: str | list) -> list[str]:
|
||||||
#
|
#
|
||||||
return [cat["name"] for cat in category if "name" in cat]
|
return [cat["name"] for cat in category if "name" in cat]
|
||||||
case _:
|
case _:
|
||||||
raise TypeError(f"Unexpected type for category: {type(category)}, {category}")
|
raise TypeError(
|
||||||
|
f"Unexpected type for category: {
|
||||||
|
type(category)}, {category}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def clean_tags(data: str | list[str]) -> list[str]:
|
def clean_tags(data: str | list[str]) -> list[str]:
|
||||||
|
@ -570,4 +583,10 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
|
||||||
with contextlib.suppress(AttributeError, TypeError):
|
with contextlib.suppress(AttributeError, TypeError):
|
||||||
output_nutrition[key] = str(float(output_nutrition[key]) * 1000)
|
output_nutrition[key] = str(float(output_nutrition[key]) * 1000)
|
||||||
|
|
||||||
|
for key in ["calories"]:
|
||||||
|
if val := nutrition.get(key, None):
|
||||||
|
if isinstance(val, int | float):
|
||||||
|
with contextlib.suppress(AttributeError, TypeError):
|
||||||
|
output_nutrition[key] = str(val)
|
||||||
|
|
||||||
return output_nutrition
|
return output_nutrition
|
||||||
|
|
|
@ -537,6 +537,24 @@ nutrition_test_cases = (
|
||||||
"fatContent": "10",
|
"fatContent": "10",
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
CleanerCase(
|
||||||
|
test_id="calories as int",
|
||||||
|
input={
|
||||||
|
"calories": 100,
|
||||||
|
},
|
||||||
|
expected={
|
||||||
|
"calories": "100",
|
||||||
|
},
|
||||||
|
),
|
||||||
|
CleanerCase(
|
||||||
|
test_id="calories as float",
|
||||||
|
input={
|
||||||
|
"calories": 100.0,
|
||||||
|
},
|
||||||
|
expected={
|
||||||
|
"calories": "100.0",
|
||||||
|
},
|
||||||
|
),
|
||||||
CleanerCase(
|
CleanerCase(
|
||||||
test_id="invalid keys get removed",
|
test_id="invalid keys get removed",
|
||||||
input={
|
input={
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue