File size: 4,790 Bytes
4d3237d
 
 
 
211b763
 
 
 
 
 
 
 
 
 
 
 
 
4d3237d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211b763
 
 
 
 
 
 
4d3237d
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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))