gpn23-recipes/report_allergene.py

94 lines
2.6 KiB
Python
Raw Normal View History

2025-06-10 12:34:14 +02:00
import json
import sys
from pathlib import Path
from datetime import timedelta
import re
import os
from tqdm import tqdm
from config import *
def allergens_for_step_ingredients(step):
l = []
for ingredient in step["ingredients"]:
if not ingredient.get("food"):
raise ValueError("No food in ingredient")
continue
i_name = ingredient["food"]["name"]
i_description = ingredient["food"]["description"]
if match := re.search(r"^Allergene: (.*)$", i_description, re.MULTILINE):
i_allergens = match.group(1)
else:
i_allergens = "❗ keine Allergene hinterlegt"
l.append((i_name, i_allergens))
return l
def allergens_for_recipe(recipe):
l = []
for step in recipe["steps"]:
l += allergens_for_step_ingredients(step)
return l
def link(recipe):
return f"{TANDOOR_URL + "/view/recipe/" + str(recipe["id"])}"
def wiki_link(recipe):
wiki_slug = recipe["name"]
wiki_slug = re.sub(" ", "_", wiki_slug)
return f"{WIKI_URL + WIKI_ARTICLE_PREFIX + wiki_slug}"
def decruft_ingredient(i_name: str):
i_name = i_name.replace(" (trocken)", "")
i_name = i_name.replace(" (frisch)", "")
i_name = i_name.replace(" (TK)", "")
i_name = i_name.replace(" (Dose)", "")
i_name = i_name.replace(" (getrocknet)", "")
return i_name
def main():
Path("report_allergene_korrektur.csv").unlink(missing_ok=True)
Path("report_allergene.md").unlink(missing_ok=True)
recipes = []
for json_file in os.listdir(OUTDIR_JSON):
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f:
data = json.load(f)
recipes.append(data)
for recipe in recipes:
with open("report_allergene_korrektur.csv", "a") as f:
f.write(recipe["name"] + "\n")
f.write(link(recipe) + "\n")
f.write(wiki_link(recipe) + "\n")
for i_name, i_allergens in allergens_for_recipe(recipe):
f.write(f"{i_name};{i_allergens}\n")
f.write("\n")
for recipe in recipes:
with open("report_allergene.md", "a") as f:
f.write(recipe["name"] + "\n\n")
f.write(wiki_link(recipe) + "\n\n")
zutaten = ""
for i_name, i_allergens in allergens_for_recipe(recipe):
if zutaten:
zutaten += ", "
zutaten += decruft_ingredient(i_name)
if i_allergens != "(keine)":
zutaten += f" (**{i_allergens}**)"
f.write(zutaten + "\n")
f.write("\n\n\n")
if __name__ == "__main__":
main()