Skip to content

Instantly share code, notes, and snippets.

@thomxc
Last active August 29, 2015 13:57
Show Gist options
  • Save thomxc/9809606 to your computer and use it in GitHub Desktop.
Save thomxc/9809606 to your computer and use it in GitHub Desktop.
A Pen by Thomas Wiersema.
Headphones Last.fm auto update
-----
var ajaxRequest = {
/**
* Flickr URL that will give us lots and lots of whatever we're looking for.
*
* See http://www.flickr.com/services/api/flickr.photos.search.html for
* details about the construction of this URL.
*
* @type {string}
* @private
*/
limit : 1,
lastfm_url : "http://ws.audioscrobbler.com/2.0/?method=library.getartists&api_key=42d6d83c5046d8db9f97475b3ab169da&user=thomx12&format=json&limit=",
hp_find_url: "http://musicbrainz.org/ws/2/artist?fmt=json&query=",
hp_add_url: "http://alarmpi:9091/api?apikey=4f9bfafa41775ec9ca62d9dae0e9b7ca&cmd=addArtist&id=",
hp_index_url: "http://alarmpi:9091/api?apikey=4f9bfafa41775ec9ca62d9dae0e9b7ca&cmd=getIndex",
artistList : undefined,
process: function(data)
{
console.log(data);
ajaxRequest.request(ajaxRequest.hp_index_url, function(artistList)
{
ajaxRequest.artistList = artistList;
ajaxRequest.recursiveAdd(data.artists.artist, 0);
});
},
recursiveAdd: function(data, index) {
if(index >= data.length) {
document.write("<p>done!</p>");
return;
}
if(ajaxRequest.isNotAlreadyAdded(ajaxRequest.artistList, data[index].mbid)) {
ajaxRequest.request(ajaxRequest.hp_add_url + data[index].mbid, function()
{
document.write("<p style='color: green'>toegevoegd: " + data[index].name + "</p>" );
console.log("added. adding next in line..");
ajaxRequest.recursiveAdd(data, index + 1);
});
} else {
document.write("<p style='color: cyan'>is al eerder toegevoegd: " + data[index].name + "</p>" );
ajaxRequest.recursiveAdd(data, index + 1);
}
},
/**
* Sends an XHR GET request to grab photos of lots and lots of kittens. The
* XHR's 'onload' event is hooks up to the 'showPhotos_' method.
*
* @public
*/
request: function(url, callback) {
$.ajax({
type : 'GET',
url : url,
dataType : 'jsonp',
}).success(callback)
.error(function (data) { console.log("fout, url: " + url); console.log(data);})
.done(function(data) { console.log(data)});
},
xhrRequest: function(url, callback) {
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.onload = callback.bind(this);
req.send(null);
},
isNotAlreadyAdded: function(haystack, needle) {
for(var index in haystack) {
if(needle == haystack[index].ArtistID)
return false;
}
return true;
}
};
ajaxRequest.request(ajaxRequest.lastfm_url + "0", ajaxRequest.process);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment