Last active
April 13, 2016 00:35
-
-
Save loretoparisi/bda5afe7a1d39a36fdcf4c51ed3f0299 to your computer and use it in GitHub Desktop.
It search Spotify Users having Playlists with given search terms.
This file contains hidden or 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
// Simple XMLHttpRequest | |
// based on https://davidwalsh.name/xmlhttprequest | |
var SimpleRequest = { | |
call: function(what, response) { | |
var request; | |
if (window.XMLHttpRequest) { // Mozilla, Safari, ... | |
request = new XMLHttpRequest(); | |
} else if (window.ActiveXObject) { // IE | |
try { | |
request = new ActiveXObject('Msxml2.XMLHTTP'); | |
} catch (e) { | |
try { | |
request = new ActiveXObject('Microsoft.XMLHTTP'); | |
} catch (e) {} | |
} | |
} | |
// state changes | |
request.onreadystatechange = function() { | |
if (request.readyState === 4) { // done | |
if (request.status === 200) { // complete | |
response(request.responseText) | |
} else response(); | |
} | |
} | |
request.open('GET', what, true); | |
request.send(null); | |
} | |
} | |
//PromiseAll | |
var promiseAll = function(items, block, done, fail) { | |
var self = this; | |
var promises = [], | |
index = 0; | |
items.forEach(function(item) { | |
promises.push(function(item, i) { | |
return new Promise(function(resolve, reject) { | |
if (block) { | |
block.apply(this, [item, index, resolve, reject]); | |
} | |
}); | |
}(item, ++index)) | |
}); | |
Promise.all(promises).then(function AcceptHandler(results) { | |
if (done) done(results); | |
}, function ErrorHandler(error) { | |
if (fail) fail(error); | |
}); | |
}; //promiseAll | |
Spotify = function() { | |
this.fetchUserProfiles = function(searchTerms) { | |
// LP: deferred execution block | |
var ExecutionBlock = function(item, index, resolve, reject) { | |
var url = "https://api.spotify.com/v1/" | |
url += item; | |
SimpleRequest.call(url, function(result) { | |
if (result) { | |
var profileUrls = JSON.parse(result).playlists.items.map(function(item, index) { | |
return item.owner.href; | |
}) | |
resolve(profileUrls); | |
} else { | |
reject(new Error("call error")); | |
} | |
}) | |
} | |
var arr = searchTerms.map(function(item, index) { | |
return "search?type=playlist&q=" + item; | |
}); | |
promiseAll(arr, function(item, index, resolve, reject) { | |
console.log("Making request [" + index + "]") | |
ExecutionBlock(item, index, resolve, reject); | |
}, function(results) { // aggregated results | |
console.log("All profiles received " + results.length); | |
//console.log(JSON.stringify(results[0], null, 2)); | |
///// promiseall again | |
var ExecutionProfileBlock = function(item, index, resolve, reject) { | |
SimpleRequest.call(item, function(result) { | |
if (result) { | |
var obj = JSON.parse(result); | |
resolve({ | |
id: obj.id, | |
name: obj.display_name, | |
followers: obj.followers.total, | |
url: obj.href | |
}); | |
} //result | |
}) | |
} //ExecutionProfileBlock | |
promiseAll(results, function(item, index, resolve, reject) { | |
promiseAll(item, function(item, index, resolve, reject) { | |
ExecutionProfileBlock(item, index, resolve, reject); | |
}, function(results) { // aggregated results | |
resolve(results) | |
} | |
, | |
function(error) { // error | |
console.log(error); | |
}) | |
}, function(results) { // aggregated results | |
console.log("All response received " + results.length); | |
console.log(JSON.stringify(results, null, 2)); | |
} | |
, | |
function(error) { // error | |
console.log(error); | |
}) | |
///// | |
}, | |
function(error) { // error | |
console.log(error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
Example output