encyclopedique: use mblinks and clean code.

This commit is contained in:
Aurélien Mino 2015-06-15 23:05:59 +02:00
parent 4bd4b74c7f
commit f9b4b1e51a

View file

@ -1,6 +1,6 @@
// ==UserScript==
// ==UserScript==
// @name Import Encyclopedisque releases to MusicBrainz
// @version 2015.06.14.0
// @version 2015.06.15.0
// @namespace http://userscripts.org/users/22504
// @description Easily import Encyclopedisque releases into MusicBrainz
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/encyclopedisque_importer.user.js
@ -9,20 +9,22 @@
// @include http://www.encyclopedisque.fr/artiste/*.html
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// @require lib/import_functions.js
// @require lib/mblinks.js
// @require lib/logger.js
// @require lib/mbimportstyle.js
// ==/UserScript==
var mblinks = new MBLinks('ENCYLOPEDISQUE_MBLINKS_CACHE', 7*24*60); // force refresh of cached links once a week
$(document).ready(function() {
MBImportStyle();
if (window.location.href.match( /encyclopedisque\.fr\/disque\/(\d+)/) ) {
if (window.location.href.match(/encyclopedisque\.fr\/disque\/(\d+)/)) {
var release = parseEncyclopedisquePage();
setupUI(release);
setupImportUI(release);
}
insertMBLinks();
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -30,7 +32,7 @@ $(document).ready(function() {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function setupUI(release) {
function setupImportUI(release) {
// Form parameters
var edit_note = 'Imported from ' + window.location.href;
@ -47,70 +49,14 @@ function setupUI(release) {
}
function insertMBLinks($root) {
function insertMBLinks() {
// Check if we already added links for this content
var CACHE_STRING = localStorage.getItem('ENCYCLOPEDISQUE_MB_MAPPING_CACHE');
if(!CACHE_STRING) {
CACHE_STRING = "{}";
}
var CACHE = JSON.parse(CACHE_STRING);
var ajax_requests = [];
setInterval(function() {
if(ajax_requests.length > 0) {
var request = ajax_requests.shift();
if(typeof request === "function") {
request();
}
}
}, 1000);
function createLink(mb_url) {
return '<a href="'+mb_url+'"><img src="http://musicbrainz.org/favicon.ico" /></a> ';
}
function searchAndDisplayMbLink($div) {
$div.find('a[href*="/disque/"]').each(function() {
$('body').find('div.v7P, div.v12P').each(function() {
$(this).find('a[href*="/disque/"]').each(function() {
var $link = $(this);
var external_url = 'http://www.encyclopedisque.fr' + $link.attr('href');
if(CACHE[external_url]) {
$.each(CACHE[external_url], function(index, mb_url) {
$link.after(createLink(mb_url)).after('<br />');
});
} else {
ajax_requests.push($.proxy(function() {
var context = this;
$.getJSON('http://musicbrainz.org/ws/2/url?resource='+context.external_url+'&inc=release-rels', function(data) {
if ('relations' in data) {
CACHE[context.external_url] = [];
$.each(data['relations'], function(idx, relation) {
if ('release'.replace('-', '_') in relation) {
var mb_url = 'http://musicbrainz.org/release/' + relation['release']['id'];
CACHE[context.external_url].push(mb_url);
localStorage.setItem('ENCYCLOPEDISQUE_MB_MAPPING_CACHE', JSON.stringify(CACHE));
context.$link.after(createLink(mb_url)).after('<br />');
}
});
}
});
}, {'external_url': external_url, '$link': $link}));
}
mblinks.searchAndDisplayMbLink(external_url, 'release', function (link) { $link.after(link).after('<br />') } );
});
}
if (!$root) {
$root = $('body');
}
$root.find('div.v7P').each(function() {
searchAndDisplayMbLink($(this));
});
$root.find('div.v12P').each(function() {
searchAndDisplayMbLink($(this));
});
}