🎨 reformat using black
This commit is contained in:
parent
a1ab4c0b5a
commit
1994109507
5 changed files with 70 additions and 53 deletions
|
@ -67,12 +67,11 @@ def fetch_recipes(keyword_id):
|
||||||
|
|
||||||
return recipes
|
return recipes
|
||||||
|
|
||||||
|
|
||||||
def fetch_recipe(recipe_id):
|
def fetch_recipe(recipe_id):
|
||||||
endpoint = "/api/recipe"
|
endpoint = "/api/recipe"
|
||||||
|
|
||||||
response = requests.get(
|
response = requests.get(f"{TANDOOR_URL}/api/recipe/{recipe_id}", headers=headers)
|
||||||
f"{TANDOOR_URL}/api/recipe/{recipe_id}", headers=headers
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(f"Error: Received status code {response.status_code}")
|
print(f"Error: Received status code {response.status_code}")
|
||||||
|
@ -81,6 +80,7 @@ def fetch_recipe(recipe_id):
|
||||||
recipe = response.json()
|
recipe = response.json()
|
||||||
return recipe
|
return recipe
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
keyword_id = fetch_keyword_id(TANDOOR_KEYWORD)
|
keyword_id = fetch_keyword_id(TANDOOR_KEYWORD)
|
||||||
recipes = fetch_recipes(keyword_id)
|
recipes = fetch_recipes(keyword_id)
|
||||||
|
|
|
@ -4,37 +4,41 @@ import os
|
||||||
|
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
|
|
||||||
def normalize_ingredients(recipe):
|
def normalize_ingredients(recipe):
|
||||||
if 'recipeIngredient' in recipe:
|
if "recipeIngredient" in recipe:
|
||||||
recipe['recipeIngredient'] = [
|
recipe["recipeIngredient"] = [
|
||||||
ingredient.replace('g / Gramm', 'g') for ingredient in recipe['recipeIngredient']
|
ingredient.replace("g / Gramm", "g")
|
||||||
|
for ingredient in recipe["recipeIngredient"]
|
||||||
]
|
]
|
||||||
recipe['recipeIngredient'] = [
|
recipe["recipeIngredient"] = [
|
||||||
ingredient.replace('kg / Kilogramm', 'kg') for ingredient in recipe['recipeIngredient']
|
ingredient.replace("kg / Kilogramm", "kg")
|
||||||
|
for ingredient in recipe["recipeIngredient"]
|
||||||
]
|
]
|
||||||
recipe['recipeIngredient'] = [
|
recipe["recipeIngredient"] = [
|
||||||
ingredient.replace('.0 ', ' ') for ingredient in recipe['recipeIngredient']
|
ingredient.replace(".0 ", " ") for ingredient in recipe["recipeIngredient"]
|
||||||
]
|
]
|
||||||
recipe['recipeIngredient'] = [
|
recipe["recipeIngredient"] = [
|
||||||
re.sub("^0 (g|kg|Milliliter|None) ", "", ingredient) for ingredient in recipe['recipeIngredient']
|
re.sub("^0 (g|kg|Milliliter|None) ", "", ingredient)
|
||||||
|
for ingredient in recipe["recipeIngredient"]
|
||||||
]
|
]
|
||||||
recipe['recipeIngredient'] = [
|
recipe["recipeIngredient"] = [
|
||||||
ingredient \
|
ingredient
|
||||||
for ingredient in recipe['recipeIngredient'] \
|
for ingredient in recipe["recipeIngredient"]
|
||||||
if ingredient != "None"
|
if ingredient != "None"
|
||||||
]
|
]
|
||||||
return recipe
|
return recipe
|
||||||
|
|
||||||
|
|
||||||
def normalize_instructions(recipe):
|
def normalize_instructions(recipe):
|
||||||
if 'recipeInstructions' in recipe:
|
if "recipeInstructions" in recipe:
|
||||||
recipe['recipeInstructions'] = [
|
recipe["recipeInstructions"] = [
|
||||||
instruction \
|
instruction
|
||||||
for instruction in recipe['recipeInstructions'] \
|
for instruction in recipe["recipeInstructions"]
|
||||||
if instruction.get('text')
|
if instruction.get("text")
|
||||||
]
|
]
|
||||||
if not recipe['recipeInstructions']:
|
if not recipe["recipeInstructions"]:
|
||||||
del recipe['recipeInstructions']
|
del recipe["recipeInstructions"]
|
||||||
return recipe
|
return recipe
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,8 +54,8 @@ def normalize_recipe(recipe):
|
||||||
|
|
||||||
def check_recipe(recipe) -> list:
|
def check_recipe(recipe) -> list:
|
||||||
md = []
|
md = []
|
||||||
if 'steps' in recipe:
|
if "steps" in recipe:
|
||||||
steps = recipe['steps']
|
steps = recipe["steps"]
|
||||||
else:
|
else:
|
||||||
steps = []
|
steps = []
|
||||||
md.append("No steps?")
|
md.append("No steps?")
|
||||||
|
@ -59,15 +63,17 @@ def check_recipe(recipe) -> list:
|
||||||
md += check_ingredients(step)
|
md += check_ingredients(step)
|
||||||
return md
|
return md
|
||||||
|
|
||||||
|
|
||||||
def normalize_amount(a):
|
def normalize_amount(a):
|
||||||
if a == round(a):
|
if a == round(a):
|
||||||
return round(a)
|
return round(a)
|
||||||
else:
|
else:
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def check_ingredients(step):
|
def check_ingredients(step):
|
||||||
md = []
|
md = []
|
||||||
for ingredient in step['ingredients']:
|
for ingredient in step["ingredients"]:
|
||||||
i_amount = ingredient["amount"]
|
i_amount = ingredient["amount"]
|
||||||
i_amount = normalize_amount(i_amount)
|
i_amount = normalize_amount(i_amount)
|
||||||
|
|
||||||
|
@ -90,11 +96,12 @@ def check_ingredients(step):
|
||||||
def make_link(recipe):
|
def make_link(recipe):
|
||||||
return f"[{recipe["name"]}]({TANDOOR_URL + "/view/recipe/" + str(recipe["id"])})"
|
return f"[{recipe["name"]}]({TANDOOR_URL + "/view/recipe/" + str(recipe["id"])})"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
recipes = []
|
recipes = []
|
||||||
for json_file in os.listdir(OUTDIR_JSON):
|
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)
|
data = json.load(f)
|
||||||
recipes.append(data)
|
recipes.append(data)
|
||||||
|
|
||||||
|
@ -107,6 +114,6 @@ def main():
|
||||||
for line in md:
|
for line in md:
|
||||||
print(line)
|
print(line)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,10 @@ def normalize_amount(a):
|
||||||
else:
|
else:
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def md_for_step_ingredients(step):
|
def md_for_step_ingredients(step):
|
||||||
md = []
|
md = []
|
||||||
for ingredient in step['ingredients']:
|
for ingredient in step["ingredients"]:
|
||||||
i_amount = ingredient["amount"]
|
i_amount = ingredient["amount"]
|
||||||
i_amount = normalize_amount(i_amount)
|
i_amount = normalize_amount(i_amount)
|
||||||
|
|
||||||
|
@ -52,6 +53,7 @@ def md_for_step_ingredients(step):
|
||||||
md.append(f"- {i_name}")
|
md.append(f"- {i_name}")
|
||||||
return md
|
return md
|
||||||
|
|
||||||
|
|
||||||
def format_recipe_to_markdown(recipe):
|
def format_recipe_to_markdown(recipe):
|
||||||
md = []
|
md = []
|
||||||
|
|
||||||
|
@ -60,7 +62,7 @@ def format_recipe_to_markdown(recipe):
|
||||||
md.append(f"# {recipe.get('name', 'Untitled Recipe')}")
|
md.append(f"# {recipe.get('name', 'Untitled Recipe')}")
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
if valid_description(recipe.get('description')):
|
if valid_description(recipe.get("description")):
|
||||||
md.append(f"\n{recipe['description']}\n")
|
md.append(f"\n{recipe['description']}\n")
|
||||||
|
|
||||||
# Details
|
# Details
|
||||||
|
@ -71,7 +73,7 @@ def format_recipe_to_markdown(recipe):
|
||||||
details_parts.append(f"Prep time: {prep_time}")
|
details_parts.append(f"Prep time: {prep_time}")
|
||||||
if total_time:
|
if total_time:
|
||||||
details_parts.append(f"Total time: {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']}")
|
details_parts.append(f"Portionen: {recipe['servings']}")
|
||||||
# TODO servings_text
|
# TODO servings_text
|
||||||
if details_parts:
|
if details_parts:
|
||||||
|
@ -84,9 +86,9 @@ def format_recipe_to_markdown(recipe):
|
||||||
md += md_for_step_ingredients(step)
|
md += md_for_step_ingredients(step)
|
||||||
|
|
||||||
# Instructions
|
# Instructions
|
||||||
if 'steps' in recipe:
|
if "steps" in recipe:
|
||||||
md.append("\n## Zubereitung")
|
md.append("\n## Zubereitung")
|
||||||
steps = recipe['steps']
|
steps = recipe["steps"]
|
||||||
for i, step in enumerate(steps, 1):
|
for i, step in enumerate(steps, 1):
|
||||||
step_name = step["name"] or "<span></span>"
|
step_name = step["name"] or "<span></span>"
|
||||||
md.append(f"{i}. {step_name}")
|
md.append(f"{i}. {step_name}")
|
||||||
|
@ -100,13 +102,14 @@ def format_recipe_to_markdown(recipe):
|
||||||
# if key != "@type":
|
# if key != "@type":
|
||||||
# md.append(f"- **{key.replace('_', ' ').capitalize()}**: {value}")
|
# md.append(f"- **{key.replace('_', ' ').capitalize()}**: {value}")
|
||||||
|
|
||||||
return '\n'.join(md) + '\n'
|
return "\n".join(md) + "\n"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
recipes = []
|
recipes = []
|
||||||
for json_file in os.listdir(OUTDIR_JSON):
|
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)
|
data = json.load(f)
|
||||||
recipes.append(data)
|
recipes.append(data)
|
||||||
|
|
||||||
|
@ -118,6 +121,6 @@ def main():
|
||||||
with open(os.path.join(OUTDIR_MARKDOWN, markdown_fn), "w") as f:
|
with open(os.path.join(OUTDIR_MARKDOWN, markdown_fn), "w") as f:
|
||||||
f.write(markdown)
|
f.write(markdown)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,18 @@ for filename in tqdm(os.listdir(OUTDIR_MARKDOWN)):
|
||||||
|
|
||||||
# Run pandoc command
|
# Run pandoc command
|
||||||
try:
|
try:
|
||||||
subprocess.run([
|
subprocess.run(
|
||||||
|
[
|
||||||
"pandoc",
|
"pandoc",
|
||||||
input_path,
|
input_path,
|
||||||
"-f", "markdown",
|
"-f",
|
||||||
"-t", "mediawiki",
|
"markdown",
|
||||||
"-o", output_path
|
"-t",
|
||||||
], check=True)
|
"mediawiki",
|
||||||
|
"-o",
|
||||||
|
output_path,
|
||||||
|
],
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Error converting {filename}: {e}")
|
print(f"Error converting {filename}: {e}")
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,10 @@ from tqdm import tqdm
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
# Template strings
|
# Template strings
|
||||||
HIDDEN_COMMENT = '<!-- This page is auto-generated. Do not edit manually. -->'
|
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)
|
CATEGORY_TEMPLATE = "\n".join(
|
||||||
|
"[[Category:{0}]]".format(cat) for cat in WIKI_CATEGORY_LIST
|
||||||
|
)
|
||||||
|
|
||||||
# Connect to MediaWiki
|
# Connect to MediaWiki
|
||||||
site = mwclient.Site(WIKI_HOST, path=WIKI_PATH)
|
site = mwclient.Site(WIKI_HOST, path=WIKI_PATH)
|
||||||
|
@ -15,11 +17,11 @@ site.login(WIKI_USERNAME, WIKI_PASSWORD)
|
||||||
|
|
||||||
# Process and upload each .mediawiki file
|
# Process and upload each .mediawiki file
|
||||||
for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
|
for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
|
||||||
if filename.endswith('.mediawiki'):
|
if filename.endswith(".mediawiki"):
|
||||||
filepath = os.path.join(OUTDIR_MEDIAWIKI, filename)
|
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()
|
content = f.read()
|
||||||
|
|
||||||
# Compose final content
|
# Compose final content
|
||||||
|
@ -27,4 +29,4 @@ for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
|
||||||
|
|
||||||
# Post to MediaWiki
|
# Post to MediaWiki
|
||||||
page = site.pages[article_name]
|
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")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue