diff --git a/0_get_recipes.py b/0_get_recipes.py index 2bfadd8..ca1f913 100644 --- a/0_get_recipes.py +++ b/0_get_recipes.py @@ -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) diff --git a/1_clean_json.py b/1_clean_json.py index bc7e550..bc49e2c 100644 --- a/1_clean_json.py +++ b/1_clean_json.py @@ -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'] \ - if ingredient != "None" + 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() - diff --git a/2_to_markdown.py b/2_to_markdown.py index c37b49f..c1bc094 100644 --- a/2_to_markdown.py +++ b/2_to_markdown.py @@ -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 @@ -68,10 +70,10 @@ def format_recipe_to_markdown(recipe): 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: - details_parts.append(f"Prep time: {prep_time}") + details_parts.append(f"Prep time: {prep_time}") if total_time: - details_parts.append(f"Total time: {total_time}") - if 'servings' in recipe: + details_parts.append(f"Total time: {total_time}") + 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 "" md.append(f"{i}. {step_name}") @@ -100,24 +102,25 @@ 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) for recipe in tqdm(recipes): markdown_fn = f"{recipe.get('name', 'Untitled Recipe')}.md" - validate_filename(markdown_fn) # XXX does this check directory traversal? + validate_filename(markdown_fn) # XXX does this check directory traversal? markdown = format_recipe_to_markdown(recipe) os.makedirs(OUTDIR_MARKDOWN, exist_ok=True) with open(os.path.join(OUTDIR_MARKDOWN, markdown_fn), "w") as f: f.write(markdown) + if __name__ == "__main__": main() - diff --git a/3_to_mediawiki.py b/3_to_mediawiki.py index d8b4c30..1dd5388 100644 --- a/3_to_mediawiki.py +++ b/3_to_mediawiki.py @@ -15,13 +15,18 @@ for filename in tqdm(os.listdir(OUTDIR_MARKDOWN)): # Run pandoc command try: - subprocess.run([ - "pandoc", - input_path, - "-f", "markdown", - "-t", "mediawiki", - "-o", output_path - ], check=True) + subprocess.run( + [ + "pandoc", + input_path, + "-f", + "markdown", + "-t", + "mediawiki", + "-o", + output_path, + ], + check=True, + ) except subprocess.CalledProcessError as e: print(f"Error converting {filename}: {e}") - diff --git a/4_post_to_wiki.py b/4_post_to_wiki.py index 4cad733..711ffa7 100644 --- a/4_post_to_wiki.py +++ b/4_post_to_wiki.py @@ -6,8 +6,10 @@ from tqdm import tqdm from config import * # Template strings -HIDDEN_COMMENT = '' -CATEGORY_TEMPLATE = '\n'.join('[[Category:{0}]]'.format(cat) for cat in WIKI_CATEGORY_LIST) +HIDDEN_COMMENT = "" +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")