Skip to content

Instantly share code, notes, and snippets.

@brigand
Forked from hedgerh/buildStream.js
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brigand/7630e8f9ac03f937b0a3 to your computer and use it in GitHub Desktop.
Save brigand/7630e8f9ac03f937b0a3 to your computer and use it in GitHub Desktop.
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