🎨 reformat using black

This commit is contained in:
neingeist 2025-06-08 21:17:47 +02:00
parent a1ab4c0b5a
commit 1994109507
5 changed files with 70 additions and 53 deletions

View file

@ -67,12 +67,11 @@ def fetch_recipes(keyword_id):
return recipes
def fetch_recipe(recipe_id):
endpoint = "/api/recipe"
response = requests.get(
f"{TANDOOR_URL}/api/recipe/{recipe_id}", headers=headers
)
response = requests.get(f"{TANDOOR_URL}/api/recipe/{recipe_id}", headers=headers)
if response.status_code != 200:
print(f"Error: Received status code {response.status_code}")
@ -81,6 +80,7 @@ def fetch_recipe(recipe_id):
recipe = response.json()
return recipe
def main():
keyword_id = fetch_keyword_id(TANDOOR_KEYWORD)
recipes = fetch_recipes(keyword_id)

View file

@ -4,37 +4,41 @@ 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']
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("kg / Kilogramm", "kg")
for ingredient in recipe["recipeIngredient"]
]
recipe['recipeIngredient'] = [
ingredient.replace('.0 ', ' ') 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"] = [
re.sub("^0 (g|kg|Milliliter|None) ", "", ingredient)
for ingredient in recipe["recipeIngredient"]
]
recipe['recipeIngredient'] = [
ingredient \
for ingredient in recipe['recipeIngredient'] \
recipe["recipeIngredient"] = [
ingredient
for ingredient in recipe["recipeIngredient"]
if ingredient != "None"
]
return recipe
def normalize_instructions(recipe):
if 'recipeInstructions' in recipe:
recipe['recipeInstructions'] = [
instruction \
for instruction in recipe['recipeInstructions'] \
if instruction.get('text')
if "recipeInstructions" in recipe:
recipe["recipeInstructions"] = [
instruction
for instruction in recipe["recipeInstructions"]
if instruction.get("text")
]
if not recipe['recipeInstructions']:
del recipe['recipeInstructions']
if not recipe["recipeInstructions"]:
del recipe["recipeInstructions"]
return recipe
@ -50,8 +54,8 @@ def normalize_recipe(recipe):
def check_recipe(recipe) -> list:
md = []
if 'steps' in recipe:
steps = recipe['steps']
if "steps" in recipe:
steps = recipe["steps"]
else:
steps = []
md.append("No steps?")
@ -59,15 +63,17 @@ def check_recipe(recipe) -> list:
md += check_ingredients(step)
return md
def normalize_amount(a):
if a == round(a):
return round(a)
else:
return a
def check_ingredients(step):
md = []
for ingredient in step['ingredients']:
for ingredient in step["ingredients"]:
i_amount = ingredient["amount"]
i_amount = normalize_amount(i_amount)
@ -90,11 +96,12 @@ def check_ingredients(step):
def make_link(recipe):
return f"[{recipe["name"]}]({TANDOOR_URL + "/view/recipe/" + str(recipe["id"])})"
def main():
recipes = []
for json_file in os.listdir(OUTDIR_JSON):
with open(os.path.join(OUTDIR_JSON, json_file), 'r', encoding='utf-8') as f:
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f:
data = json.load(f)
recipes.append(data)
@ -107,6 +114,6 @@ def main():
for line in md:
print(line)
if __name__ == "__main__":
main()

View file

@ -25,9 +25,10 @@ def normalize_amount(a):
else:
return a
def md_for_step_ingredients(step):
md = []
for ingredient in step['ingredients']:
for ingredient in step["ingredients"]:
i_amount = ingredient["amount"]
i_amount = normalize_amount(i_amount)
@ -52,6 +53,7 @@ def md_for_step_ingredients(step):
md.append(f"- {i_name}")
return md
def format_recipe_to_markdown(recipe):
md = []
@ -60,7 +62,7 @@ def format_recipe_to_markdown(recipe):
md.append(f"# {recipe.get('name', 'Untitled Recipe')}")
# Description
if valid_description(recipe.get('description')):
if valid_description(recipe.get("description")):
md.append(f"\n{recipe['description']}\n")
# Details
@ -71,7 +73,7 @@ def format_recipe_to_markdown(recipe):
details_parts.append(f"Prep time: {prep_time}")
if total_time:
details_parts.append(f"Total time: {total_time}")
if 'servings' in recipe:
if "servings" in recipe:
details_parts.append(f"Portionen: {recipe['servings']}")
# TODO servings_text
if details_parts:
@ -84,9 +86,9 @@ def format_recipe_to_markdown(recipe):
md += md_for_step_ingredients(step)
# Instructions
if 'steps' in recipe:
if "steps" in recipe:
md.append("\n## Zubereitung")
steps = recipe['steps']
steps = recipe["steps"]
for i, step in enumerate(steps, 1):
step_name = step["name"] or "<span></span>"
md.append(f"{i}. {step_name}")
@ -100,13 +102,14 @@ def format_recipe_to_markdown(recipe):
# if key != "@type":
# md.append(f"- **{key.replace('_', ' ').capitalize()}**: {value}")
return '\n'.join(md) + '\n'
return "\n".join(md) + "\n"
def main():
recipes = []
for json_file in os.listdir(OUTDIR_JSON):
with open(os.path.join(OUTDIR_JSON, json_file), 'r', encoding='utf-8') as f:
with open(os.path.join(OUTDIR_JSON, json_file), "r", encoding="utf-8") as f:
data = json.load(f)
recipes.append(data)
@ -118,6 +121,6 @@ def main():
with open(os.path.join(OUTDIR_MARKDOWN, markdown_fn), "w") as f:
f.write(markdown)
if __name__ == "__main__":
main()

View file

@ -15,13 +15,18 @@ for filename in tqdm(os.listdir(OUTDIR_MARKDOWN)):
# Run pandoc command
try:
subprocess.run([
subprocess.run(
[
"pandoc",
input_path,
"-f", "markdown",
"-t", "mediawiki",
"-o", output_path
], check=True)
"-f",
"markdown",
"-t",
"mediawiki",
"-o",
output_path,
],
check=True,
)
except subprocess.CalledProcessError as e:
print(f"Error converting {filename}: {e}")

View file

@ -6,8 +6,10 @@ from tqdm import tqdm
from config import *
# Template strings
HIDDEN_COMMENT = '<!-- This page is auto-generated. Do not edit manually. -->'
CATEGORY_TEMPLATE = '\n'.join('[[Category:{0}]]'.format(cat) for cat in WIKI_CATEGORY_LIST)
HIDDEN_COMMENT = "<!-- This page is auto-generated. Do not edit manually. -->"
CATEGORY_TEMPLATE = "\n".join(
"[[Category:{0}]]".format(cat) for cat in WIKI_CATEGORY_LIST
)
# Connect to MediaWiki
site = mwclient.Site(WIKI_HOST, path=WIKI_PATH)
@ -15,11 +17,11 @@ site.login(WIKI_USERNAME, WIKI_PASSWORD)
# Process and upload each .mediawiki file
for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
if filename.endswith('.mediawiki'):
if filename.endswith(".mediawiki"):
filepath = os.path.join(OUTDIR_MEDIAWIKI, filename)
article_name = WIKI_ARTICLE_PREFIX + filename.replace('.mediawiki', '')
article_name = WIKI_ARTICLE_PREFIX + filename.replace(".mediawiki", "")
with open(filepath, 'r', encoding='utf-8') as f:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
# Compose final content
@ -27,4 +29,4 @@ for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
# Post to MediaWiki
page = site.pages[article_name]
page.save(final_content, summary='Automated upload of recipe page')
page.save(final_content, summary="Automated upload of recipe page")