Skip to content

Instantly share code, notes, and snippets.

@timothyandrew
Created August 28, 2018 06:25
Show Gist options
  • Save timothyandrew/b241a23e3139dcf49cd06f7c44d71397 to your computer and use it in GitHub Desktop.
Save timothyandrew/b241a23e3139dcf49cd06f7c44d71397 to your computer and use it in GitHub Desktop.
Export Pepperplate database to Paprika YAML
import sqlite3
import requests
import yaml
import base64
conn = sqlite3.connect('/path/to/recipes_2.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
def downloadAsBase64(url):
return base64.b64encode(requests.get(url).content)
def addTags(rows):
results = []
for row in rows:
recipeId = row['Id']
query = 'select Text from ItemTag inner join Tag on tag.id = ItemTag.TagId where RecipeId = ' + str(recipeId)
c.execute(query)
tags = [r['Text'] for r in c.fetchall()]
row['Tags'] = tags
results.append(row)
return results
def addIngredients(rows):
results = []
for row in rows:
recipeId = row['Id']
query = "select Quantity, Text from IngredientGroup ig inner join Ingredient i on i.GroupId = ig.id where RecipeId = " + str(recipeId) + " order by i.DisplayOrder asc;"
c.execute(query)
ingredients = [r['Quantity'] + ' ' + r['Text'] for r in c.fetchall()]
row['Ingredients'] = "\n".join(ingredients)
results.append(row)
return results
def addDirections(rows):
results = []
for row in rows:
recipeId = row['Id']
query = "select Text from DirectionGroup dg inner join Direction d on d.GroupId = dg.id where RecipeId = " + str(recipeId) + " order by d.DisplayOrder asc;"
c.execute(query)
directions = [r['Text'] for r in c.fetchall()]
row['Directions'] = "\n".join(directions)
results.append(row)
return results
def export(recipes):
results = []
for recipe in recipes:
result = {
'name': recipe['Title'],
'source_url': recipe['OriginalUrl'],
'categories': recipe['Tags'],
'ingredients': recipe['Ingredients'],
'directions': recipe['Directions'],
'source': recipe['Source'],
}
if recipe['ImageUrl']:
result['photo'] = downloadAsBase64(recipe['ImageUrl'])
results.append(result)
return results
c.execute('SELECT * FROM Recipe')
rows = [dict(row) for row in c.fetchall()]
recipes = addIngredients(addDirections(addTags(rows)))
print yaml.safe_dump(export(recipes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment