If you’re Calorie Conscious or by any chance nerd? Then this one is for you 🙂 So how about getting the Nutritional value of a food item from Powershell 😀
HOW IT WORKS :
To make it work we are simply making calls to a REST API of “United States Department of Agriculture” to search your food item and some data wrangling to get the nutrition report.
NOTE : You’ve to just register for a API subscription key from here under ‘gaining access’ section of the page. Once you’re subscribed, place the key in the script (Line-15) and you’ll be able to get-nutrients like in the following animation
SCRIPT :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-Nutrient | |
{ | |
[cmdletbinding()] | |
[Alias("Nutrient")] | |
Param | |
( | |
[Parameter(Mandatory=$True)] [String] $Query | |
) | |
$Script:i=0 | |
Write-Verbose "Searching `"$Query`" in Nutrient database.." | |
# Send Web request to the REST API to Search items with the user given Query/Item | |
$result = (Invoke-RestMethod -Uri "http://api.nal.usda.gov/ndb/search/?format=json&q=$Query&sort=n&offset=0&api_key=YOURSUBSCRIPTIONKEYGOESHERE").list.item |` | |
select @{n='Choice';e={$Script:i=$Script:i+1;$Script:i}},` | |
@{n='Name';e={(Get-Culture).textinfo.totitlecase(($_.name -split ", UPC: ")[0].ToLower())}},` | |
@{n='Category';e={$_.Group}},@{n='12DigitBarCode';e={$barcode=($_.name -split "UPC: ")[1];If($barcode){$barcode}else{"NA"}}}, | |
@{n='NutrientDBNo';e={$_.Ndbno}} | |
# Display results from the Search and Get the Choice from the user | |
If($result) | |
{ | |
Write-host "$($result |select Choice, Name, 12DigitBarCode -first 20 |Out-String)" | |
$Choice = Read-Host "Enter You choice" | |
Write-Verbose "Calling REST API and Getting Nutrient composition" | |
# Extract the Nutrient Database number and Get Nutrient report from another REST API call | |
$Ndbno = ($result | ?{$_.choice -eq $Choice}).NutrientDBNo | |
$Food = (Invoke-RestMethod -Uri "http://api.nal.usda.gov/ndb/reports/?ndbno=$ndbno&type=b&format=json&api_key=nPEalh5PismxDHfVfSJ0VhSAG3uma32bgqO2RtEs").Report.food | |
cls | |
# Display Nutrient report | |
$Food.nutrients | ?{($_.value -as [double]) -gt 0} | select @{n='NutrientName';e={$_.Name}},` | |
@{n='Group';e={$_.group}},` | |
@{n='Value per 100g';e={$_.Value}},` | |
@{n='Unit';e={$_.unit}},` | |
@{n='Measures';e={$PV = $_;Foreach($Item in $pv.measures){"$([int]$Item.qty) "+$Item.Label+" = $($Item.value) "+$PV.unit}}} | |
} | |
Else | |
{ | |
Write-Host "Couldn't find an item that match `"$Query`", please try another name." -ForegroundColor Red | |
} | |
} | |
Get-Nutrient 'peanut butter' |ft -AutoSize | |
Get-Nutrient 'raw apple' | ft -AutoSize | |
Get-Nutrient 'coke' | ft -AutoSize |
Have fun exploring this script and find some time to show-off to your friends 😉
Prateek Singh
Follow @SinghPrateik
[…] on January 10, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
LikeLike
COOL one 😉
LikeLike
I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog. I will keep visiting.
LikeLiked by 1 person
Thank you so much, I’m glad you liked the content on my blog!
LikeLike
Hi, I’m kind of new to powershell but i’m enjoying learning and using it.
About that, your blog is pretty cool, tons of valuable informations for a beginner like me, thanks !
About the Get-Nutrient cmdlet, do you think there is a way to do the same thing but with this site : “https://informationsnutritionnelles.fr/”
LikeLike
Not sure if this matters to you or not but it looks like you still have your API Key listed on line 30! Great script though! Thanks for this!
LikeLiked by 1 person
It doesnt matters 🙂 thanks anyway!
LikeLike