2025-06-01 23:33:03 +02:00
import json
import sys
from pathlib import Path
from datetime import timedelta
import re
from pathvalidate import validate_filename
import os
2025-06-02 01:38:40 +02:00
from tqdm import tqdm
2025-06-01 23:33:03 +02:00
2025-06-02 21:28:28 +02:00
from config import *
2025-06-02 01:37:39 +02:00
include_title = False
2025-06-02 00:45:10 +02:00
2025-06-02 01:38:40 +02:00
2025-06-02 01:38:04 +02:00
def valid_description ( description ) :
if not description :
return False
return True
2025-06-06 22:18:53 +02:00
def normalize_amount ( a ) :
if a == round ( a ) :
return round ( a )
else :
return a
2025-06-08 21:17:47 +02:00
2025-06-06 22:18:53 +02:00
def md_for_step_ingredients ( step ) :
md = [ ]
2025-06-08 21:17:47 +02:00
for ingredient in step [ " ingredients " ] :
2025-06-06 22:18:53 +02:00
i_amount = ingredient [ " amount " ]
i_amount = normalize_amount ( i_amount )
if not ingredient . get ( " unit " ) :
i_unit = " "
else :
i_unit = ingredient [ " unit " ] [ " name " ]
# TODO → 1_clean_json.py
if i_unit == " g / Gramm " :
i_unit = " g "
elif i_unit == " kg / Kilogramm " :
i_unit = " kg "
if not ingredient . get ( " food " ) :
# TODO → 1_clean_json.py
continue
i_name = ingredient [ " food " ] [ " name " ]
if i_amount :
md . append ( f " - { i_amount } { i_unit } { i_name } " )
else :
md . append ( f " - { i_name } " )
return md
2025-06-01 23:33:03 +02:00
2025-06-08 21:17:47 +02:00
2025-06-01 23:33:03 +02:00
def format_recipe_to_markdown ( recipe ) :
md = [ ]
2025-06-02 01:37:39 +02:00
# Title (may be implicit by filename)
if include_title :
md . append ( f " # { recipe . get ( ' name ' , ' Untitled Recipe ' ) } " )
2025-06-01 23:33:03 +02:00
# Description
2025-06-08 21:17:47 +02:00
if valid_description ( recipe . get ( " description " ) ) :
2025-06-02 21:17:25 +02:00
md . append ( f " \n { recipe [ ' description ' ] } \n " )
2025-06-01 23:33:03 +02:00
2025-06-02 00:45:10 +02:00
# Details
details_parts = [ ]
2025-06-06 22:18:53 +02:00
prep_time = recipe . get ( " working_time " ) or 0
total_time = ( recipe . get ( " working_time " ) or 0 ) + ( recipe . get ( " waiting_time " ) or 0 )
if prep_time :
2025-06-08 21:17:47 +02:00
details_parts . append ( f " Prep time: { prep_time } " )
2025-06-06 22:18:53 +02:00
if total_time :
2025-06-08 21:17:47 +02:00
details_parts . append ( f " Total time: { total_time } " )
if " servings " in recipe :
2025-06-06 22:18:53 +02:00
details_parts . append ( f " Portionen: { recipe [ ' servings ' ] } " )
# TODO servings_text
2025-06-02 00:45:10 +02:00
if details_parts :
for p in details_parts :
md . append ( f " * { p } \n " )
2025-06-01 23:33:03 +02:00
# Ingredients
2025-06-12 22:12:07 +02:00
if recipe [ " show_ingredient_overview " ] :
md . append ( " \n ## Zutaten (gesamt) " )
for step in recipe [ " steps " ] :
md + = md_for_step_ingredients ( step )
2025-06-01 23:33:03 +02:00
# Instructions
2025-06-08 21:17:47 +02:00
if " steps " in recipe :
2025-06-02 00:45:10 +02:00
md . append ( " \n ## Zubereitung " )
2025-06-08 21:17:47 +02:00
steps = recipe [ " steps " ]
2025-06-06 22:18:53 +02:00
for i , step in enumerate ( steps , 1 ) :
step_name = step [ " name " ] or " <span></span> "
md . append ( f " { i } . { step_name } " )
md + = md_for_step_ingredients ( step )
md . append ( f " { step [ " instructions_markdown " ] } " )
# Nutrition TODO
# if 'nutrition' in recipe and recipe['nutrition']:
# md.append("\n## Nutrition")
# for key, value in recipe['nutrition'].items():
# if key != "@type":
# md.append(f"- **{key.replace('_', ' ').capitalize()}**: {value}")
2025-06-01 23:33:03 +02:00
2025-06-08 21:17:47 +02:00
return " \n " . join ( md ) + " \n "
2025-06-01 23:33:03 +02:00
2025-06-06 22:18:53 +02:00
def main ( ) :
recipes = [ ]
for json_file in os . listdir ( OUTDIR_JSON ) :
2025-06-08 21:17:47 +02:00
with open ( os . path . join ( OUTDIR_JSON , json_file ) , " r " , encoding = " utf-8 " ) as f :
2025-06-06 22:18:53 +02:00
data = json . load ( f )
recipes . append ( data )
2025-06-01 23:33:03 +02:00
2025-06-02 01:38:40 +02:00
for recipe in tqdm ( recipes ) :
2025-06-01 23:33:03 +02:00
markdown_fn = f " { recipe . get ( ' name ' , ' Untitled Recipe ' ) } .md "
2025-06-08 21:17:47 +02:00
validate_filename ( markdown_fn ) # XXX does this check directory traversal?
2025-06-01 23:33:03 +02:00
markdown = format_recipe_to_markdown ( recipe )
2025-06-02 21:28:28 +02:00
os . makedirs ( OUTDIR_MARKDOWN , exist_ok = True )
with open ( os . path . join ( OUTDIR_MARKDOWN , markdown_fn ) , " w " ) as f :
2025-06-01 23:33:03 +02:00
f . write ( markdown )
2025-06-08 21:17:47 +02:00
2025-06-01 23:33:03 +02:00
if __name__ == " __main__ " :
2025-06-06 22:18:53 +02:00
main ( )