Skip to content

Instantly share code, notes, and snippets.

@dedale
Created January 31, 2021 21:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedale/3797ac12c8a78035b4fa78affa1c4528 to your computer and use it in GitHub Desktop.
Save dedale/3797ac12c8a78035b4fa78affa1c4528 to your computer and use it in GitHub Desktop.
Get all public Spotify playlists for a user
open FSharp.Json
open Hopac
open HttpFs.Client
open System
open System.IO
type Credentials = {
access_token : string
}
let auth () =
let id = File.ReadAllText(@"D:\perso\zic\spotify\Id").Trim()
let secret = File.ReadAllText(@"D:\perso\zic\spotify\Secret").Trim()
let idSecret = sprintf "%s:%s" id secret
let basicAuth =
idSecret
|> Seq.map byte
|> Array.ofSeq
|> Convert.ToBase64String
|> sprintf "Basic %s"
Request.createUrl HttpMethod.Post "https://accounts.spotify.com/api/token"
|> Request.setHeader (Custom ("Authorization", basicAuth))
|> Request.body (BodyForm [ NameValue ("grant_type", "client_credentials") ])
|> Request.responseAsString
|> run
|> Json.deserialize<Credentials>
let getPage token url =
let bearer = sprintf "Bearer %s" token
Request.createUrl HttpMethod.Get url
|> Request.setHeader (Custom ("Authorization", bearer))
|> Request.responseAsString
|> run
type Playlist = {
href : string
name : string
}
type PlaylistPage = {
items : Playlist list
next : string option
}
let rec getPlaylists token url =
let page = getPage token url |> Json.deserialize<PlaylistPage>
for playlist in page.items do
printfn "%s: %s" playlist.name playlist.href
match page.next with
| None -> ()
| Some u -> getPlaylists token u
[<EntryPoint>]
let main argv =
let credentials = auth()
let url = "https://api.spotify.com/v1/users/particledetector2019/playlists?limit=50"
getPlaylists credentials.access_token url
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment