-
-
Save brigand/7630e8f9ac03f937b0a3 to your computer and use it in GitHub Desktop.
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
var Promise = require('bluebird'); // or load bluebird some other way | |
var getSoundCloudUser = function(artist, params){ | |
var get = Promise.promisify(Soundcloud.get, Soundcloud) | |
return get('/e1/users/' + artist + '/stream', params); | |
}; | |
var stream = { | |
tracks: [], | |
playlists: [] | |
}; | |
function buildStream(group) { | |
var artists = group.artists; | |
var params = { | |
limit: 5 | |
}; | |
Promise.map(artists, function(artistName){ | |
return getSoundCloudUser(artistName, params); | |
}) | |
// extract tracks and playlists for each artist | |
.map(function(items){ | |
var tracks = items.filter(function(x){ return (x.type === 'track') || (x.type === 'track_repost') }); | |
var playlists = items.filter(function(x){ return (item.type === 'playlist') || (item.type === 'playlist_repost') }); | |
return {tracks: tracks, playlists: playlists}; | |
}) | |
.then(function(sets){ | |
// get a flat list of playlists and tracks | |
var tracks = sets.reduce(function(a, b){ return a.concat(b.tracks); }, []); | |
var playlists = sets.reduce(function(a, b){ return a.concat(b.playlists); }, []); | |
return {tracks: tracks, playlists: playlists}; | |
}); | |
} | |
// usage: | |
buildStream(group).then(function(stream){ | |
// stream is in format: {tracks: [...], playlists: [...]} | |
}, function(error){ | |
// there was an error at some point (probably one of the Soundcloud.get calls) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment