spotify-web-api-node icon indicating copy to clipboard operation
spotify-web-api-node copied to clipboard

[Help] how to iterate with next url?

Open SwapnilSoni1999 opened this issue 5 years ago • 1 comments

as #45 didnt helped much so posting again, i have got body.tracks.next now how to get more tracks, with request module it can be done but feels a bit long can you add Object.nextUri something method?

SwapnilSoni1999 avatar Sep 15 '19 11:09 SwapnilSoni1999

I was searching for a solution to this, but couldn't find anything either, so I made my own little function.

const getAllTracks = async (playlist) => {
  let tracks = []
  const { body } = await spotify.getPlaylistTracks(playlist)
  tracks = body.items
  if (body.total > 100)
    for (let i = 1; i < Math.ceil(body.total / 100); i++) {
      const add = await spotify.getPlaylistTracks(playlist, { offset: 100 * i })
      tracks = [...tracks, ...add.body.items]
    }
  return tracks
}

This returns an Array containing all the items in a given playlist. Could easily be modified to return the rest of the playlist info too, but in my case I just needed the tracks.

vaaski avatar Jun 24 '20 16:06 vaaski