Last active
October 4, 2022 16:59
-
-
Save gravejester/0ef473ded31e06fae9e2 to your computer and use it in GitHub Desktop.
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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey, found your gist link on https://communary.wordpress.com/2014/11/27/search-spotify/ I have no use for it but just thought it was fun to have and test. Works great