Skip to content

Commit

Permalink
fix: 🐛 One can now replace (and add) more than 100 tracks at once
Browse files Browse the repository at this point in the history
  • Loading branch information
EricLambrecht committed Feb 11, 2019
1 parent e3e352b commit 93cad92
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/utils/Spotify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SpotifyWebApi from 'spotify-web-api-js';
import chunk from 'lodash/chunk';

export const api = new SpotifyWebApi();

Expand Down Expand Up @@ -126,12 +127,16 @@ export default class Spotify {

/**
* Adds multiple tracks to a playlist
* Supports more than 100 tracks, unlike the raw Spotify Web API endpoints.
* @param playlistId
* @param uris
* @returns {Promise}
*/
static async addTracksToPlaylist(playlistId, uris) {
return api.addTracksToPlaylist(playlistId, uris);
const chunks = chunk(uris, 100);
for (const uriChunk of chunks) { // eslint-disable-line no-restricted-syntax
await api.addTracksToPlaylist(playlistId, uriChunk); // eslint-disable-line no-await-in-loop
}
}

/**
Expand All @@ -152,4 +157,21 @@ export default class Spotify {
static getArtistNameFromTrack(track) {
return track.artists.map(artist => artist.name).join(',');
}

/**
* Replaces the entire playlist with the provided track-URIs.
* Since Spotify Web API supports only 100 tracks, array greater than
* 100 tracks will be split into chunks to replace everything.
*/
static async replaceTracks(playlistId, uris) {
const chunks = chunk(uris, 100);
const firstChunk = chunks.shift();
await api.replaceTracksInPlaylist(playlistId, firstChunk);

if (chunks.length > 0) {
for (const uriChunk of chunks) { // eslint-disable-line no-restricted-syntax
await api.addTracksToPlaylist(playlistId, uriChunk); // eslint-disable-line no-await-in-loop
}
}
}
}

0 comments on commit 93cad92

Please sign in to comment.