Created
December 25, 2014 02:54
-
-
Save gilbert/699b7a55523305cc90c9 to your computer and use it in GitHub Desktop.
Spotify API with Mithril.js Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use like the following: | |
// fetchArtistProfiles(names).then(vm.artist_profiles) | |
function fetchArtistProfiles (artistNames) { | |
var promises = artistNames.map(function (name) { | |
return fetchArtist(name) | |
.then(fetchTracks) | |
.then(buildProfile.curry(name)) | |
}) | |
return m.sync(promises) | |
} | |
function fetchArtist (name) { | |
var artistSearchUrl = 'https://api.spotify.com/v1/search?q=' + name + '&type=artist'; | |
return m.request({url: artistSearchUrl, method: 'GET'}).then(function(artistResults) { | |
return artistResults.artists.items[0] | |
}) | |
} | |
function fetchTracks (artist) { | |
var artistTopTracksUrl = 'https://api.spotify.com/v1/artists/' + artist.id + '/top-tracks?country=US' | |
return m.request({url: artistTopTracksUrl, method: 'GET'}).then(function(trackResults) { | |
return trackResults.tracks | |
}) | |
} | |
function buildProfile (name, tracks) { | |
var topThree = tracks.slice(0, 3) | |
return { name: name, tracks: topThree } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment