Skip to content

Commit

Permalink
fix: šŸ› Playlist analysis now supports playlists with more than 100 trā€¦
Browse files Browse the repository at this point in the history
ā€¦acks
  • Loading branch information
EricLambrecht committed Feb 12, 2019
1 parent 94241ab commit a99619e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/store/editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default {

async replaceTracks({ dispatch, state }, uris) {
try {
await Spotify.replaceTracksInPlaylist(state.playlist.id, uris);
await Spotify.replaceTracks(state.playlist.id, uris);
await dispatch('fetchPlaylist', state.playlist.id);
} catch (err) {
Spotify.handleApiError(dispatch, err);
Expand Down
6 changes: 3 additions & 3 deletions src/store/playlist-statistics/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Spotify, { api } from '../../utils/Spotify';
import Spotify from '../../utils/Spotify';

export default {
async fetchPlaylistAudioFeatures({
Expand All @@ -7,8 +7,8 @@ export default {
commit('setFetching', true);
try {
const { 'editor/playlistIds': playlistIds } = rootGetters;
const result = await api.getAudioFeaturesForTracks(playlistIds);
commit('setPlaylistAudioFeatures', result.audio_features);
const audioFeatures = await Spotify.getAudioFeaturesForTracks(playlistIds);
commit('setPlaylistAudioFeatures', audioFeatures);
} catch (err) {
Spotify.handleApiError(dispatch, err);
} finally {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/Spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ export default class Spotify {
return playlist;
}

/**
* Fetches audio features for a playlist. Supports playlists with more than 100 songs.
* @param {Array.<String>} trackIds
* @returns {Array}
*/
static async getAudioFeaturesForTracks(trackIds) {
const chunks = chunk(trackIds, 100);
const audioFeatures = [];

for (const trackIdChunk of chunks) { // eslint-disable-line no-restricted-syntax
const response = await api.getAudioFeaturesForTracks(trackIdChunk); // eslint-disable-line
audioFeatures.push(...response.audio_features);
}

return audioFeatures;
}

/**
* This function returns an array of tracks that match the search query
* @param query
Expand Down

0 comments on commit a99619e

Please sign in to comment.