mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-08 00:43:12 -05:00
feat: adding the rest ofthe nutrition properties from schema.org (#4301)
This commit is contained in:
@@ -12,6 +12,18 @@ from mealie.services.scraper import cleaner
|
||||
from ._migration_base import BaseMigrator
|
||||
from .utils.migration_helpers import scrape_image, split_by_line_break, split_by_semicolon
|
||||
|
||||
nutrition_map = {
|
||||
"carbohydrate": "carbohydrateContent",
|
||||
"protein": "proteinContent",
|
||||
"fat": "fatContent",
|
||||
"saturatedfat": "saturatedFatContent",
|
||||
"transfat": "transFatContent",
|
||||
"sodium": "sodiumContent",
|
||||
"fiber": "fiberContent",
|
||||
"sugar": "sugarContent",
|
||||
"unsaturatedfat": "unsaturatedFatContent",
|
||||
}
|
||||
|
||||
|
||||
class MyRecipeBoxMigrator(BaseMigrator):
|
||||
def __init__(self, **kwargs):
|
||||
@@ -53,22 +65,26 @@ class MyRecipeBoxMigrator(BaseMigrator):
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def parse_nutrition(self, input: Any) -> dict | None:
|
||||
if not input or not isinstance(input, str):
|
||||
def parse_nutrition(self, input_: Any) -> dict | None:
|
||||
if not input_ or not isinstance(input_, str):
|
||||
return None
|
||||
|
||||
nutrition = {}
|
||||
|
||||
vals = [x.strip() for x in input.split(",") if x]
|
||||
vals = (x.strip() for x in input_.split("\n") if x)
|
||||
for val in vals:
|
||||
try:
|
||||
key, value = val.split(":", maxsplit=1)
|
||||
key, value = (x.strip() for x in val.split(":", maxsplit=1))
|
||||
|
||||
if not (key and value):
|
||||
continue
|
||||
|
||||
key = nutrition_map.get(key.lower(), key)
|
||||
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
nutrition[key.strip()] = value.strip()
|
||||
nutrition[key] = value
|
||||
|
||||
return cleaner.clean_nutrition(nutrition) if nutrition else None
|
||||
|
||||
|
||||
@@ -37,6 +37,19 @@ def get_value_as_string_or_none(dictionary: dict, key: str):
|
||||
return None
|
||||
|
||||
|
||||
nutrition_map = {
|
||||
"Calories": "calories",
|
||||
"Fat": "fatContent",
|
||||
"Saturated Fat": "saturatedFatContent",
|
||||
"Cholesterol": "cholesterolContent",
|
||||
"Sodium": "sodiumContent",
|
||||
"Sugar": "sugarContent",
|
||||
"Carbohydrate": "carbohydrateContent",
|
||||
"Fiber": "fiberContent",
|
||||
"Protein": "proteinContent",
|
||||
}
|
||||
|
||||
|
||||
class PlanToEatMigrator(BaseMigrator):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@@ -63,16 +76,7 @@ class PlanToEatMigrator(BaseMigrator):
|
||||
|
||||
def _parse_recipe_nutrition_from_row(self, row: dict) -> dict:
|
||||
"""Parses the nutrition data from the row"""
|
||||
|
||||
nut_dict: dict = {}
|
||||
|
||||
nut_dict["calories"] = get_value_as_string_or_none(row, "Calories")
|
||||
nut_dict["fatContent"] = get_value_as_string_or_none(row, "Fat")
|
||||
nut_dict["proteinContent"] = get_value_as_string_or_none(row, "Protein")
|
||||
nut_dict["carbohydrateContent"] = get_value_as_string_or_none(row, "Carbohydrate")
|
||||
nut_dict["fiberContent"] = get_value_as_string_or_none(row, "Fiber")
|
||||
nut_dict["sodiumContent"] = get_value_as_string_or_none(row, "Sodium")
|
||||
nut_dict["sugarContent"] = get_value_as_string_or_none(row, "Sugar")
|
||||
nut_dict = {normalized_k: row[k] for k, normalized_k in nutrition_map.items() if k in row}
|
||||
|
||||
return cleaner.clean_nutrition(nut_dict)
|
||||
|
||||
|
||||
@@ -495,7 +495,7 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
|
||||
list of valid keys
|
||||
|
||||
Assumptionas:
|
||||
- All units are supplied in grams, expect sodium which maybe be in milligrams
|
||||
- All units are supplied in grams, expect sodium and cholesterol which maybe be in milligrams
|
||||
|
||||
Returns:
|
||||
dict[str, str]: If the argument is None, or not a dictionary, an empty dictionary is returned
|
||||
@@ -509,9 +509,10 @@ def clean_nutrition(nutrition: dict | None) -> dict[str, str]:
|
||||
if matched_digits := MATCH_DIGITS.search(val):
|
||||
output_nutrition[key] = matched_digits.group(0).replace(",", ".")
|
||||
|
||||
if sodium := nutrition.get("sodiumContent", None):
|
||||
if isinstance(sodium, str) and "m" not in sodium and "g" in sodium:
|
||||
with contextlib.suppress(AttributeError, TypeError):
|
||||
output_nutrition["sodiumContent"] = str(float(output_nutrition["sodiumContent"]) * 1000)
|
||||
for key in ["sodiumContent", "cholesterolContent"]:
|
||||
if val := nutrition.get(key, None):
|
||||
if isinstance(val, str) and "m" not in val and "g" in val:
|
||||
with contextlib.suppress(AttributeError, TypeError):
|
||||
output_nutrition[key] = str(float(output_nutrition[key]) * 1000)
|
||||
|
||||
return output_nutrition
|
||||
|
||||
Reference in New Issue
Block a user