|
import requests
|
|
import json
|
|
import random
|
|
|
|
API_KEY = '16b65f6510e040528021ce8f8b439002'
|
|
|
|
def fetchRecipeData(foodName, apiKey = API_KEY):
|
|
recipe = {}
|
|
|
|
|
|
url = f"https://api.spoonacular.com/recipes/search?query={foodName}&apiKey={apiKey}"
|
|
response = requests.get(url)
|
|
json_data = response.json()
|
|
|
|
|
|
response_status_code = response.status_code
|
|
|
|
|
|
recipe_list = json_data['results']
|
|
foodRecipe = random.choice(recipe_list)
|
|
|
|
recipe_ID = foodRecipe['id']
|
|
|
|
|
|
url = f"https://api.spoonacular.com/recipes/{recipe_ID}/information?apiKey={apiKey}&includeNutrition=true"
|
|
recipe_response = requests.get(url)
|
|
all_recipe_json_data = recipe_response.json()
|
|
|
|
|
|
recipe_instructions = preprocessing_instructions(all_recipe_json_data['instructions'])
|
|
|
|
|
|
recipe_summary = all_recipe_json_data['summary']
|
|
|
|
|
|
recipe_Ingredients = all_recipe_json_data['extendedIngredients']
|
|
for i, dict in enumerate(recipe_Ingredients):
|
|
recipe_Ingredients[i] = dict['originalName']
|
|
Ingredients = ', '.join(recipe_Ingredients)
|
|
|
|
|
|
recipe_caloric_breakdown = all_recipe_json_data['nutrition']['caloricBreakdown']
|
|
|
|
|
|
recipe['id'] = recipe_ID
|
|
recipe['title'] = foodRecipe['title']
|
|
recipe['readyTime'] = foodRecipe['readyInMinutes']
|
|
recipe['soureUrl'] = foodRecipe['sourceUrl']
|
|
|
|
recipe['instructions'] = recipe_instructions
|
|
|
|
recipe['ingridents'] = recipe_Ingredients
|
|
|
|
recipe_summary = recipe_summary.replace('<b>', '')
|
|
recipe_summary = recipe_summary.replace('</b>', '')
|
|
recipe['summary'] = recipe_summary
|
|
|
|
recipe['percentProtein'] = recipe_caloric_breakdown['percentProtein']
|
|
recipe['percentFat'] = recipe_caloric_breakdown['percentFat']
|
|
recipe['percentCarbs'] = recipe_caloric_breakdown['percentCarbs']
|
|
|
|
return response_status_code, recipe
|
|
|
|
|
|
def preprocessing_instructions(text):
|
|
word_to_remove = ['<ol>', '</ol>', '<li>', '</li>']
|
|
for word in word_to_remove:
|
|
text = text.replace(word, '')
|
|
return text |