Skip to content

Instantly share code, notes, and snippets.

@zeratax
Created November 26, 2022 06:28
Show Gist options
  • Save zeratax/284ce8c953611d6269e11f9fbfd12dba to your computer and use it in GitHub Desktop.
Save zeratax/284ce8c953611d6269e11f9fbfd12dba to your computer and use it in GitHub Desktop.
Rezeptbuch to Pestle converter
import sqlite3
import os
import sys
import logging
import json
import math
import pprint
def write_image(image, image_path):
if not image:
logging.debug("no image")
return
logging.debug(f"writing image '{image_path}")
if not os.path.isfile(image_path):
if not os.path.exists("images"):
os.makedirs("images")
with open(image_path, "wb") as file:
file.write(image)
else:
logging.debug("image file already exists")
def convert_db(DB_PATH):
if not os.path.isfile(DB_PATH):
logging.error("no database found!")
sys.exit()
connection = sqlite3.connect(DB_PATH)
cursor = connection.cursor()
pestle_recipes = []
for (
id,
name,
description,
image,
rating,
time,
category_id,
servings,
) in cursor.execute(
"SELECT _id, name, beschreibung, pic, bewertung, zeit_kochen, kategorie_id, zutaten_fuer_x \
FROM rezept"
).fetchall():
logging.debug(f"working on recipe {id:03}")
image_path = f"images/{name}.png"
write_image(image, image_path)
ingredients = []
for ing_name, unit, quantity in cursor.execute(
"SELECT name, einheit, menge \
FROM rezept_pos WHERE rezept_id == ?",
(id,),
):
ingredients.append({
"unit": unit or None,
"quantity": quantity,
"unitRange": None,
"quantityRange": None,
"text": f"{ing_name} {quantity} {unit}" if unit else f"{quantity} {ing_name}",
"name": ing_name,
})
lines = description.split("\n")
steps = []
for line in lines:
steps.append({
"url": None,
"itemListElement": [],
"processed": False,
"name": None,
"@type": 1,
"text": line,
})
hours = math.floor(time / 60)
minutes = time % 60
pestle = {
"review": [],
"public": None,
"source": None,
"image": None,
"aggregateRating": {"ratingCount": 1, "ratingValue": rating}
if rating
else None,
"prepTime": None,
"@type": "Recipe",
"recipeCuisine": [],
"prepTime": None,
"cookTime": None,
"totalTime": f"PT{hours}H{minutes}M",
"video": None,
"keywords": [],
"imageUploaded": True if image else False,
"name": name,
"recipeYield": f"{servings}",
"note": None,
"nutrition": None,
"recipeCategory": [],
"userCreated": True,
"datePublished": None,
"author": {"@type": 0},
"description": None,
"recipeIngredient": ingredients,
"recipeInstructions": [
{
"url": None,
"itemListElement": steps,
"processed": True,
"name": "Steps",
"@type": 0,
"text": None,
}
],
}
pestle_recipes.append(pestle)
pprint.pprint(pestle)
with open("rezepte.pestle", "w") as outfile:
json.dump(pestle_recipes, outfile)
cursor.close()
connection.close()
if __name__ == "__main__":
DB_PATH = "Rezeptbuch/data.db"
logging.basicConfig(level=logging.DEBUG)
convert_db(DB_PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment