diff --git a/Nutritional-Facts/README.md b/Nutritional-Facts/README.md new file mode 100644 index 0000000000..91ae3d53d7 --- /dev/null +++ b/Nutritional-Facts/README.md @@ -0,0 +1,30 @@ +# Nutritional Facts +Use this script to retrieve the nutritional information of your favorite food items! + +## Setup instructions +### Packages +- Install all required packages using this `pip install -r requirements.txt`. + +### API Key +- Before you run the script you will need to receive a CalorieNinja API Key. +- To do this got to the [CalorieNinja](https://calorieninjas.com/register) website and create an account, which is completely free. +- Once you have made an account you can receive a free API-Key by navigating through the tabs and generating one. +- Once you have received an API Key you may paste it into the code here `headers={"X-API-key": "YOUR-API-KEY"}`. + +## Output +Here is an example use case of me using the script to find out the nutrition of chicken. + +![Chicken example](image.png) + +You can also be more specific + +![Chicken Drumsticks example](image-1.png) + +You can also do multiple food items at once + +![Multiple food items example](image-2.png) + +## Author(s) + +Adithya Bollu + diff --git a/Nutritional-Facts/image-1.png b/Nutritional-Facts/image-1.png new file mode 100644 index 0000000000..87b2a75209 Binary files /dev/null and b/Nutritional-Facts/image-1.png differ diff --git a/Nutritional-Facts/image-2.png b/Nutritional-Facts/image-2.png new file mode 100644 index 0000000000..dde06784c6 Binary files /dev/null and b/Nutritional-Facts/image-2.png differ diff --git a/Nutritional-Facts/image.png b/Nutritional-Facts/image.png new file mode 100644 index 0000000000..5cf853f1dd Binary files /dev/null and b/Nutritional-Facts/image.png differ diff --git a/Nutritional-Facts/nutrition.py b/Nutritional-Facts/nutrition.py new file mode 100644 index 0000000000..2b634bf3f1 --- /dev/null +++ b/Nutritional-Facts/nutrition.py @@ -0,0 +1,89 @@ +import requests +''' +Nutritional Info Script + +The script will be able to retrieve the nutrional facts about foods +using the CalorieNinjas API. Users can enter a food query and receive detailed +nutrional facts like calories, protein, fat etc. +''' + + +# Function to get the nutrition info of the food item +# Using the calorie ninja api + +def get_nutrition_info(food: str): + ''' + Retrieves nutritional information of food item, + Args: + food (str): Name of food (apple, chucken) + Returns: + list[dict] | None: A list of nutritional fact dictionaries if found otherwise None + ''' + + # API Call URL + api_url = f"https://api.calorieninjas.com/v1/nutrition?query={food}" + + # Request call to send to API, + # Takes in the api url, headers with api-key and a timeout + # so the request doesn't run forever + response = requests.get(api_url, headers={"X-API-key": "YOUR-API-KEY"}, timeout=30) + + # Checking if the request was a 200 or an error. + if response.status_code == requests.codes.ok: + data = response.json() + return data["items"] + + # Prints error if response was bad + print("Error:", response.status_code, response.text ) + return None + + +def print_info(facts : dict): + ''' + Prints the nutritional information in a legible format + + Args: + facts (dict): A dictionary of nutrition facts for a single food item + ''' + + # prints items in a hierarchical format. + for fact in facts: + # Do not indent name, indent everything else + if fact == "name": + print(f"{fact}: {facts[fact]}") + else: + print(f"\t{fact}: {facts[fact]}") + + + +def main(): + ''' + Main loop, for the nutrition script + Infintely prompts user for food queries + until user quits + ''' + + # main loop + while True: + # Print opening message + print("Enter food query: ", end="") + query = input() + + # Lower casing the query to normalize the checks + if query.lower() == "q" or query.lower() == "quit": + print("Thank you for using the Nutrition script!") + break + + # same thing here + items = get_nutrition_info(query.lower()) + # Checks if the query entered was invalid or valid + if not items: + print("Please try another query!\n") + else: + # Looping through each item to print them out in neat format + for item in items: + print_info(item) + print("\n") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/Nutritional-Facts/requirements.txt b/Nutritional-Facts/requirements.txt new file mode 100644 index 0000000000..a5505dbcfc --- /dev/null +++ b/Nutritional-Facts/requirements.txt @@ -0,0 +1,30 @@ +annotated-types==0.7.0 +anyio==4.9.0 +blinker==1.9.0 +certifi==2025.4.26 +charset-normalizer==3.4.3 +click==8.1.8 +colorama==0.4.6 +distro==1.9.0 +dotenv==0.9.9 +Flask==3.1.0 +flask-cors==5.0.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.10 +itsdangerous==2.2.0 +Jinja2==3.1.6 +jiter==0.9.0 +MarkupSafe==3.0.2 +openai==1.78.0 +pydantic==2.11.4 +pydantic_core==2.33.2 +python-dotenv==1.1.0 +requests==2.32.5 +sniffio==1.3.1 +tqdm==4.67.1 +typing-inspection==0.4.0 +typing_extensions==4.13.2 +urllib3==2.5.0 +Werkzeug==3.1.3