-
Notifications
You must be signed in to change notification settings - Fork 20
/
build_database.py
172 lines (155 loc) · 6.14 KB
/
build_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from dbcommands import *
import logging
import json
from recipe import *
import os.path
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M:%S',
filename='log',
filemode='a')
DATABASE_PATH = 'cookbook'
DB = DataBase(DATABASE_PATH)
'''
recipes = "'title':title,\\\n"
recipes = recipes + "'url':url,\\\n"
recipes = recipes + "'source':source,\\"
recipes = recipes + "'directions':directions,\\\n"
recipes = recipes + "'time':time,\\\n"
recipes = recipes + "'total_cost':total_cost,\\\n"
recipes = recipes + "'total_cost_per_serving':total_cost_per_serving,\\\n"
recipes = recipes + "'serving_size':serving_size,\\\n"
recipes = recipes + "'total_grams':total_grams,\\\n"
recipes = recipes + "'num_ingredients':num_ingredients,\\\n"
with open('list_of_nutrients.txt','r') as f:
for line in f:
recipes = recipes + "'" + formatIngredientKey(line) + "':0,\\\n"
print(recipes)
'''
logger = logging.getLogger('build_database.createTable')
recipes = 'recipes (id INTEGER PRIMARY KEY AUTOINCREMENT, '
recipes = recipes + 'title TEXT, '
recipes = recipes + 'url TEXT UNIQUE, '
recipes = recipes + 'source TEXT, '
recipes = recipes + 'directions TEXT, '
recipes = recipes + 'time TEXT, '
recipes = recipes + 'total_cost REAL, '
recipes = recipes + 'total_cost_per_serving REAL, '
recipes = recipes + 'serving_size REAL, '
recipes = recipes + 'total_grams REAL, '
recipes = recipes + 'num_ingredients INTEGER, '
with open('list_of_nutrients.txt','r') as f:
for line in f:
recipes = recipes + formatIngredientKey(line) + " REAL,"
recipes = recipes[:-1] + ')'
if not DB.tableExists('recipes'):
logger.warning('"recipes" table not found')
logger.info('Creating "recipes" table...')
DB.createTable(recipes)
else:
logger.debug('Table "recipes" found')
logger = logging.getLogger('build_database.createIngredients')
recipes = 'ingredients (id INTEGER PRIMARY KEY AUTOINCREMENT, '
recipes = recipes + 'recipe_id INTEGER, '
recipes = recipes + 'ingredient_uuid TEXT UNIQUE, '
recipes = recipes + 'actual TEXT, '
recipes = recipes + 'measurement TEXT, '
recipes = recipes + 'description TEXT, '
recipes = recipes + 'ndb_no TEXT, '
recipes = recipes + 'cost REAL, '
recipes = recipes + 'grams REAL)'
if not DB.tableExists('ingredients'):
logger.warning('"ingredients" table not found')
logger.info('Creating "ingredients" table...')
DB.createTable(recipes)
else:
logger.debug('Table "ingredients" found')
'''
def newRecipe( self,\
title,\
url,\
source,\
directions,\
time,\
total_cost,\
total_cost_per_serving,\
serving_size,\
total_grams,\
num_ingredients):
'''
'''
'recipe_id':recipe_id,\
'ingredient_uuid':ingredient_uuid,\
'actual':actual,\
'measurement':measurement,\
'description':description,\
'ndb_no':ndb_no,\
'cost':cost,\
'grams':grams\
'''
startNum = 9620
logger = logging.getLogger('build_database.building')
with open('get_recipes/recipes/index0_10.txt','r') as f:
for line in f:
#try:
try:
data = line.strip().split()
recipeNum = int(data[0])
url = data[1]
title = ' '.join(data[2:])
except:
recipeNum = 0
file = 'get_recipes/recipes/' + str(recipeNum/500) + '/' + str(recipeNum) + '.md'
if recipeNum>startNum and os.path.isfile(file):
logger.info(line)
try:
a = Recipe('get_recipes/recipes/' + str(recipeNum/500) + '/' + str(recipeNum) + '.md')
recipe = a.returnJson()
recipe['url'] = url
recipe['title'] = title
# Insert the new recipe
try:
recipeID = DB.newRecipe(recipe['title'],recipe['url'],recipe['source'],recipe['directions'],recipe['time'],recipe['total_cost'],recipe['total_cost_per_serving'],recipe['serving_size'],recipe['total_grams'],len(recipe['ingredients']))
except:
recipeID = DB.getRecipeIDfromURL(recipe['url'])
# Update the nutrients
for nutritionClass in recipe['nutrition'].keys():
for nutrient in recipe['nutrition'][nutritionClass].keys():
DB.updateIngredient(nutrient,recipe['nutrition'][nutritionClass][nutrient],recipeID)
# Insert the ingredients
for ingredient in recipe['ingredients']:
try:
actual = ingredient['actual']
ingredient_uuid = recipe['url']+ingredient['ndb_no']
measurement = ingredient['measurement']
description = ingredient['description']
ndb_no = ingredient['ndb_no']
cost = ingredient['cost']
grams = ingredient['grams']
foo = DB.addIngredient(recipeID,ingredient_uuid,actual,measurement,description,ndb_no,cost,grams)
except:
logger.warning("ingredient already exists")
except:
logger.error("Unexpected error:", sys.exc_info()[0])
'''
recipe = Recipe(sys.argv[1])
recipe['url']='asdlfkj'
try:
recipeID = DB.newRecipe(recipe['title'],recipe['url'],recipe['source'],recipe['directions'],recipe['time'],recipe['total_cost'],recipe['total_cost_per_serving'],recipe['serving_size'],recipe['total_grams'],len(recipe['ingredients']))
except:
recipeID = DB.getRecipeIDfromURL(recipe['url'])
for nutritionClass in recipe['nutrition'].keys():
for nutrient in recipe['nutrition'][nutritionClass].keys():
DB.updateIngredient(nutrient,recipe['nutrition'][nutritionClass][nutrient],recipeID)
for ingredient in recipe['ingredients']:
print(ingredient)
actual = ingredient['actual']
ingredient_uuid = recipe['url']+ingredient['ndb_no']
measurement = ingredient['measurement']
description = ingredient['description']
ndb_no = ingredient['ndb_no']
cost = ingredient['cost']
grams = ingredient['grams']
foo = DB.addIngredient(recipeID,ingredient_uuid,actual,measurement,description,ndb_no,cost,grams)
break
'''