diff --git a/README.md b/README.md index 2effcd7..e94975d 100644 --- a/README.md +++ b/README.md @@ -32,15 +32,15 @@ Mealie instance and adds the ingredients of a recipe to a specified Bring shoppi No matter which deployment option you chose you must setup some environment variables: -| Variable name | Description | Required | Default | Example | -|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------|:--------:|-----------------------------------|------------------------------| -| `BRING_USERNAME` | The email address of your bring account | Yes | - | myuser@myemailprovider.com | -| `BRING_PASSWORD` | The password of your bring account | Yes | - | my super secret password | -| `BRING_LIST_NAME` | The exact name of the list you want to add the ingredients to, supports special characters | Yes | - | My shopping list with spaces | -| `IGNORED_INGREDIENTS` | Ingredients that are never added to the shopping list (things you always have at home), separated by a `,`, case insensitive | No | - (all ingredients will be added) | Salt,Pepper,Frying oil | -| `LOG_LEVEL` | The loglevel the application logs at | No | `INFO` | `DEBUG` | -| `HTTP_HOST` | The address the application tries to attach to, leave this empty to listen on all interfaces, leave this empty if you are using Docker | No | `0.0.0.0` | `192.168.1.5` | -| `HTTP_PORT` | The port the application listens on, change this if needed if you run the application locally, leave this empty if you are using Docker | No | `8742` | `1234` | +| Variable name | Description | Required | Default | Example | +|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:|-----------------------------------|--------------------------------| +| `BRING_USERNAME` | The email address of your bring account | Yes | - | `myuser@myemailprovider.com` | +| `BRING_PASSWORD` | The password of your bring account | Yes | - | `my super secret password` | +| `BRING_LIST_NAME` | The exact name of the list you want to add the ingredients to, supports special characters | Yes | - | `My shopping list with spaces` | +| `IGNORED_INGREDIENTS` | Ingredients that are never added to the shopping list (things you always have at home), separated by a `,`, case insensitive, **WARNING**: This *only* works for recipes with *enabled* ingredient amounts! | No | - (all ingredients will be added) | `Salt,Pepper,Frying oil` | +| `LOG_LEVEL` | The loglevel the application logs at | No | `INFO` | `DEBUG` | +| `HTTP_HOST` | The address the application tries to attach to, leave this empty to listen on all interfaces, leave this empty if you are using Docker | No | `0.0.0.0` | `192.168.1.5` | +| `HTTP_PORT` | The port the application listens on, change this if needed if you run the application locally, leave this empty if you are using Docker | No | `8742` | `1234` | ### Deployment options @@ -61,9 +61,9 @@ you can ignore some environment variables (e.g. `HTTP_HOST` and `HTTP_PORT`). - Example: ```bash docker run - -e BRING_USERNAME="" - -e BRING_PASSWORD="" - -e BRING_LIST_NAME="" + -e BRING_USERNAME="myuser@myemailprovider.com" + -e BRING_PASSWORD="my super secret password" + -e BRING_LIST_NAME="My shopping list with spaces" -p 1234:8742 ghcr.io/felixschndr/mealie-bring-api:latest ``` diff --git a/source/bring_handler.py b/source/bring_handler.py index 497cc3d..903baa8 100644 --- a/source/bring_handler.py +++ b/source/bring_handler.py @@ -66,17 +66,21 @@ def determine_list_uuid(self) -> str: return bring_list_uuid - def parse_ignored_ingredients(self) -> list[str]: - ignored_ingredients = [] + def parse_ignored_ingredients(self) -> dict[str, str | list[str]]: + ignored_ingredients = {"as list": [], "as string": ""} if not self.ignored_ingredients_input: return ignored_ingredients - for ingredient in self.ignored_ingredients_input.replace(", ", ",").split(","): - ignored_ingredients.append(ingredient.lower()) + ignored_ingredients_input = self.ignored_ingredients_input.lower() + + for ingredient in ignored_ingredients_input.replace(", ", ",").split(","): + ignored_ingredients["as list"].append(ingredient) + + ignored_ingredients["as string"] = ignored_ingredients_input if ignored_ingredients: - self.log.info(f"Ignoring ingredients {ignored_ingredients}") + self.log.info(f"Ignoring ingredients {ignored_ingredients['as list']}") return ignored_ingredients @@ -84,7 +88,9 @@ def add_item_to_list(self, ingredient: Ingredient) -> None: self.log.debug(f"Adding ingredient to Bring: {ingredient}") if ingredient.specification: - self.bring.saveItem(self.list_uuid, ingredient.food, ingredient.specification) + self.bring.saveItem( + self.list_uuid, ingredient.food, ingredient.specification + ) else: self.bring.saveItem(self.list_uuid, ingredient.food) diff --git a/source/ingredient.py b/source/ingredient.py index de50106..1cd2fa1 100644 --- a/source/ingredient.py +++ b/source/ingredient.py @@ -2,35 +2,50 @@ class Ingredient: - def __init__(self, ingredient_input: dict, ignored_ingredients: list[str]): + def __init__( + self, ingredient_input: dict, ignored_ingredients: dict[str, str | list[str]] + ): + self.ingredient_input = ingredient_input + self.ignored_ingredients = ignored_ingredients + self.food = None self.specification = "" - self.parse_input(ingredient_input, ignored_ingredients) + self.parse_input() def __repr__(self): if self.specification: return f"{self.food} ({self.specification})" return self.food - def parse_input(self,ingredient_input: dict, ignored_ingredients: list[str]) -> None: - food = ingredient_input.get("food", None) - if not food: - raise ValueError("This recipe has a ingredient with no name, it will be ignored!") + def parse_input(self) -> None: + try: + _ = self.ingredient_input["food"] + except KeyError: + raise ValueError("There is an ingredient with no name, it will be ignored!") + if self.ingredient_input["disableAmount"]: + self._parse_input_with_no_ingredient_amounts() + else: + self._parse_input_with_ingredient_amounts() + + def _parse_input_with_no_ingredient_amounts(self) -> None: + self.food = self.ingredient_input["display"] + + def _parse_input_with_ingredient_amounts(self) -> None: - food_name = food["name"] - if food_name.lower() in ignored_ingredients: + food_name = self.ingredient_input["food"]["name"] + if food_name.lower() in self.ignored_ingredients["as list"]: raise IgnoredIngredient(f"Found ignored ingredient {food_name}") self.food = food_name - quantity = ingredient_input.get("quantity", None) + quantity = self.ingredient_input.get("quantity", None) if quantity: self.specification += str(quantity) - unit = ingredient_input.get("unit", None) + unit = self.ingredient_input.get("unit", None) if unit: self.specification += unit.get("abbreviation", unit["name"]) - note = ingredient_input.get("note", None) + note = self.ingredient_input.get("note", None) if note: self.specification += f" ({note})" diff --git a/source/main.py b/source/main.py index ce01247..dfe4d18 100644 --- a/source/main.py +++ b/source/main.py @@ -20,13 +20,18 @@ def webhook_handler(): data = request.get_json(force=True) recipe_name = data["name"] - logger.log.info(f"Received recipe {recipe_name} from {request.origin}") + logger.log.info(f'Received recipe "{recipe_name}" from "{request.origin}"') + + if data["settings"]["disableAmount"]: + logger.log.warning( + "This recipe has its ingredient amount this disabled. Its ingredients will not be checked whether they are supposed to be ignored." + ) for ingredient in data["recipeIngredient"]: try: ingredient = Ingredient(ingredient, bring_handler.ignored_ingredients) except ValueError as e: - logging.error(e) + logging.warning(e) continue except IgnoredIngredient as e: logging.debug(e)