|
| 1 | +import requests |
| 2 | +import random |
| 3 | + |
| 4 | +# Replace 'your_api_key_here' with your actual Spoonacular API key |
| 5 | +API_KEY = "74de4b7826b74b70b4d3a0fe3191239a" |
| 6 | +BASE_URL = "https://api.spoonacular.com/recipes" |
| 7 | + |
| 8 | +def find_recipes(ingredients): |
| 9 | + """Search for recipes based on ingredients using the Spoonacular API.""" |
| 10 | + url = f"{BASE_URL}/findByIngredients" |
| 11 | + params = { |
| 12 | + 'ingredients': ','.join(ingredients), |
| 13 | + 'number': 10, # Get up to 10 recipes |
| 14 | + 'apiKey': API_KEY |
| 15 | + } |
| 16 | + response = requests.get(url, params=params) |
| 17 | + |
| 18 | + if response.status_code == 200: |
| 19 | + recipes = response.json() |
| 20 | + return [(recipe['title'], recipe['id']) for recipe in recipes] |
| 21 | + else: |
| 22 | + print(f"Error: {response.status_code}") |
| 23 | + return [] |
| 24 | + |
| 25 | +def get_recipe_details(recipe_id): |
| 26 | + """Get details of a specific recipe by its ID.""" |
| 27 | + url = f"{BASE_URL}/{recipe_id}/information" |
| 28 | + params = {'apiKey': API_KEY} |
| 29 | + response = requests.get(url, params=params) |
| 30 | + |
| 31 | + if response.status_code == 200: |
| 32 | + return response.json() |
| 33 | + else: |
| 34 | + print(f"Error: {response.status_code}") |
| 35 | + return None |
| 36 | + |
| 37 | +def plan_meals(ingredients): |
| 38 | + """Plan meals for the week based on available ingredients.""" |
| 39 | + recipes = find_recipes(ingredients) |
| 40 | + |
| 41 | + if not recipes: |
| 42 | + return "No recipes found with the given ingredients." |
| 43 | + |
| 44 | + meals = random.sample(recipes, min(7, len(recipes))) |
| 45 | + return meals |
| 46 | + |
| 47 | +def user_interface(): |
| 48 | + print("Welcome to the Meal Planner!") |
| 49 | + print("Enter the ingredients you have, separated by commas:") |
| 50 | + |
| 51 | + ingredients = input().split(",") |
| 52 | + ingredients = [ingredient.strip().lower() for ingredient in ingredients] |
| 53 | + |
| 54 | + print("\nSearching for recipes based on your ingredients...\n") |
| 55 | + recipes = find_recipes(ingredients) |
| 56 | + |
| 57 | + if recipes: |
| 58 | + print(f"Found the following recipes with your ingredients:") |
| 59 | + for i, (title, _) in enumerate(recipes): |
| 60 | + print(f"{i + 1}. {title}") |
| 61 | + else: |
| 62 | + print("No recipes found with the given ingredients.") |
| 63 | + |
| 64 | + print("\nDo you want to plan meals for the week with these ingredients? (yes/no)") |
| 65 | + if input().strip().lower() == "yes": |
| 66 | + meals = plan_meals(ingredients) |
| 67 | + print("\nHere is your meal plan for the week:") |
| 68 | + for i, (title, recipe_id) in enumerate(meals, 1): |
| 69 | + print(f"Day {i}: {title}") |
| 70 | + recipe_details = get_recipe_details(recipe_id) |
| 71 | + if recipe_details: |
| 72 | + print(f" - Ready in: {recipe_details['readyInMinutes']} minutes") |
| 73 | + print(f" - Servings: {recipe_details['servings']}") |
| 74 | + else: |
| 75 | + print("No meal plan generated.") |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + user_interface() |
0 commit comments