init
Browse files- RecipeData.py +69 -0
RecipeData.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
|
5 |
+
API_KEY = '16b65f6510e040528021ce8f8b439002'
|
6 |
+
|
7 |
+
def fetchRecipeData(foodName, apiKey = API_KEY):
|
8 |
+
recipe = {}
|
9 |
+
|
10 |
+
# Fetching recipe Details from food name
|
11 |
+
url = f"https://api.spoonacular.com/recipes/search?query={foodName}&apiKey={apiKey}"
|
12 |
+
response = requests.get(url)
|
13 |
+
json_data = response.json()
|
14 |
+
|
15 |
+
# saving responce code
|
16 |
+
response_status_code = response.status_code
|
17 |
+
|
18 |
+
# selecting random recipe from fetched recipes
|
19 |
+
recipe_list = json_data['results']
|
20 |
+
foodRecipe = random.choice(recipe_list)
|
21 |
+
|
22 |
+
recipe_ID = foodRecipe['id']
|
23 |
+
|
24 |
+
# getting recipe details from api using recipe id
|
25 |
+
url = f"https://api.spoonacular.com/recipes/{recipe_ID}/information?apiKey={apiKey}&includeNutrition=true"
|
26 |
+
recipe_response = requests.get(url)
|
27 |
+
all_recipe_json_data = recipe_response.json()
|
28 |
+
|
29 |
+
# recipe instructions
|
30 |
+
recipe_instructions = preprocessing_instructions(all_recipe_json_data['instructions'])
|
31 |
+
|
32 |
+
# recipe summary
|
33 |
+
recipe_summary = all_recipe_json_data['summary']
|
34 |
+
|
35 |
+
# recipe ingredients
|
36 |
+
recipe_Ingredients = all_recipe_json_data['extendedIngredients']
|
37 |
+
for i, dict in enumerate(recipe_Ingredients):
|
38 |
+
recipe_Ingredients[i] = dict['originalString']
|
39 |
+
Ingredients = ', '.join(recipe_Ingredients)
|
40 |
+
|
41 |
+
# caloric Breakdow of recipe
|
42 |
+
recipe_caloric_breakdown = all_recipe_json_data['nutrition']['caloricBreakdown']
|
43 |
+
|
44 |
+
# storing all values in recipe dict
|
45 |
+
recipe['id'] = recipe_ID
|
46 |
+
recipe['title'] = foodRecipe['title']
|
47 |
+
recipe['readyTime'] = foodRecipe['readyInMinutes']
|
48 |
+
recipe['soureUrl'] = foodRecipe['sourceUrl']
|
49 |
+
|
50 |
+
recipe['instructions'] = recipe_instructions
|
51 |
+
|
52 |
+
recipe['ingridents'] = recipe_Ingredients
|
53 |
+
|
54 |
+
recipe_summary = recipe_summary.replace('<b>', '')
|
55 |
+
recipe_summary = recipe_summary.replace('</b>', '')
|
56 |
+
recipe['summary'] = recipe_summary
|
57 |
+
|
58 |
+
recipe['percentProtein'] = recipe_caloric_breakdown['percentProtein']
|
59 |
+
recipe['percentFat'] = recipe_caloric_breakdown['percentFat']
|
60 |
+
recipe['percentCarbs'] = recipe_caloric_breakdown['percentCarbs']
|
61 |
+
|
62 |
+
return response_status_code, recipe
|
63 |
+
|
64 |
+
|
65 |
+
def preprocessing_instructions(text):
|
66 |
+
word_to_remove = ['<ol>', '</ol>', '<li>', '</li>']
|
67 |
+
for word in word_to_remove:
|
68 |
+
text = text.replace(word, '')
|
69 |
+
return text
|