Skip to content

Commit

Permalink
fix: Allow scraping calories as number (#4854)
Browse files Browse the repository at this point in the history
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
  • Loading branch information
parumpum and Kuchenpirat authored Jan 7, 2025
1 parent eafb7b9 commit 795c2cf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
29 changes: 24 additions & 5 deletions mealie/services/scraper/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,19 @@ 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)}:
return [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]


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,24 @@ def test_cleaner_clean_tags(case: CleanerCase):
"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={
Expand Down

0 comments on commit 795c2cf

Please sign in to comment.