-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestapi.py
63 lines (44 loc) · 2.07 KB
/
testapi.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
import requests
def retrieve_data(value):
URL1 = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients"
querystring = {"number": "5", "ranking": "1", "ignorePantry": "false", "ingredients": value}
headers = {
'x-rapidapi-host': "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com",
'x-rapidapi-key': "51d5225cf3msh3beb5dacf075161p1812b2jsnba32bca5162d",
}
response1 = requests.request("GET", URL1, headers=headers, params=querystring)
recipes_data = response1.json()
titles = []
ids = []
recipe_fat_list = []
recipe_protein_list = []
recipe_carbs_list = []
images = []
for hit in recipes_data:
titles.append(hit["title"])
ids.append(hit["id"])
images.append(hit["image"])
links = []
keys = ["fat", "carbs", "protein"]
for id in ids:
URL2 = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/" + str(id) + "/information"
response2 = requests.request("GET", URL2, headers=headers, params=querystring)
other_data = response2.json()
links.append(other_data["sourceUrl"])
for id in ids:
URL3 = "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/" + str(id) + "/nutritionWidget.json"
response3 = requests.request("GET", URL3, headers=headers, params=querystring)
response3_json = response3.json()
#print(response3_json)
for key in keys:
if key == "fat":
response_fat_no_units = response3_json[key][:-1]
recipe_fat_list.append(response_fat_no_units)
elif key == "carbs":
response_carbs_no_units = response3_json[key][:-1]
recipe_carbs_list.append(response_carbs_no_units)
elif key == "protein":
response_protein_no_units = response3_json[key][:-1]
recipe_protein_list.append(response_protein_no_units)
return [titles, links, recipe_fat_list, recipe_carbs_list, recipe_protein_list, images]
print(retrieve_data("banana"))