Fetch samples from Last.fm
JavaScript
// Author: Oskar Krawczyk (nouincolor.com)
// Let's create a class that takes care of YQL requests
Request.YQL = new Class({
Extends: Request.JSONP,
options: {
url: 'http://query.yahooapis.com/v1/public/yql?q=',
data: {
format: 'json'
}
},
initialize: function(options) {
this.setOptions(options);
this.options.url = this.options.url + encodeURIComponent(this.options.query);
this.running = false;
this.requests = 0;
this.triesRemaining = [];
}
});
var MucisSamples = new Class({
Implements: [Options, Events],
options: {},
initialize: function(options) {
this.setOptions(options);
this.urls = [];
this.setup();
},
setup: function() {
this.requestSamples();
},
requestSamples: function() {
this.yqlQuery = "select * from html where url=\"http://www.last.fm/music/Hands/Give+Me+Rest\" and xpath='//table[@id=\"albumTracklist\"]'".substitute({});
new Request.YQL({
query: this.yqlQuery,
onSuccess: this.findSamples.bind(this)
}).send();
},
findSamples: function(tracks) {
var fileList = tracks.query.results['table'];
if (fileList) {
fileList['tbody']['tr'].each(function(el, index) {
if (this.urls.length < 1) {
if (el['td'][1]['a']) {
this.urls.push(el['td'][1]['a'].href);
}
}
if (index === 1) {
this.getFileUrl();
}
}, this);
}
},
getFileUrl: function() {
this.urls.each(function(url) {
new Request.YQL({
query: "select * from html where url=\"http://www.last.fm{url}\" and xpath='//a[contains(concat(\" \", normalize-space(@class), \" \"), \"previewbutton\")]'".substitute({
url: url
}),
...