File indexing completed on 2024-04-28 05:51:07

0001 #! /usr/bin/python3
0002 #
0003 # Copyright (c) 2019 Sune Vuorela <sune@vuorela.dk>
0004 #
0005 # Permission is hereby granted, free of charge, to any person
0006 # obtaining a copy of this software and associated documentation
0007 # files (the "Software"), to deal in the Software without
0008 # restriction, including without limitation the rights to use,
0009 # copy, modify, merge, publish, distribute, sublicense, and/or sell
0010 # copies of the Software, and to permit persons to whom the
0011 # Software is furnished to do so, subject to the following
0012 # conditions:
0013 #
0014 # The above copyright notice and this permission notice shall be
0015 # included in all copies or substantial portions of the Software.
0016 #
0017 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0018 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0019 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0020 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0021 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0022 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0023 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0024 # OTHER DEALINGS IN THE SOFTWARE.
0025 
0026 # Reads a krecipes database, krecipes.krecdb from current directory and outputs
0027 # the data in kookbook markdown ready format also in current directory
0028 
0029 import sqlite3
0030 import re
0031 import base64
0032 from collections import defaultdict
0033 
0034 connection = sqlite3.connect('krecipes.krecdb')
0035 
0036 cursor = connection.cursor()
0037 
0038 def filenamefromtitle(string):
0039     return re.sub("[- ()\"'/,]","",string.lower())
0040 
0041 taglist = defaultdict(list)
0042 for recipe_id, tagname in cursor.execute('select recipe_id, categories.name '
0043                                              'from category_list '
0044                                              'join categories on categories.id = category_id '
0045                                              'where category_id != -1 '
0046                                              'order by recipe_id;'
0047                                              ):
0048     taglist[recipe_id].append(tagname)
0049 
0050 authorlist = defaultdict(list)
0051 for recipe_id, author_name in cursor.execute('select recipe_id, authors.name '
0052                                                  'from author_list '
0053                                                  'join authors on authors.id = author_id;'
0054                                                  ):
0055     authorlist[recipe_id].append(author_name)
0056 
0057 ingredientlist = defaultdict(list)
0058 for recipe_id, amount, unit, name, additional in cursor.execute('select ingredient_list.recipe_id, ingredient_list.amount, units.plural, ingredients.name, prep_methods.name '
0059                                                                     'from ingredient_list '
0060                                                                     'join ingredients on ingredients.id = ingredient_list.ingredient_id '
0061                                                                     'join units on units.id = ingredient_list.unit_id '
0062                                                                     'left join prep_method_list on ingredient_list.id = prep_method_list.ingredient_list_id '
0063                                                                     'left join prep_methods on prep_method_list.prep_method_id = prep_methods.id '
0064                                                                     'order by recipe_id, ingredient_list.order_index;'
0065                                                                     ):
0066     ingredientlist[recipe_id].append((amount, unit, name, additional))
0067 
0068 for recipe_id, title, instructions, photo, yield_amount, yield_name, prep_time  in cursor.execute('select recipes.id, recipes.title, recipes.instructions, recipes.photo, recipes.yield_amount, yield_types.name, recipes.prep_time '
0069                                                                                                   'from recipes '
0070                                                                                                   'left join yield_types on recipes.yield_type_id = yield_types.id;'
0071                                                                                                   ):
0072     filename = filenamefromtitle(title)
0073     if photo:
0074         with open("{}.jpg".format(filename), "wb") as picture:
0075             picture.write(base64.b64decode(photo))
0076     with open(filename + ".recipe.md", "w", encoding="utf-8") as recipefile:
0077         recipefile.write("# {}\n\n".format(title))
0078         if yield_amount and yield_name:
0079             recipefile.write("Recipe yields {:,g} {} \n\n".format(yield_amount, yield_name))
0080         if prep_time and prep_time != "00:00:00":
0081             recipefile.write("Prep time is {} \n\n".format(prep_time))
0082         if photo:
0083             recipefile.write("![Image]({}.jpg)\n".format(filename))
0084         recipefile.write("### Ingredients\n")
0085         for amount, unit, name, additional  in ingredientlist[recipe_id]:
0086             recipefile.write(" * ")
0087             if amount != 0.0:
0088                 recipefile.write('{:,g}'.format(amount))
0089                 recipefile.write(" ")
0090                 if unit:
0091                     recipefile.write(unit)
0092                 else:
0093                     recipefile.write("pieces")
0094                 recipefile.write(" ")
0095             recipefile.write(name)
0096             if additional:
0097                 recipefile.write(", {}".format(additional))
0098             recipefile.write("\n")
0099         recipefile.write("\n")
0100         recipefile.write("### Directions\n\n")
0101         recipefile.write(instructions)
0102         recipefile.write("\n\n")
0103         recipefile.write("### Meta\n")
0104         for author in authorlist[recipe_id]:
0105             recipefile.write("author: {}\n".format(author))
0106         recipefile.write("\n")
0107         tags = taglist[recipe_id]
0108         if tags:
0109             recipefile.write("tags: {}\n".format(", ".join(tags)))
0110         recipefile.write("\n")