Public/Get-SpotifyPlaylists.ps1
|
<# .SYNOPSIS Get your Spotify playlists .DESCRIPTION Returns all your Spotify playlists as objects with Name and Id. Use -Name to search for a specific playlist and get its Id for use with Add-SpotifyPlaylistTracks. .PARAMETER Name Optional. Filter playlists by name (case-insensitive, partial match). Returns the matching playlist(s). .PARAMETER ClientId Optional. The ClientId of the app you registered in the Spotify developer portal. .PARAMETER RedirectURI Optional. The redirect URI used for OAuth authentication. .PARAMETER ConfigFile Optional. The path to a JSON configuration file containing 'ClientId' and 'RedirectURI' properties. .EXAMPLE Get-SpotifyPlaylists .EXAMPLE $pl = Get-SpotifyPlaylists -Name "Rock Olmen 2026" Add-SpotifyPlaylistTracks -PlaylistId $pl.Id -Tracks $songs #> function Get-SpotifyPlaylists { [CmdletBinding()] param ( [Parameter(Mandatory=$false)] [string] $Name, [Parameter(Mandatory=$false)] [string] $ClientId, [Parameter(Mandatory=$false)] [string] $RedirectURI, [ValidateScript({Test-Path $_})] [Parameter(Mandatory=$false)] [string] $ConfigFile ) Set-StrictMode -Version 1.0 $ErrorActionPreference = 'Stop' $TokenParams = @{ Scopes = @('playlist-read-private', 'playlist-read-collaborative') } foreach ($param in @('ClientId', 'RedirectURI', 'ConfigFile')) { if ($PSBoundParameters.ContainsKey($param)) { $TokenParams.Add($param, $PSBoundParameters[$param]) } } $token = Get-SpotifyToken @TokenParams $headers = @{ Authorization = "Bearer $token" } $uri = $script:MYPLAYLISTS_URI $raw = [System.Collections.ArrayList]::new() while ($true) { $response = ( Invoke-SpotifyRequest -Uri $uri -Headers $headers ).Content | ConvertFrom-Json $raw.AddRange(@($response.items)) | Out-Null if (! $response.next) { break } $uri = $response.next [System.Threading.Thread]::Sleep($script:API_DELAY) } $result = [array]($raw | ForEach-Object { [PSCustomObject]@{ Name = $_.name; Id = $_.id } }) if ($PSBoundParameters.ContainsKey('Name')) { $match = $result | Where-Object { $_.Name -ilike "*$Name*" } if (!$match) { Write-Warning "No playlist found matching '$Name'" return } return $match } return $result } |