2015-06-05 09:13:42 +00:00
|
|
|
|
// Class MBLinks : query MusicBrainz for urls and display links for matching urls
|
|
|
|
|
// The main method is searchAndDisplayMbLink()
|
|
|
|
|
|
|
|
|
|
// Example:
|
|
|
|
|
// $(document).ready(function () {
|
|
|
|
|
//
|
|
|
|
|
// var mblinks = new MBLinks('EXAMPLE_MBLINKS_CACHE', 7*24*60); // force refresh of cached links once a week
|
|
|
|
|
//
|
|
|
|
|
// var artist_link = 'http://' + window.location.href.match( /^https?:\/\/(.*)\/album\/.+$/i)[1];
|
|
|
|
|
// mblinks.searchAndDisplayMbLink(artist_link, 'artist', function (link) { $('div#there').before(link); } );
|
|
|
|
|
//
|
|
|
|
|
// var album_link = 'http://' + window.location.href.match( /^https?:\/\/(.*\/album\/.+)$/i)[1];
|
|
|
|
|
// mblinks.searchAndDisplayMbLink(album_link, 'release', function (link) { $('div#there').after(link); } );
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// cachekey = textual key used to store cached data in local storage
|
|
|
|
|
// expiration = time in minutes before an entry is refreshed, value <= 0 disables cache reads
|
|
|
|
|
var MBLinks = function (cachekey, expiration) {
|
|
|
|
|
this.supports_local_storage = function () {
|
|
|
|
|
try {
|
|
|
|
|
return !!localStorage.getItem;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}();
|
|
|
|
|
|
2015-06-08 20:44:39 +00:00
|
|
|
|
this.ajax_requests = {
|
|
|
|
|
// properties: "key": {handlers: [], next: property}
|
|
|
|
|
first: "",
|
|
|
|
|
last: "",
|
|
|
|
|
empty: function() {return this.first == "";},
|
|
|
|
|
push: function(key, handler) {
|
|
|
|
|
if (key in this) {
|
|
|
|
|
this[key].handlers.push(handler);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this[key] = {handlers: [handler], next: ""};
|
|
|
|
|
if (this.first == "") {
|
|
|
|
|
this.first = this.last = key;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this[this.last].next = key;
|
|
|
|
|
this.last = key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
shift: function() {
|
|
|
|
|
var key = this.first;
|
|
|
|
|
var handlers = this[key].handlers;
|
|
|
|
|
this.first = this[key].next;
|
|
|
|
|
delete this[key]; // delete this property
|
|
|
|
|
return handlers;
|
|
|
|
|
},
|
|
|
|
|
size: function() {
|
|
|
|
|
return Object.keys(this).length;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-06-05 09:13:42 +00:00
|
|
|
|
this.cache = {};
|
|
|
|
|
this.expirationMinutes = parseInt(expiration);
|
|
|
|
|
this.cache_key = cachekey;
|
2015-06-05 15:01:19 +00:00
|
|
|
|
this.mb_server = '//musicbrainz.org';
|
2015-06-05 19:57:03 +00:00
|
|
|
|
// overrides link title and img src url (per type), see createMusicBrainzLink()
|
|
|
|
|
this.type_link_info = {
|
|
|
|
|
release_group: {
|
|
|
|
|
title: 'See this release group on MusicBrainz',
|
|
|
|
|
},
|
|
|
|
|
}
|
2015-06-05 09:13:42 +00:00
|
|
|
|
|
|
|
|
|
this.initAjaxEngine = function () {
|
|
|
|
|
var ajax_requests = this.ajax_requests;
|
|
|
|
|
setInterval(function () {
|
2015-06-08 20:44:39 +00:00
|
|
|
|
if (ajax_requests.size() > 0) {
|
|
|
|
|
var requests = ajax_requests.shift();
|
|
|
|
|
if (typeof requests === "function") {
|
|
|
|
|
requests();
|
|
|
|
|
} else if (requests instanceof Array) {
|
|
|
|
|
$.each(requests, function(i, request) {
|
|
|
|
|
if (typeof request === "function") {
|
|
|
|
|
request();
|
|
|
|
|
}
|
|
|
|
|
})
|
2015-06-05 09:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, 1000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.initCache = function () {
|
|
|
|
|
if (!this.supports_local_storage) return;
|
|
|
|
|
// Check if we already added links for this content
|
2015-06-05 15:15:04 +00:00
|
|
|
|
this.cache = JSON.parse(localStorage.getItem(this.cache_key) || '{}');
|
2015-06-05 09:13:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.saveCache = function () {
|
|
|
|
|
if (!this.supports_local_storage) return;
|
|
|
|
|
try {
|
|
|
|
|
localStorage.setItem(this.cache_key, JSON.stringify(this.cache));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
alert(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2015-06-07 19:49:51 +00:00
|
|
|
|
// Search for ressource 'url' in local cache, and return the matching MBID if there's only matching MB entity.
|
|
|
|
|
// If the url is not known by the cache, no attempt will be made to request the MusicBrainz webservice, in order to keep this method synchronous.
|
|
|
|
|
this.resolveMBID = function (url) {
|
|
|
|
|
if (mblinks.cache[url]
|
|
|
|
|
&& mblinks.expirationMinutes > 0
|
|
|
|
|
&& new Date().getTime() < mblinks.cache[url].timestamp
|
|
|
|
|
&& mblinks.cache[url].urls.length == 1) {
|
|
|
|
|
return mblinks.cache[url].urls[0].slice(-36);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-05 19:50:52 +00:00
|
|
|
|
this.createMusicBrainzLink = function (mb_url, _type) {
|
2015-06-05 19:57:03 +00:00
|
|
|
|
var title = 'See this ' + _type + ' on MusicBrainz';
|
2015-06-05 19:50:52 +00:00
|
|
|
|
var img_url = this.mb_server + '/static/images/entity/' + _type + '.png';
|
2015-06-05 19:57:03 +00:00
|
|
|
|
// handle overrides
|
|
|
|
|
var ti = this.type_link_info[_type];
|
|
|
|
|
if (ti) {
|
|
|
|
|
if (ti.title) title = ti.title;
|
|
|
|
|
if (ti.img_url) img_url = ti.img_url;
|
|
|
|
|
}
|
2015-06-05 19:50:52 +00:00
|
|
|
|
return '<a href="' + mb_url + '" title="' + title + '"><img src="' + img_url + '"/></a> ';
|
2015-06-05 09:13:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-06-05 15:26:53 +00:00
|
|
|
|
// Search for ressource 'url' on MB, for relation of type 'mb_type' (artist, release, label, release-group, ...)
|
2015-06-05 09:13:42 +00:00
|
|
|
|
// and call 'insert_func' function with matching MB links (a tag built in createMusicBrainzLink) for each
|
|
|
|
|
// entry found
|
|
|
|
|
this.searchAndDisplayMbLink = function (url, mb_type, insert_func) {
|
|
|
|
|
var mblinks = this;
|
2015-06-05 19:50:52 +00:00
|
|
|
|
var _type = mb_type.replace('-', '_'); // underscored type
|
2015-06-05 09:13:42 +00:00
|
|
|
|
|
|
|
|
|
if (mblinks.cache[url]
|
|
|
|
|
&& mblinks.expirationMinutes > 0
|
|
|
|
|
&& new Date().getTime() < mblinks.cache[url].timestamp) {
|
|
|
|
|
$.each(mblinks.cache[url].urls, function (idx, mb_url) {
|
2015-06-05 19:50:52 +00:00
|
|
|
|
insert_func(mblinks.createMusicBrainzLink(mb_url, _type));
|
2015-06-05 09:13:42 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2015-06-08 20:44:39 +00:00
|
|
|
|
mblinks.ajax_requests.push(url, $.proxy(function () {
|
2015-06-05 09:13:42 +00:00
|
|
|
|
var context = this;
|
2015-06-05 15:01:19 +00:00
|
|
|
|
$.getJSON(mblinks.mb_server + '/ws/2/url?resource=' + encodeURIComponent(context.url)
|
2015-06-05 14:53:15 +00:00
|
|
|
|
+ '&inc=' + context.mb_type + '-rels',
|
2015-06-05 09:13:42 +00:00
|
|
|
|
function (data) {
|
|
|
|
|
if ('relations' in data) {
|
|
|
|
|
var expires = new Date().getTime() + (mblinks.expirationMinutes * 60 * 1000);
|
|
|
|
|
mblinks.cache[context.url] = {
|
|
|
|
|
timestamp: expires,
|
|
|
|
|
urls: []
|
|
|
|
|
};
|
|
|
|
|
$.each(data['relations'], function (idx, relation) {
|
2015-06-05 14:56:37 +00:00
|
|
|
|
if (_type in relation) {
|
2015-06-05 15:01:19 +00:00
|
|
|
|
var mb_url = mblinks.mb_server + '/' + context.mb_type + '/' + relation[_type]['id'];
|
2015-06-05 09:13:42 +00:00
|
|
|
|
if ($.inArray(mb_url, mblinks.cache[context.url].urls) == -1) { // prevent dupes
|
|
|
|
|
mblinks.cache[context.url].urls.push(mb_url);
|
2015-06-05 19:50:52 +00:00
|
|
|
|
context.insert_func(mblinks.createMusicBrainzLink(mb_url, _type));
|
2015-06-05 09:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-06-05 15:32:11 +00:00
|
|
|
|
mblinks.saveCache();
|
2015-06-05 09:13:42 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, {
|
|
|
|
|
'url': url,
|
|
|
|
|
'insert_func': insert_func,
|
|
|
|
|
'mb_type': mb_type
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.initCache();
|
|
|
|
|
this.initAjaxEngine();
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|