Skip to content

Instantly share code, notes, and snippets.

@jlnbuiles
Last active November 28, 2017 15:22
Show Gist options
  • Save jlnbuiles/bf0eea3dc96dba103f19b8aa07b9a95a to your computer and use it in GitHub Desktop.
Save jlnbuiles/bf0eea3dc96dba103f19b8aa07b9a95a to your computer and use it in GitHub Desktop.
Spotify Magic 🎩. A sample webtask endpoint
{
"dependencies": {
"spotify-web-api-node": "2.5.0"
}
}
var SpotifyApi = require('spotify-web-api-node');
var CLIENT_ID = 'fb1aec35307d496ca35006b97cc09ced';
var CLIENT_SECRET = '8ce3c1ca8b5c479cb5d2eec4fef7dcb9';
var REDIRECT_URI = 'https://dayspring.com.co';
var REFRESH_TOKEN = 'AQCAHij2FfDyGUp4fxODSLMmeqBcK7Hy-bvLDoqqiqLsWxLjh2VHtq6AjkOgoj5k4T6ROF5U3XXMjlCzWTNV2hZcb9gnDn8wwRvbROrVqzsvWqQ_xq03CU3PJpYvv5YqENU';
var spotifyApiClient = new SpotifyApi({
clientId : CLIENT_ID,
clientSecret : CLIENT_SECRET,
redirectUri : REDIRECT_URI,
refreshToken: REFRESH_TOKEN
});
module.exports = function (context, cb) {
var artistName = context.query.name;
if (!artistName) {
console.log('No name parameter provided');
return cb(null, `Please provide an artist name via the \'name\' URL parameter.`);
}
spotifyApiClient.refreshAccessToken()
.then(function(data) {
spotifyApiClient.setAccessToken(data.body['access_token']);
return spotifyApiClient.searchArtists(artistName);
}).then(function(searchResultsData) {
return fetchTopTracksFromSearchResultsData(searchResultsData);
}).then(function(topTracksData) {
var trackIDs = getTrackIDsFromData(topTracksData.body.tracks);
return spotifyApiClient.getAudioFeaturesForTracks(trackIDs);
}).then(function(tracksAudioFeaturesData) {
var aggregateAudioFeatures = processAudioFeaturesFromData(tracksAudioFeaturesData);
return cb(null, aggregateAudioFeatures);
}, function(err) {
console.error(err);
cb(null, `An error occured ${err.toString()}`);
});
}
/**
* @param {Object} searchResultsData The search results data
* @return {Object} The top tracks with the provided data's artist ID
*/
function fetchTopTracksFromSearchResultsData(searchResultsData) {
var artists = searchResultsData.body.artists.items;
if (artists.length > 0 && artists[0].id.length > 0) {
var topArtist = artists[0];
return spotifyApiClient.getArtistTopTracks(topArtist.id, 'US');
} else {
return Promise.resolve('Your search returned no results');
}
}
/**
* @param {Object} tracksData The track's data
* @return {Array} An array of track IDs
*/
function getTrackIDsFromData(tracksData) {
var trackIDs = [];
for (i = 0; i < tracksData.length; i++) {
if (tracksData[i].id && tracksData[i].id.length > 0) {
trackIDs.push(tracksData[i].id);
} else {
console.log(`No id for object: ${JSON.stringify(tracksData[i])}`);
}
}
return trackIDs;
}
/**
* @param {Object} audioData The aggregate tracks' audio data
* @return {Object} An aggregate data object resulting from the mean of the main track attributes
*/
function processAudioFeaturesFromData(audioData) {
var audioFeatures = audioData.body.audio_features;
if (!audioFeatures.length) {
console.log(`No tracks found for data ${audioData.body}`);
return[];
}
var overallDanceability = 0;
var overallEnergy = 0;
var overallInstrumentalness = 0;
var overallLiveness = 0;
var overallTempo = 0;
var overallSpeechiness = 0;
for (i = 0; i < audioFeatures.length; i++) {
overallDanceability += audioFeatures[i].danceability;
overallTempo += audioFeatures[i].tempo;
overallSpeechiness += audioFeatures[i].speechiness;
overallLiveness += audioFeatures[i].liveness;
overallEnergy += audioFeatures[i].energy;
overallInstrumentalness += audioFeatures[i].instrumentalness;
}
overallInstrumentalness /= audioFeatures.length;
overallDanceability /= audioFeatures.length;
overallTempo /= audioFeatures.length;
overallSpeechiness /= audioFeatures.length;
overallLiveness /= audioFeatures.length;
overallEnergy /= audioFeatures.length;
return {
overallEnergy: overallEnergy,
overallLiveness: overallLiveness,
overallTempo: overallTempo,
overallSpeechiness: overallSpeechiness,
overallDanceability: overallDanceability,
overallInstrumentalness: overallInstrumentalness
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment