encylopedisque: code cleanup (#302)

This commit is contained in:
Aurélien Mino 2020-09-14 22:44:18 +02:00 committed by GitHub
parent 1f0a9681c9
commit 4b249c2dee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,6 @@
// ==UserScript== // ==UserScript==
// @name Import Encyclopedisque releases to MusicBrainz // @name Import Encyclopedisque releases to MusicBrainz
// @version 2018.2.18.1 // @version 2020.9.13.1
// @namespace http://userscripts.org/users/22504 // @namespace http://userscripts.org/users/22504
// @description Easily import Encyclopedisque releases into MusicBrainz // @description Easily import Encyclopedisque releases into MusicBrainz
// @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/encyclopedisque_importer.user.js // @downloadURL https://raw.github.com/murdos/musicbrainz-userscripts/master/encyclopedisque_importer.user.js
@ -15,7 +15,7 @@
// @icon https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/assets/images/Musicbrainz_import_logo.png // @icon https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/assets/images/Musicbrainz_import_logo.png
// ==/UserScript== // ==/UserScript==
var mblinks = new MBLinks('ENCYLOPEDISQUE_MBLINKS_CACHE'); const mbLinks = new MBLinks('ENCYLOPEDISQUE_MBLINKS_CACHE');
$(document).ready(function () { $(document).ready(function () {
MBImportStyle(); MBImportStyle();
@ -47,11 +47,11 @@ function setupImportUI(release) {
function insertMBLinks() { function insertMBLinks() {
let current_url = window.location.href; let current_url = window.location.href;
if (current_url.match(/\/disque\//)) { if (current_url.match(/\/disque\//)) {
mblinks.searchAndDisplayMbLink(current_url, 'release', function (link) { mbLinks.searchAndDisplayMbLink(current_url, 'release', function (link) {
$('h2 span').before(link); $('h2 span').before(link);
}); });
} else if (current_url.match(/\/artiste\//)) { } else if (current_url.match(/\/artiste\//)) {
mblinks.searchAndDisplayMbLink(current_url, 'artist', function (link) { mbLinks.searchAndDisplayMbLink(current_url, 'artist', function (link) {
$('h2').prepend(link); $('h2').prepend(link);
}); });
} }
@ -61,7 +61,7 @@ function insertMBLinks() {
.each(function () { .each(function () {
let $link = $(this); let $link = $(this);
let external_url = window.location.origin + $link.attr('href'); let external_url = window.location.origin + $link.attr('href');
mblinks.searchAndDisplayMbLink(external_url, 'release', function (link) { mbLinks.searchAndDisplayMbLink(external_url, 'release', function (link) {
$link.after(link).after('<br />'); $link.after(link).after('<br />');
}); });
}); });
@ -71,7 +71,7 @@ function insertMBLinks() {
.each(function () { .each(function () {
let $link = $(this); let $link = $(this);
let external_url = window.location.origin + $link.attr('href'); let external_url = window.location.origin + $link.attr('href');
mblinks.searchAndDisplayMbLink(external_url, 'artist', function (link) { mbLinks.searchAndDisplayMbLink(external_url, 'artist', function (link) {
$link.before(link); $link.before(link);
}); });
}); });
@ -79,14 +79,15 @@ function insertMBLinks() {
// Analyze Encyclopedisque data and prepare to release object // Analyze Encyclopedisque data and prepare to release object
function parseEncyclopedisquePage() { function parseEncyclopedisquePage() {
release = new Object(); const release = {
labels: [],
};
let infoHeader = document.body.querySelector('#contenu > h2:nth-of-type(1)'); let infoHeader = document.body.querySelector('#contenu > h2:nth-of-type(1)');
// Release artist credit // Release artist credit
release.artist_credit = new Array();
let artist_name = infoHeader.querySelector('div.floatright:nth-of-type(1)').textContent.trim(); let artist_name = infoHeader.querySelector('div.floatright:nth-of-type(1)').textContent.trim();
release.artist_credit.push({ artist_name: artist_name }); release.artist_credit = [{ artist_name: artist_name }];
// Release title // Release title
release.title = infoHeader.querySelector('span:nth-of-type(1)').textContent.trim(); release.title = infoHeader.querySelector('span:nth-of-type(1)').textContent.trim();
@ -102,117 +103,78 @@ function parseEncyclopedisquePage() {
release.discs = [disc]; release.discs = [disc];
// Release URL // Release URL
release.urls = new Array(); release.urls = [{ url: window.location.href, link_type: MBImport.URL_TYPES.other_databases }];
release.urls.push({ url: window.location.href, link_type: MBImport.URL_TYPES.other_databases });
// Parse other infos // Parse other infos
let releaseInfos = document.body.querySelectorAll('div.main tr'); let lastMediumSide = '';
let lastVinylFace = ''; let lastInfoType = undefined;
let lastInfoType; document.body.querySelectorAll('div.main tr').forEach(releaseInfo => {
for (let i = 0; i < releaseInfos.length; i++) { let infoType = releaseInfo.querySelector('td:nth-of-type(1)').textContent.trim();
let infoType = releaseInfos[i].querySelector('td:nth-of-type(1)').textContent.trim();
// Release date // Release date
if (infoType == 'Sortie :') { if (infoType === 'Sortie :') {
var infoValue = releaseInfos[i].querySelector('td:nth-of-type(2)').textContent.trim(); const infoValue = releaseInfo.querySelector('td:nth-of-type(2)').textContent.trim();
let re = /\s*(janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|décembre)?\s*([\d\?]{4})?\s*(?:chez)?\s*((?:\S+\s?)*)\s*\(?([^\)]*)?\)?/; const releaseRegexp = /\s*(janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|décembre)?\s*([\d?]{4})?\s*(?:chez)?\s*((?:\S+\s?)*)\s*\(?([^)]*)?\)?/;
m = infoValue.match(re); const m = infoValue.match(releaseRegexp);
month = m[1]; if (m[1] !== undefined) {
if (month != undefined) { release.month = MONTHS[m[1]];
switch (month) {
case 'janvier':
release.month = 1;
break;
case 'février':
release.month = 2;
break;
case 'mars':
release.month = 3;
break;
case 'avril':
release.month = 4;
break;
case 'mai':
release.month = 5;
break;
case 'juin':
release.month = 6;
break;
case 'juillet':
release.month = 7;
break;
case 'août':
release.month = 8;
break;
case 'septembre':
release.month = 9;
break;
case 'octobre':
release.month = 10;
break;
case 'novembre':
release.month = 11;
break;
case 'décembre':
release.month = 12;
break;
}
} }
release.year = m[2]; release.year = m[2];
release.labels = []; const labels = m[3];
let labels = m[3]; if (labels !== undefined) {
if (labels != undefined) { labels.split('/').forEach(label => release.labels.push({ name: label.trim(), catno: m[4] }));
$.each(labels.split('/'), function (index, label) {
release.labels.push({ name: label.trim(), catno: m[4] });
});
} else { } else {
release.labels.push({ catno: m[4] }); release.labels.push({ catno: m[4] });
} }
} else if (infoType.match(/^Face [A-Z]/) || (infoType == '' && lastInfoType.match(/^Face [A-Z]/))) { } else if (infoType.match(/^Face [A-Z]/) || (infoType === '' && lastInfoType !== undefined && lastInfoType.match(/^Face [A-Z]/))) {
// Tracks // Tracks
let track = new Object(); let track = {};
// First part of tracknumber (A, B, ...) // First part of tracknumber (A, B, ...)
let tnum_part1 = ''; let mediumSide;
if ((m = infoType.match(/^Face ([A-Z])/))) { let m = infoType.match(/^Face ([A-Z])/);
lastVinylFace = m[1]; if (m != null) {
tnum_part1 = m[1]; lastMediumSide = m[1];
mediumSide = m[1];
} else { } else {
tnum_part1 = lastVinylFace; mediumSide = lastMediumSide;
} }
// Track title // Track title
if (releaseInfos[i].querySelector('td:nth-of-type(2) em') == null) { if (releaseInfo.querySelector('td:nth-of-type(2) em') == null) {
continue; return;
} }
let title = releaseInfos[i].querySelector('td:nth-of-type(2) em').textContent.trim(); let title = releaseInfo.querySelector('td:nth-of-type(2) em').textContent.trim();
// 2nd part of tracknumber (1, 2, ...) // 2nd part of tracknumber (1, 2, ...)
let tnum_part2 = ''; let trackNumber = '';
if ((m = infoType.match(/^Face [A-Z](\d+)/))) { m = infoType.match(/^Face [A-Z](\d+)/);
tnum_part2 = m[1]; if (m !== null) {
} else if ((m = title.match(/^(\d+)\.\s+(.*)$/))) { trackNumber = m[1];
tnum_part2 = m[1]; } else {
title = m[2]; m = title.match(/^(\d+)\.\s+(.*)$/);
if (m !== null) {
trackNumber = m[1];
title = m[2];
}
} }
// Track length // Track length
if ( m = releaseInfo
(m = releaseInfos[i] .querySelector('td:nth-of-type(2)')
.querySelector('td:nth-of-type(2)') .textContent.trim()
.textContent.trim() .match(/- (\d+)(\d+)$/);
.match(/- (\d+)(\d+)$/)) if (m !== null) {
) {
track.duration = `${m[1]}:${m[2]}`; track.duration = `${m[1]}:${m[2]}`;
} }
track.number = tnum_part1 + tnum_part2; track.number = mediumSide + trackNumber;
track.title = title; track.title = title;
disc.tracks.push(track); disc.tracks.push(track);
} else if (infoType == 'Format :') { } else if (infoType === 'Format :') {
// Format => medium format, release-group type, release status // Format => medium format, release-group type, release status
var infoValue = releaseInfos[i].querySelector('td:nth-of-type(2)').textContent.trim(); const infoValue = releaseInfo.querySelector('td:nth-of-type(2)').textContent.trim();
let values = infoValue.split(' / '); const values = infoValue.split(' / ');
values.forEach(function (value) { values.forEach(function (value) {
if (value.indexOf('45 tours') > -1) { if (value.indexOf('45 tours') > -1) {
disc.format = '7" Vinyl'; disc.format = '7" Vinyl';
@ -233,24 +195,24 @@ function parseEncyclopedisquePage() {
release.status = 'promotion'; release.status = 'promotion';
} }
}); });
} else if (infoType == 'Pays :') { } else if (infoType === 'Pays :') {
// Country // Country
var infoValue = releaseInfos[i].querySelector('td:nth-of-type(2)').textContent.trim(); const infoValue = releaseInfo.querySelector('td:nth-of-type(2)').textContent.trim();
if (infoValue == 'France') { if (infoValue === 'France') {
release.country = 'FR'; release.country = 'FR';
} else if (infoValue == 'Royaume-uni') { } else if (infoValue === 'Royaume-uni') {
release.country = 'UK'; release.country = 'UK';
} else if (infoValue == 'Allemagne') { } else if (infoValue === 'Allemagne') {
release.country = 'DE'; release.country = 'DE';
} else if (infoValue == 'Belgique') { } else if (infoValue === 'Belgique') {
release.country = 'BE'; release.country = 'BE';
} }
} }
if (infoType != '') { if (infoType !== '') {
lastInfoType = infoType; lastInfoType = infoType;
} }
} });
// Barcode ? // Barcode ?
if (parseInt(release.year, 10) <= 1982) { if (parseInt(release.year, 10) <= 1982) {
@ -262,3 +224,18 @@ function parseEncyclopedisquePage() {
return release; return release;
} }
const MONTHS = {
janvier: 1,
février: 2,
mars: 3,
avril: 4,
mai: 5,
juin: 6,
juillet: 7,
août: 8,
septembre: 9,
octobre: 10,
novembre: 11,
décembre: 12,
};