1
0
Fork 0
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:
gpotter@gmail.com 2025-01-07 07:55:59 -08:00 committed by GitHub
parent eafb7b9ffc
commit 795c2cf575
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 5 deletions

View file

@ -120,7 +120,8 @@ def clean_image(image: str | list | dict | None = None, default: str = "no image
case str(image):
return [image]
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(_)}, *_]:
return [x["url"] for x in image if "url" in x]
case {"url": str(image)}:
@ -128,7 +129,10 @@ def clean_image(image: str | list | dict | None = None, default: str = "no image
case [{"@id": str(_)}, *_]:
return [x["@id"] for x in image if "@id" in x]
case _:
logger.exception(f"Unexpected type for image: {type(image)}, {image}")
logger.exception(
f"Unexpected type for image: {
type(image)}, {image}"
)
return [default]
@ -223,7 +227,10 @@ def clean_instructions(steps_object: list | dict | str, default: list | None = N
)
)
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:
@ -283,7 +290,10 @@ def clean_ingredients(ingredients: list | str | None, default: list | None = Non
case str(ingredients):
return [clean_string(ingredient) for ingredient in ingredients.splitlines() if ingredient.strip()]
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):
@ -521,7 +531,10 @@ def clean_categories(category: str | list) -> list[str]:
#
return [cat["name"] for cat in category if "name" in cat]
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]:
@ -570,4 +583,10 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
with contextlib.suppress(AttributeError, TypeError):
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

View file

@ -537,6 +537,24 @@ nutrition_test_cases = (
"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(
test_id="invalid keys get removed",
input={