Friday, November 8, 2013

PowerShell: Invoke-RestMethod Decibel.Net Sample

I learned how to insert Headers into an Inoke-RestMethod request in order to pull music information from an online database.  Afterwards, I can insert the received objects into MP3 ID3 tags using methods mentioned in my previous posts (including album covers).  I recently started using Decibel for grabbing music information (even though indications are that they're going to a pay-only API).  As of now, they have a free version and two paid versions which allow more information about each query.  When you create a free account, you'll be requested to make an application.  I called my application "PowerShell."  The system generated an Application ID & Key.  New to me was having to pass this Application ID, Application Key, and Date/Time via a header request when making my PowerShell queries.  After many web searches and trying different script methods, I'm sharing my successful script:

#Replace the x's with your Applications information
#Using an artist like "Fine Young Cannibals" will search for artists
#that have any or all of those three words

$DAppID = "xxxxxxxx"
$DAppKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
[string]$DTimeStamp = get-date -format "yyyyMMdd HH:mm:ss"
$Artist = "Metallica"

#Decibel documention

#I requested all albums that have Metallica in the artists name field

$query = invoke-restmethod http://api.decibel.net/v1/albums?artist=$Artist `
-headers @{"DecibelAppID"=$DAppID;"DecibelAppKey"=$DAppKey;"DecibelTimestamp"=$DTimeStamp;} `
-method get

$result = $query.AlbumQueryResult.ResultSet.album

# I filtered for only Metallica album names

foreach ($r in $result){$r.Name|where-object{$r.Artists -eq $Artist}}