-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroccolify.py
More file actions
65 lines (48 loc) · 2.48 KB
/
broccolify.py
File metadata and controls
65 lines (48 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import os
import string
import shutil
with open('recipes.json', mode='r', encoding='utf-8') as file:
recipes = json.load(file)
categories = []
if not os.path.exists('broccoli'): #makes dir for archive if does not already exist
os.mkdir('broccoli')
for recipe in recipes:
title = recipes[recipe]['title'].replace('&','&')
fileName = recipes[recipe]['title'].replace(' & ',' ')
fileName = fileName.translate(str.maketrans('', '', string.punctuation))
fileName = fileName.replace(' ','_')
directions = '\n'.join(map(str,recipes[recipe]['directions']))
directions = directions.replace('&','&')
ingredients = '\n'.join(map(str,recipes[recipe]['ingredients']))
ingredients = ingredients.replace('&','&')
if recipes[recipe]['notes']:
notes = '\n'.join(map(str,recipes[recipe]['notes']))
notes = notes.replace('&','&')
else:
notes = ''
if recipes[recipe]['src']:
src = '\n'.join(map(str,recipes[recipe]['src']))
for cat in recipes[recipe]['categories']:
categories.append(cat)
recipeDir = 'broccoli/'+fileName #makes directory for each recipe if they do not already exist
if not os.path.exists(recipeDir):
os.makedirs(recipeDir)
if os.path.exists('img/'+fileName+'.jpg'): #copies over recipe image and sets imageName if it exists
imageName = fileName+'.jpg'
shutil.copy('img/'+fileName+'.jpg',recipeDir)
else:
imageName = ''
newRecipe = {'categories':recipes[recipe]['categories'],'description':'','directions':directions,'imageName':imageName,'ingredients':ingredients,'notes':notes,'nutritionalValues':'','preparationTime':recipes[recipe]['prepTime'],'servings':recipes[recipe]['amt'],'source':src,'title':title,'favorite':False}
recipePath = recipeDir+'/'+fileName
with open(recipePath+'.json','w',encoding='utf-8') as file: #dumps newRecipe into fileName/fileName.json
json.dump(newRecipe,file)
shutil.make_archive(recipeDir,'zip',recipeDir) #zips up recipe folder
shutil.move(recipeDir+'.zip',recipeDir+'.broccoli') #turns .zip files into .broccoli files
shutil.rmtree(recipeDir) #removes files and dirs that have been zipped up
categories = list(set(categories)) #dedupes category list and creates categories.json
with open('broccoli/categories.json','w') as file:
json.dump(categories,file)
shutil.make_archive('broccoli_import','zip','broccoli') #zips up archive
shutil.move('broccoli_import.zip','broccoli_import.broccoli-archive') #turns .zip into .broccoli-archive file
shutil.rmtree('broccoli') #removes files and dirs that have been zipped up