-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseed_database.py
executable file
·95 lines (72 loc) · 3.05 KB
/
seed_database.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Script to seed database."""
import os
import json
# Populate my web database on data.heroku.com: s
# Switch out environment variable and run:
# DATABASE_URI="alkdjfladsj;" python3 seed_database.py
# Latest reseeds:
# 3/7/21 (clean data) 3/14/21 (clean data) 5/9/21 (update links)
from random import choice, randint
import re # this is to help split by two delimiters
import crud
import model
import server
# Commented out to avoid accidental reseeds
# os.system('dropdb recipes')
# os.system('createdb recipes')
# model.connect_to_db(server.app)
# model.db.create_all()
# Populate Tags DATA table (2 Columns, 2 Parameters, PK in API)
with open('data/tags.json') as g:
tag_data = json.loads(g.read())
tag_list = []
for tag in tag_data:
current_tag = crud.create_tag(tag['name'],
tag['id'])
tag_list.append(current_tag)
# Populate Recipe DATA table (7 columns, 7 parameters, PK in API)
with open('data/recipes.json') as f:
recipe_data = json.loads(f.read())
recipe_list = []
for recipe in recipe_data:
current_recipe = crud.create_recipe(recipe['img'],
recipe['serves'],
recipe['title'],
recipe['directions'],
recipe['cookingTime'],
recipe['prepTime'],
recipe['id']
)
recipe_list.append(current_recipe)
# Populate Ingredients DATA table (4 Columns, 3 Parameters, Generate PK)
abridged_ingredients_dict = {}
detailed_ingredients_dict = {}
for recipe in recipe_data:
for detailed_ingredient in recipe['ingredients']:
# Detailed Dictionary
if recipe['id'] not in detailed_ingredients_dict:
detailed_ingredients_dict[recipe['id']] = []
detailed_ingredients_dict[recipe['id']].append(detailed_ingredient)
# Split detailed ingredient
ingredient_split = re.split('TJ\'s |, ', detailed_ingredient)
# print(ingredient_split)
# Abridged Dictionary
if len(ingredient_split) > 1:
if recipe['id'] not in abridged_ingredients_dict:
abridged_ingredients_dict[recipe['id']] = []
abridged_ingredient = ingredient_split[1]
abridged_ingredients_dict[recipe['id']].append(abridged_ingredient)
# print(ingredient_split[1])
else:
abridged_ingredient = None
current_ingredient = crud.create_ingredient(
recipe['id'],
detailed_ingredient,
abridged_ingredient
)
# Populate Recipe Tags RELATIONSHIP table
for tag in recipe['tagIds']:
current_tag = crud.create_recipe_tag_relationship(
recipe['id'],
tag
)