Skip to content

Instantly share code, notes, and snippets.

@gilbert
Created December 25, 2014 02:54
Show Gist options
  • Save gilbert/699b7a55523305cc90c9 to your computer and use it in GitHub Desktop.
Save gilbert/699b7a55523305cc90c9 to your computer and use it in GitHub Desktop.
Spotify API with Mithril.js Promises
// 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