Get-Nutrient composition of Food Items using Powershell


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 😀

nut

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

get-nutrient

SCRIPT : 

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

 

7 thoughts on “Get-Nutrient composition of Food Items using Powershell

  1. 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/”

    Like

Leave a comment