How to Search Spotify Using PowerShell & Spotify’s Metadata API

Windows PowerShell V3 includes a command, Invoke-WebRequest, which we can use to grab information using Spotify’s Metadata API.


PS > Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society


StatusCode        : 200
StatusDescription : OK
Content           : {"info": {"num_results": 417, "limit": 100, ..
                    "spotify:album:396vQgjfPsC0lisMX...
RawContent        : HTTP/1.1 200 OK
                    Vary: Accept-Charset
                    X-Varnish: 1580131781 1580105616
                    Age: 1908
                    Access-Control-Allow-Origin: *
                    Content-Length: 51642
                    Content-Type: application/json; charset=utf-8
                    Date: Thu, 01 ...
Forms             : {}
Headers           : {[Vary, Accept-Charset], [X-Varnish, 1580131..
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 51642

Let's check out the information in the Content property.


PS > Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society | 
  Select-Object -ExpandProperty Content


{"info": {"num_results": 417, "limit": 100, "offset": 0, 
"query": "Black Label Society", "type": "track", "page": 1}, 
"tracks": [{"album": {"released": "2009", "href": 
"spotify:album:396vQgjfP
sC0lisMXkrQ2A", "name": "Skullage", "availability": 
{"territories": "AT CH DE DK FI IS IT NO PT SE"}}, "name": 
"In This River", "popularity": "0.56160", "external-ids": 
[{"type": "isrc", "id": "USAR50451581"}], "length": 
235.466, "href": "spotify:track:1LgNG6JFuM3E4......

The output from isn’t as nicely formatted as you might have hoped, however, using the cmdlet ConvertFrom-Json converts the json document to usable object.


PS > $bls = Invoke-WebRequest http://ws.spotify.com/search/1/track.json?q=Black+Label+Society | 
Select-Object -ExpandProperty Content |
ConvertFrom-Json

Now we can simply call on a property and the information is displayed nicely formatted.


PS > $bls.info

num_results : 417
limit       : 100
offset      : 0
query       : Black Label Society
type        : track
page        : 1

Here’s an example on how you can display information about each track


PS > $bls.tracks

album        : @{released=2009; href=spotify:album:396vQgjfPsC0lisMXkrQ2A; name=Skullage; availability=}
name         : In This River
popularity   : 0.56160
external-ids : {@{type=isrc; id=USAR50451581}}
length       : 235.466
href         : spotify:track:1LgNG6JFuM3E4R7YsJgdf5
artists      : {@{href=spotify:artist:0zfT626RwO6zN3RDYeRit5; name=Black Label Society}, @{href=spotify:artist:1AeC9AuzqGc3IXMC2T5xny; name=Zakk Wylde}}
track-number : 10

album        : @{released=2010; href=spotify:album:7yji7GJSmvIpjMoEykLQq7; name=Order Of The Black; availability=}
name         : Crazy Horse
popularity   : 0.48597
external-ids : {@{type=isrc; id=USKO11000885}}
length       : 246.2
href         : spotify:track:2quyhkBRqiv56dQcfuLops
artists      : {@{href=spotify:artist:0zfT626RwO6zN3RDYeRit5; name=Black Label Society}}
track-number : 1

Rating 4.60 out of 5
[?]