Skip to content

Commit

Permalink
Add support for recipes disabled ingredient amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
felixschndr committed May 20, 2024
1 parent b2ab6a7 commit b26d84c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | - | [email protected] |
| `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 | - | `[email protected]` |
| `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 in the recipe settings! ![recipe settings](./assets/images/recipe_settings.png) | 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

Expand All @@ -61,9 +61,9 @@ you can ignore some environment variables (e.g. `HTTP_HOST` and `HTTP_PORT`).
- Example:
```bash
docker run
-e BRING_USERNAME="<your email>"
-e BRING_PASSWORD="<your password>"
-e BRING_LIST_NAME="<your list name>"
-e BRING_USERNAME="[email protected]"
-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
```
Expand Down
18 changes: 12 additions & 6 deletions source/bring_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,31 @@ 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

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)

Expand Down
37 changes: 26 additions & 11 deletions source/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
9 changes: 7 additions & 2 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b26d84c

Please sign in to comment.