Skip to content

Instantly share code, notes, and snippets.

@mager
Created February 25, 2012 22:10
Show Gist options
  • Save mager/1911137 to your computer and use it in GitHub Desktop.
Save mager/1911137 to your computer and use it in GitHub Desktop.
Spotify Apps API - Get the top five tracks from five artists (JS)
/* Instantiate the global sp object; include models & views */
var sp = getSpotifyApi(1);
var models = sp.require("sp://import/scripts/api/models");
var views = sp.require("sp://import/scripts/api/views");
var artists = ['Frank Sinatra', 'Drake', 'T-Pain', 'The Lonely Island', 'Avan Lava'];
var playlist = [];
var done = 0;
for(var i=0; i<artists.length; i++){
var search = new models.Search('artist:"'+artists[i]+'"');
search.localResults = models.LOCALSEARCHRESULTS.IGNORE;
search.searchPlaylists = false;
search.searchAlbums = false;
search.pageSize = 5;
search.observe(models.EVENT.CHANGE, function(result) {
result.tracks.forEach(function(track) {
playlist.push(track);
});
done++;
if (done == artists.length) {
console.log(playlist);
}
});
search.appendNext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment