The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Spotify API: Get Current User's Playlists

TODO: Pull subtitle into page object

Notes

Loading

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Spotify API: Get Current User's Playlists


-- notes

- Data is output to the console

- Current gets a max of 1,000 playlists

- Change maxLoopCount to up that number

- >docs>https://developer.spotify.com/documentation/web-api/reference/get-a-list-of-current-users-playlists>



-- html
-- type: showbelow

<pre id="spotifyData">Loading</pre>


-- script
-- type: show

const user_id = localStorage.getItem("spotify_example_user_id")
const access_token = localStorage.getItem("spotify_example_access_token")

const getPlaylists = async () => {
  let playlists = []
  let maxLoopCount = 20
  for (let i=0; i<maxLoopCount; i++) {
    const url = new URL(`https://api.spotify.com/v1/me/playlists`)
    const params = {
      offset: 50 * i,
      limit: 50
    }
    url.search = new URLSearchParams(params).toString()
    const payload = {
      method: 'GET',
      headers: {
        'Authorization': `Bearer  ${access_token}`
      },
    }
    const body = await fetch(url, payload)
    const response = await body.json()
    if (!response.next) {
      i = maxLoopCount
    }
    response.items.forEach((item) => {
      playlists.push(item)
    })
  }
  return playlists 
}

const pullPlaylists = async () => {
  let output = ""
  const data = await getPlaylists()
  data.forEach((item) => {
    output += `${item.name}\n`
  })
  spotifyData.innerHTML = output
}

document.addEventListener("DOMContentLoaded", () => {
  if (user_id) {
    pullPlaylists()
  } else {
    spotifyData.innerHTML = "Not logged in"
  }
})


-- categories
-- Spotify 
-- APIs

-- metadata
-- date: 2023-12-25 00:33:22
-- id: 2a1vjo9k
-- site: aws
-- type: post
-- status: published