-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
104 lines (89 loc) · 3.43 KB
/
main.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
import dotenv
import sqlite3
import os
from dotenv import load_dotenv
from api_integration.edamam_integration import get_recipe
from api_integration.openai_integration import alt_recipe_query
from db.recipe_sql import (create_tables, store_original_recipe,
store_alt_recipe, lookup_original_recipe_id,
lookup_prev_recipe, select_recipe,
reset_database)
# Importing env variables for API authentication
load_dotenv()
EDAMAM_APP_ID = os.getenv('EDAMAM_APP_ID')
EDAMAM_APP_KEY = os.getenv('EDAMAM_APP_KEY')
OPENAI_KEY = os.getenv('OPENAI_KEY')
def main(action, *args):
if action.lower() == "reset":
return reset_database()
elif action.lower() == "make":
return make_recipe(args[0], args[1])
elif action.lower() == "lookup":
return lookup_db(args[0]) if args else lookup_db()
elif action.lower() == "exit":
exit()
else:
return "Choice not valid, please try again!\n"
def make_recipe(diet, requested_recipe):
recipe_query = get_recipe(EDAMAM_APP_ID, EDAMAM_APP_KEY, requested_recipe)
original_name = recipe_query['name']
ingredients = []
for ingredient in recipe_query['ingredients']:
ingredients.append(ingredient)
recipe_link = recipe_query['url']
store_original_recipe(original_name, ingredients, recipe_link)
alt_recipe = alt_recipe_query(OPENAI_KEY, diet, recipe_query)
alt_name = alt_recipe['name']
alt_ingredients = alt_recipe['ingredients']
alt_instructions = alt_recipe['instructions']
original_recipe_id = lookup_original_recipe_id(original_name,
ingredients,
recipe_link)
store_alt_recipe(original_recipe_id,
alt_name,
alt_ingredients,
alt_instructions)
return (
f"\nAlternative Recipe: {alt_name}"
f"\nIngredients:f{','.join(alt_ingredients)}"
f"\nInstructions: {alt_instructions}\n"
)
def lookup_db(recipe_id=None):
if recipe_id:
return select_recipe(recipe_id)
else:
foundRecipes, recipes = lookup_prev_recipe()
if foundRecipes and __name__ == "__main__":
try:
recipe_id = int(
input("Enter the ID of the recipe you want to view: ")
)
except ValueError:
print("Invalid input. Please enter a number.")
return
select_recipe(recipe_id)
else:
return recipes
if __name__ == "__main__":
create_tables()
print("Welcome to DietMatch!")
while True:
print("What would you like to do?")
choice = input(
"Type \"make\" to make a recipe,"
" \"lookup\" to find a previously made recipe,"
" \"exit\" to quit the program,"
" or \"reset\" to clear database! "
)
if choice == "make":
diet = input("\nPlease tell me what kind of diet you follow: ")
requested_recipe = input(
"Please tell me what kind of dish you would like to make: "
)
result = main(choice, diet, requested_recipe)
if result:
print(result)
else:
print("Recipe creation failed or returned empty result.")
else:
print(main(choice))