Search-Spotify

Have you ever wanted to search Spotify from the PowerShell console? No? Want to try it anyway? I’ll show you how…

I created a wrapper for the public Spotify web API that lets you search for artists, albums, tracks and popular playlists in the Spotify online database. It’s real easy to use, just check out the built-in help. I also created a small helper function called Out-Spotify that will try to send your result to the Windows Spotify application if you have it installed and running. The cool thing is if you send a track, it will also automatically start to play it.

function Search-Spotify {
<#
.SYNOPSIS
Search Spotify.
.DESCRIPTION
This function uses the public web API of Spotify to let you query their database for information about
artists, albums, tracks and popular playlists.
.EXAMPLE
Search-Spotify 'Madonna'
Will search for 'Madonna' among all artists in the Spotify database.
.EXAMPLE
Search-Spotify 'Tania*'
Uses wildcard to search for all artists that starts with 'Tania'.
.EXAMPLE
Search-Spotify 'album:arrival artist:abba' -Type 'Album'
Will search for album with name matching 'Arrival' and artist matching 'Abba'
.EXAMPLE
Search-Spotify 'Madonna' -Type 'Album','Artis'
Will return both album and tracks with 'Madonna' in their name.
.NOTES
Author: Øyvind Kallstad
Date: 27.11.2014
Version: 1.0
#>
[CmdletBinding()]
param (
# Search query. Supports wildcard (*, max 2 per query) and operators (NOT/OR, must be in uppercase).
# By default, results are returned when a match is found in any field of the target object type.
# Searches can be made more specific by specifying an album, artist or track field filter.
# Field names must be entered in lower-case.
[Parameter(Position = 0)]
[ValidateNotNullorEmpty()]
[string] $Query,
# Type of information to search for. Valid choices are 'Artist','Album','Playlist' or 'Track'
[Parameter()]
[ValidateSet('Album','Artist','PlayList','Track')]
[string[]] $Type = 'Artist',
# Filter the results to only show results available in the supplied market.
# Defaults to the current region of the machine you are running the command from.
[Parameter()]
[string] $Market = [System.Globalization.RegionInfo]::CurrentRegion.TwoLetterISORegionName,
# The maximum number of objects to return. Defaults to 20.
[Parameter()]
[int] $Limit = 20,
# The index of the first object to return. Defaults to 0.
[Parameter()]
[int] $Offset = 0
)
$endPoint = 'https://api.spotify.com/v1/search'
$queryString = "?q=$([uri]::EscapeUriString($Query))&type=$($Type -Join ',')"
[uri]$uri = $endPoint + $queryString
try {
$result = Invoke-RestMethod Uri $uri
}
catch {
Write-Warning $_.Exception.Message
break
}
switch ($Type) {
'Artist' {
foreach ($artist in $result.artists.items) {
Write-Output (,([PSCustomObject] [Ordered] @{
Name = $artist.name
Popularity = $artist.popularity
Genres = $artist.genres
ID = $artist.id
Uri = $artist.uri
ExternalUrl = $artist.external_urls | Select-Object ExpandProperty 'spotify'
}))
}
}
'Album' {
foreach ($album in $result.albums.items) {
Write-Output (,([PSCustomObject] [Ordered] @{
Name = $album.name
Type = $album.album_type
ID = $album.id
Uri = $album.uri
ExternalUrl = $album.external_urls | Select-Object ExpandProperty 'spotify'
}))
}
}
'Track' {
foreach ($track in $result.tracks.items) {
Write-Output (,([PSCustomObject] [Ordered] @{
Name = $track.name
Album = $track.album | Select-Object ExpandProperty 'Name'
Artists = ($track.artists | Select-Object ExpandProperty 'Name')
DurationMS = $track.duration_ms
Explicit = $track.explicit
Popularity = $track.popularity
DiskNo = $track.disc_number
TrackNo = $track.track_number
ID = $track.id
Uri = "spotify:track:$($track.id)"
ExternalUrl = $track.external_urls | Select-Object ExpandProperty 'spotify'
}))
}
}
'PlayList' {
foreach ($playlist in $result.playlists.items) {
Write-Output (,([PSCustomObject] [Ordered] @{
Name = $playlist.name
OwnerID = $playlist.owner | Select-Object ExpandProperty 'id'
Collaborative = $playlist.collaborative
Tracks = $playlist.tracks.total
ID = $playlist.id
Uri = $playlist.uri
ExternalUrl = $playlist.external_urls | Select-Object ExpandProperty 'spotify'
}))
}
}
}
}
function Out-Spotify {
<#
.SYNOPSIS
Open Spotify with the specified Uri.
.EXAMPLE
Search-Spotify 'Love' -Type 'Track' | Select -First 1 | Out-Spotify
.NOTES
Author: Øyvind Kallstad
Date: 27.11.2014
Version: 1.0
#>
[CmdletBinding()]
param (
# Spotify Uri
[Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[string] $Uri
)
Start-Process $Uri
}

view raw
spotify.ps1
hosted with ❤ by GitHub

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s