-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.py
64 lines (46 loc) · 1.98 KB
/
recipe.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
import json
import requests
from models import db, Recipe
def add_structure_recipe(res):
"""takes response from API request and structures the return recipe info for db, returns recipe object"""
response_string = res.text # get text from response
response_dict = json.loads(response_string) #convert to json dict
id = response_dict["id"]
title = response_dict["title"]
try:
img = response_dict["image"]
except KeyError:
print("No image available")
img = ""
protein = response_dict["nutrition"]["nutrients"][9]["amount"]
source_url = response_dict["sourceUrl"]
ingredient_info = response_dict["extendedIngredients"] # list of ingredients with all the info
ingredients = []
for ing in ingredient_info: # make a list with just the amount, unit, name
ingredients.append([ing["amount"], ing["unit"], ing["name"]])
if len(response_dict["analyzedInstructions"]) != 0:
instructions = response_dict["analyzedInstructions"][0]["steps"]
else:
instructions = {}
time = response_dict["readyInMinutes"]
servings= response_dict["servings"]
# adds recipe to database
recipe = Recipe.addRecipe(
id=id,
title=title,
img=img,
protein=protein,
source_url=source_url,
ingredients=json.dumps(ingredients), #converts to json
instructions=json.dumps(instructions), #converts to json
time=time,
servings=servings
)
db.session.commit()
return recipe
def gets_recipes(API_KEY, min_protein, intolerances, query):
"""Makes a request to spoonacular api for recipes based on query"""
res = requests.get(f"https://api.spoonacular.com/recipes/complexSearch?apiKey={API_KEY}&minProtein={min_protein}&intolerances={intolerances}&number=3&diet=vegan&query={query}&addRecipeInformation=True")
response_string = res.text
response_dict = json.loads(response_string) #convert to json dict
return response_dict