gpn24-recipes/fruehstueck_bestellung+inventar.py

138 lines
3.9 KiB
Python

import csv
import json
import os
import pprint
import re
import sys
from collections import defaultdict
from datetime import timedelta
from pathlib import Path
from openpyxl import load_workbook
from tabulate import tabulate
from tqdm import tqdm
from config import *
from lib import read_recipes, markdown_link, recipe_url, markdown_alert
def read_plan():
wb = load_workbook("data/Planung + Einkauf Brotaufstriche.xlsx")
sheet = wb.active
plan = {}
for row in sheet.iter_rows(values_only=True):
r_name = row[0]
# Ansprechpartner*in = row[1]
r_total_net_weight = row[2]
# gpn23 = row[3]
r_serving_net_weight = row[4]
# (Formula) = row[5]
r_recipe_url = row[6]
# We simply skip over any rows that do not have the recipe url
if not r_recipe_url:
continue
if m := re.match(r"https://.*/(\d+)$", r_recipe_url):
r_recipe_id = int(m.group(1))
else:
continue # skip
r_wanted_servings = r_total_net_weight / r_serving_net_weight
plan[r_name] = {
"total_net_weight": r_total_net_weight,
"serving_net_weight": r_serving_net_weight,
"recipe_id": r_recipe_id,
"wanted_servings": r_wanted_servings,
}
return plan
plan = read_plan()
# XXX move to lib
def grams(ingredient):
conversion = None
for c in ingredient["conversions"]:
if c["unit"] in ["g / Gramm", "g"]:
conversion = c
if conversion:
return conversion.get("amount")
def main():
recipes = read_recipes(keyword="Frühstück")
inventory = {}
for recipe in recipes:
scale_factor = plan[recipe["name"]]["wanted_servings"] / recipe["servings"]
r_name = recipe["name"]
for step in recipe["steps"]:
for ingredient in step["ingredients"]:
if not ingredient.get("food"):
raise ValueError("No food in ingredient")
continue
i_grams = scale_factor * (grams(ingredient) or 0)
i_name = ingredient["food"]["name"]
i_description = ingredient["food"][
"description"
] # XXX probably not what i wanted
inventory.setdefault(i_name, []).append((i_grams, r_name))
# gross ingredient weight per serving
r_gross_weight = 0.0
for step in recipe["steps"]:
for ingredient in step["ingredients"]:
if not ingredient.get("food"):
raise ValueError("No food in ingredient")
continue
r_gross_weight += (grams(ingredient) or 0)
r_gross_weight /= recipe["servings"]
recipe["total_net_weight"] = plan[r_name]["total_net_weight"]
recipe["wanted_servings"] = plan[r_name]["wanted_servings"]
recipe["total_gross_weight"] = r_gross_weight * plan[r_name]["wanted_servings"]
print(markdown_alert("automatisch erstellt, nicht editieren..."))
print()
print("## Rezepte und Portionen")
data = []
for recipe in recipes:
row = [markdown_link(recipe["name"], recipe_url(recipe["id"])),
int(recipe["wanted_servings"]),
int(recipe["total_net_weight"]),
int(recipe["total_gross_weight"])]
data.append(row)
headers = ["Rezept", "Portionen", "Gewicht Netto (Soll)", "Gewicht Brutto Zutaten"]
print(tabulate(data, headers=headers, tablefmt="github"))
print()
print()
print("## Zutaten")
for ingredient, entries in inventory.items():
total = sum(amount for amount, _ in entries)
print(f"### {ingredient}")
print(f"- **{total:.1f}g** **Total**")
for i_grams, r_name in entries:
print(f"- {i_grams:.1f}g {r_name}")
if __name__ == "__main__":
main()