-
Notifications
You must be signed in to change notification settings - Fork 8
/
SpoonacularAPI.py
80 lines (63 loc) · 2.77 KB
/
SpoonacularAPI.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
import requests
class Ingredient:
def __init__(self, ingredient):
self.ingredient = ingredient
self.name = ingredient.get("name")
self.amount = ingredient.get("amount")
self.measurement = ingredient.get("unitLong")
self.image = ingredient.get("image")
class Recipe:
def __init__(self, recipe):
self.recipe = recipe
self.name = recipe.get("name")
self.image = recipe.get("image")
self.readyInMinutes = recipe.get("readyInMinutes")
self.summary = recipe.get("summary")
self.ingredients = {}
self.steps = {}
self.addIngredient()
def addIngredient(self):
for ingredient in self.recipe.get("missedIngredients"):
ingredientClass = Ingredient(ingredient)
self.ingredients[ingredientClass.name] = ingredientClass
class RecipeBook:
def __init__(self, cuisine, keyword):
# What gets returned from the API
self.recipes = {}
# This will be the one we use to pull information easily from the mess of a return
self.recipeBook = {}
self.cuisine = cuisine
# we can later make it get random recipe if nothing is searched.
self.keyword = keyword
self.findRecipes()
self.addRecipe()
def findRecipes(self):
url = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/searchComplex"
querystring = {"limitLicense": "false", "offset": "0", "number": "10", "intolerances": "",
"instructionsRequired": "true", "ranking": "2", "addRecipeInformation": "true",
"fillIngredients": "true", "excludeIngredients": "", "cuisine": self.cuisine,
"query": self.keyword, "includeIngredients": "", "type": "main course"}
headers = {
'x-rapidapi-key': "25c9120b91mshda2f6434b6ba640p12800cjsn7e5efc1cb1b3",
'x-rapidapi-host': "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
self.recipes = response.json()
def addRecipe(self):
# make a for loop to loop over recipee
for recipe in self.recipes.get("results"):
recipeClass = Recipe(recipe)
self.recipeBook[recipe.get("title")] = recipeClass
# Can add more terms later. But its (cuiseine, keyword)
result = RecipeBook("american", "burger")
print(result.recipeBook)
print("\n\n\n\n")
# To get ingredients of a recipe
for value in result.recipeBook.values():
print(value.name)
print(value.ingredients)
print("\n\n")
# To get the quantities of each ingredient
for amount in value.ingredients.values():
print(amount.name)
print(amount.amount, " ", amount.measurement)