Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Last active April 13, 2016 00:35
Show Gist options
  • Save loretoparisi/bda5afe7a1d39a36fdcf4c51ed3f0299 to your computer and use it in GitHub Desktop.
Save loretoparisi/bda5afe7a1d39a36fdcf4c51ed3f0299 to your computer and use it in GitHub Desktop.
It search Spotify Users having Playlists with given search terms.
// 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);
});
}
}
@loretoparisi
Copy link
Author

How to use:

var spotify=new Spotify()
var spotify.fetchUserProfiles(["hello adele", "metallica"]);

Example output

[
    [
        {
            "id": "cocolilokids",
            "name": null,
            "followers": 7,
            "url": "https://api.spotify.com/v1/users/cocolilokids"
        },
        {
            "id": "madisonsalas1-us",
            "name": "Madison Salas",
            "followers": 2,
            "url": "https://api.spotify.com/v1/users/madisonsalas1-us"
        },
        {
            "id": "1298156234",
            "name": "David Hernandez Chambi",
            "followers": 11,
            "url": "https://api.spotify.com/v1/users/1298156234"
        },
        {
            "id": "liamxbox616",
            "name": null,
            "followers": 2,
            "url": "https://api.spotify.com/v1/users/liamxbox616"
        },
        {
            "id": "12156110381",
            "name": "VenusYvon Sibal",
            "followers": null,
            "url": "https://api.spotify.com/v1/users/12156110381"
        },
        {
            "id": "ranirileyrn09",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/ranirileyrn09"
        },
        {
            "id": "cdental",
            "name": null,
            "followers": null,
            "url": "https://api.spotify.com/v1/users/cdental"
        },
        {
            "id": "lolycumbres",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/lolycumbres"
        },
        {
            "id": "224ma3dgtavwn7qwu3bem3eui",
            "name": "Edwin Armijo",
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/224ma3dgtavwn7qwu3bem3eui"
        },
        {
            "id": "22v2vasyo4uynmtjgsgzj3dya",
            "name": "Kimberly Marie Silcox",
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/22v2vasyo4uynmtjgsgzj3dya"
        },
        {
            "id": "1258109873",
            "name": "Theresa Rules",
            "followers": 2,
            "url": "https://api.spotify.com/v1/users/1258109873"
        },
        {
            "id": "bitayncl",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/bitayncl"
        },
        {
            "id": "sunuaf",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/sunuaf"
        },
        {
            "id": "21afbnfzuyc2r5jivulreu4ei",
            "name": "Michelle Kennedy",
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/21afbnfzuyc2r5jivulreu4ei"
        },
        {
            "id": "doroothy",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/doroothy"
        },
        {
            "id": "22zovtqmx7qaotgiiovyspfca",
            "name": "Shelbie Bradford",
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/22zovtqmx7qaotgiiovyspfca"
        },
        {
            "id": "svedz",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/svedz"
        },
        {
            "id": "jesandjor",
            "name": null,
            "followers": 0,
            "url": "https://api.spotify.com/v1/users/jesandjor"
        },
        {
            "id": "1268131308",
            "name": "Andrew Germundson",
            "followers": 2,
            "url": "https://api.spotify.com/v1/users/1268131308"
        },
        {
            "id": "1158973854",
            "name": "Georgia Smith-Ball",
            "followers": 4,
            "url": "https://api.spotify.com/v1/users/1158973854"
        }
    ],
    [
        {
            "id": "metallicamika",
            "name": "Mika Sjölund",
            "followers": 1318,
            "url": "https://api.spotify.com/v1/users/metallicamika"
        },
        {
            "id": "javiflames",
            "name": "Javi Salinas",
            "followers": 2149,
            "url": "https://api.spotify.com/v1/users/javiflames"
        },
        {
            "id": "dergamefan234",
            "name": null,
            "followers": 7,
            "url": "https://api.spotify.com/v1/users/dergamefan234"
        },
        {
            "id": "hif.pettersson",
            "name": null,
            "followers": 3,
            "url": "https://api.spotify.com/v1/users/hif.pettersson"
        },
        {
            "id": "1162700363",
            "name": "Marco Kurz",
            "followers": 60,
            "url": "https://api.spotify.com/v1/users/1162700363"
        },
        {
            "id": "torarneiversen",
            "name": "Tor-Arne Iversen",
            "followers": 44,
            "url": "https://api.spotify.com/v1/users/torarneiversen"
        },
        {
            "id": "12148450919",
            "name": "Travis Lowe-Farnsworth",
            "followers": 8,
            "url": "https://api.spotify.com/v1/users/12148450919"
        },
        {
            "id": "toymz",
            "name": null,
            "followers": 8,
            "url": "https://api.spotify.com/v1/users/toymz"
        },
        {
            "id": "dimmuborgir666",
            "name": "Daniel Johansson",
            "followers": 390,
            "url": "https://api.spotify.com/v1/users/dimmuborgir666"
        },
        {
            "id": "1139317407",
            "name": "Thomas Parviainen",
            "followers": 15,
            "url": "https://api.spotify.com/v1/users/1139317407"
        },
        {
            "id": "serveta-serv",
            "name": null,
            "followers": 10,
            "url": "https://api.spotify.com/v1/users/serveta-serv"
        },
        {
            "id": "12165427480",
            "name": "Brenda K Banks",
            "followers": null,
            "url": "https://api.spotify.com/v1/users/12165427480"
        },
        {
            "id": "pr0x",
            "name": null,
            "followers": null,
            "url": "https://api.spotify.com/v1/users/pr0x"
        },
        {
            "id": "hitmancodename85",
            "name": "Farshid Javid",
            "followers": 39,
            "url": "https://api.spotify.com/v1/users/hitmancodename85"
        },
        {
            "id": "11133828602",
            "name": "Jan Martyniak",
            "followers": null,
            "url": "https://api.spotify.com/v1/users/11133828602"
        },
        {
            "id": "1211998498",
            "name": "Ashley DiVenere",
            "followers": 28,
            "url": "https://api.spotify.com/v1/users/1211998498"
        },
        {
            "id": "mrventura",
            "name": "Espen Stensaas",
            "followers": 18,
            "url": "https://api.spotify.com/v1/users/mrventura"
        },
        {
            "id": "12129717824",
            "name": "Drew Niceley",
            "followers": 3,
            "url": "https://api.spotify.com/v1/users/12129717824"
        },
        {
            "id": "pmgfcaeiro",
            "name": null,
            "followers": 4,
            "url": "https://api.spotify.com/v1/users/pmgfcaeiro"
        },
        {
            "id": "115444417",
            "name": "Pedro Javier Muela García",
            "followers": null,
            "url": "https://api.spotify.com/v1/users/115444417"
        }
    ]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment