Spaces:
Sleeping
Sleeping
| import requests | |
| import json | |
| import os | |
| READ_KEY_FROM_FILE = True | |
| def read_json_to_dict(file_path) -> dict: | |
| try: | |
| with open(file_path, 'r') as file: | |
| data = json.load(file) | |
| return data | |
| except FileNotFoundError: | |
| print("File not found at the specified path.") | |
| return {} | |
| except json.JSONDecodeError: | |
| print("Error decoding JSON file.") | |
| return {} | |
| def get_recipes_by_macronutrients(spoonacular_api_key: str, calories: int, protein_grams: int, carbs_grams: int, fat_grams: int, nb_recipes: int) -> list: | |
| if not spoonacular_api_key: | |
| return [] | |
| params = { | |
| 'apiKey': spoonacular_api_key, | |
| 'minCalories': int(calories) - 50, | |
| 'maxCalories': int(calories) + 50, | |
| 'minProtein': int(protein_grams) - 5, | |
| 'maxProtein': int(protein_grams) + 5, | |
| 'minCarbs': int(carbs_grams) - 5, | |
| 'maxCarbs': int(carbs_grams) + 5, | |
| 'minFat': int(fat_grams) - 5, | |
| 'maxFat': int(fat_grams) + 5, | |
| 'number': nb_recipes, | |
| } | |
| response = requests.get(url='https://api.spoonacular.com/recipes/findByNutrients', params=params) | |
| if response.status_code == 200: | |
| recipes = response.json() | |
| if recipes: | |
| return recipes | |
| else: | |
| return [] | |
| else: | |
| print(f"Error {response.status_code}: {response.text}") | |
| return [] | |
| def get_recipe_information_by_id(spoonacular_api_key, id) -> dict: | |
| if not spoonacular_api_key: | |
| return {} | |
| params = { | |
| 'apiKey': spoonacular_api_key, | |
| } | |
| response = requests.get(url=f'https://api.spoonacular.com/recipes/{id}/information', params=params) | |
| if response.status_code == 200: | |
| information = response.json() | |
| return information if information else {} | |
| else: | |
| print(f"Error {response.status_code}: {response.text}") | |
| return {} | |
| def print_recipe_macronutrients(recipe): | |
| if recipe: | |
| print(f"Recipe: {recipe['title']}") | |
| print(f"Calories: {recipe['calories']}") | |
| print(f"Protein: {recipe['protein']}") | |
| print(f"Carbs: {recipe['carbs']}") | |
| print(f"Fat: {recipe['fat']}") | |
| else: | |
| print("No recipe found") | |
| def print2string_recipe_macronutrients(recipe) -> str: | |
| string = f"" | |
| if recipe: | |
| string += f"Recipe: {recipe['title']}\n" | |
| string += f"Calories: {recipe['calories']}\n" | |
| string += f"Protein: {recipe['protein']}\n" | |
| string += f"Carbs: {recipe['carbs']}\n" | |
| string += f"Fat: {recipe['fat']}\n" | |
| else: | |
| string += "No recipe found\n" | |
| return string | |
| def print_recipe_information(recipe_info): | |
| if recipe_info: | |
| print(f"Ingredients:") | |
| for ingredient in recipe_info["extendedIngredients"]: | |
| print(f"- {ingredient['original']}") | |
| url = recipe_info['spoonacularSourceUrl'] | |
| print(f"URL: {url}\n") | |
| else: | |
| print("No recipe information found") | |
| def print2string_recipe_information(recipe_info) -> str: | |
| string = "" | |
| if recipe_info: | |
| string += f"Ingredients:\n" | |
| for ingredient in recipe_info["extendedIngredients"]: | |
| string += f"- {ingredient['original']}\n" | |
| url = recipe_info['spoonacularSourceUrl'] | |
| string += f"URL: {url}\n" | |
| else: | |
| string += "No recipe information found\n" | |
| return string | |
| def print_recipe(recipe, recipe_info): | |
| if recipe: | |
| print_recipe_macronutrients(recipe) | |
| else: | |
| print("No recipe found") | |
| if recipe_info: | |
| print_recipe_information(recipe_info) | |
| else: | |
| print("No recipe information found") | |
| def print2string_recipe(recipe, recipe_info) -> str: | |
| string = "" | |
| if recipe: | |
| string += print2string_recipe_macronutrients(recipe) | |
| else: | |
| string += "No recipe found\n" | |
| if recipe_info: | |
| string += print2string_recipe_information(recipe_info) | |
| else: | |
| string += "No recipe information found\n" | |
| return string | |
| if __name__ == "__main__": | |
| if READ_KEY_FROM_FILE: | |
| CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) | |
| spoonacular_api_key = read_json_to_dict(file_path=f"{CURRENT_DIR}/api_key.json")['spoonacular_api_key'] | |
| else: | |
| spoonacular_api_key = input('Enter your spoonacular api key: ') | |
| recipes = get_recipes_by_macronutrients( | |
| spoonacular_api_key=spoonacular_api_key, | |
| calories=600, | |
| protein_grams=25, | |
| carbs_grams=50, | |
| fat_grams=25, | |
| nb_recipes=3) | |
| for recipe in recipes: | |
| recipe_info = get_recipe_information_by_id( | |
| spoonacular_api_key=spoonacular_api_key, | |
| id=recipe['id']) | |
| print() | |
| print(print2string_recipe(recipe, recipe_info)) | |