actually clean json again

This commit is contained in:
neingeist 2025-06-12 22:48:40 +02:00
parent 24fc3ed646
commit 0295a113c7
25 changed files with 99 additions and 208 deletions

View file

@ -5,53 +5,15 @@ import os
from config import *
def normalize_ingredients(recipe):
if "recipeIngredient" in recipe:
recipe["recipeIngredient"] = [
ingredient.replace("g / Gramm", "g")
for ingredient in recipe["recipeIngredient"]
]
recipe["recipeIngredient"] = [
ingredient.replace("kg / Kilogramm", "kg")
for ingredient in recipe["recipeIngredient"]
]
recipe["recipeIngredient"] = [
ingredient.replace(".0 ", " ") for ingredient in recipe["recipeIngredient"]
]
recipe["recipeIngredient"] = [
re.sub("^0 (g|kg|Milliliter|None) ", "", ingredient)
for ingredient in recipe["recipeIngredient"]
]
recipe["recipeIngredient"] = [
ingredient
for ingredient in recipe["recipeIngredient"]
if ingredient != "None"
]
def clean_recipe(recipe):
# remove useless ever-changing fields
for keyword in recipe["keywords"]:
if "updated_at" in keyword:
del keyword["updated_at"]
return recipe
def normalize_instructions(recipe):
if "recipeInstructions" in recipe:
recipe["recipeInstructions"] = [
instruction
for instruction in recipe["recipeInstructions"]
if instruction.get("text")
]
if not recipe["recipeInstructions"]:
del recipe["recipeInstructions"]
return recipe
def normalize_recipe(recipe):
cleaned_recipe = recipe
cleaned_recipe = normalize_ingredients(cleaned_recipe)
cleaned_recipe = normalize_instructions(cleaned_recipe)
return cleaned_recipe
# TODO: functions oben sind so nicht mehr funktional
def check_recipe(recipe) -> list:
md = []
if "steps" in recipe:
@ -99,6 +61,17 @@ def make_link(recipe):
def main():
# Clean
for json_file in os.listdir(OUTDIR_JSON):
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f:
recipe = json.load(f)
recipe = clean_recipe(recipe)
with open(os.path.join(OUTDIR_JSON, json_file), "w", encoding="utf-8") as f:
json.dump(recipe, f, sort_keys=True, indent=2)
# Read all and check
recipes = []
for json_file in os.listdir(OUTDIR_JSON):
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f: