gpn24-recipes/report_allergene.py

92 lines
2.5 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 *
2026-04-12 23:51:21 +02:00
from lib import *
2025-06-10 12:34:14 +02:00
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):
2026-04-12 23:52:10 +02:00
return f"{TANDOOR_URL + '/view/recipe/' + str(recipe['id'])}"
2025-06-10 12:34:14 +02:00
2026-04-08 23:51:21 +02:00
2025-06-10 12:34:14 +02:00
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
2026-04-08 23:51:21 +02:00
2025-06-10 12:34:14 +02:00
def main():
Path("report_allergene_korrektur.csv").unlink(missing_ok=True)
Path("report_allergene.md").unlink(missing_ok=True)
2026-04-12 23:51:21 +02:00
recipes = read_recipes()
2025-06-10 12:34:14 +02:00
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")
2026-04-08 23:51:21 +02:00
2025-06-10 12:34:14 +02:00
if __name__ == "__main__":
main()