musicbrainz-userscripts/deezer_importer.user.js

157 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-01-30 12:49:20 +00:00
// ==UserScript==
// @name Import Deezer releases into MusicBrainz
// @namespace https://github.com/murdos/musicbrainz-userscripts/
// @description One-click importing of releases from deezer.com into MusicBrainz
// @version 2019.1.30.1
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/deezer_importer.user.js
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/deezer_importer.user.js
// @include http*://www.deezer.com/*/album/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require lib/mbimport.js
// @require lib/logger.js
// @require lib/mbimportstyle.js
// @icon https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/assets/images/Musicbrainz_import_logo.png
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// ==/UserScript==
// prevent JQuery conflicts, see http://wiki.greasespot.net/@grant
this.$ = this.jQuery = jQuery.noConflict(true);
2020-04-05 14:01:21 +00:00
$(document).ready(function () {
2019-01-30 12:49:20 +00:00
let gmXHR;
if (typeof GM_xmlhttpRequest != 'undefined') {
gmXHR = GM_xmlhttpRequest;
} else if (GM.xmlHttpRequest != 'undefined') {
gmXHR = GM.xmlHttpRequest;
} else {
LOGGER.error('Userscript requires GM_xmlHttpRequest or GM.xmlHttpRequest');
return;
}
// allow 1 second for Deezer SPA to initialize
2020-04-05 14:01:21 +00:00
window.setTimeout(function () {
2019-01-30 12:49:20 +00:00
MBImportStyle();
let releaseUrl = window.location.href.replace(/\?.*$/, '').replace(/#.*$/, '');
let releaseId = releaseUrl.replace(/^https?:\/\/www\.deezer\.com\/[^/]+\/album\//i, '');
let deezerApiUrl = `https://api.deezer.com/album/${releaseId}`;
gmXHR({
method: 'GET',
url: deezerApiUrl,
2020-04-05 14:01:21 +00:00
onload: function (resp) {
2019-01-30 12:49:20 +00:00
try {
let release = parseDeezerRelease(releaseUrl, JSON.parse(resp.responseText));
insertLink(release, releaseUrl);
} catch (e) {
LOGGER.error('Failed to parse release: ', e);
}
},
2020-04-05 14:01:21 +00:00
onerror: function (resp) {
2019-01-30 12:49:20 +00:00
LOGGER.error('AJAX status:', resp.status);
LOGGER.error('AJAX response:', resp.responseText);
2020-04-05 14:01:21 +00:00
},
2019-01-30 12:49:20 +00:00
});
}, 1000);
});
function parseDeezerRelease(releaseUrl, data) {
let releaseDate = data.release_date.split('-');
let release = {
artist_credit: [],
title: data.title,
year: releaseDate[0],
month: releaseDate[1],
day: releaseDate[2],
packaging: 'None',
country: 'XW',
status: 'official',
language: 'eng',
script: 'Latn',
type: '',
urls: [],
labels: [],
2020-04-05 14:01:21 +00:00
discs: [],
2019-01-30 12:49:20 +00:00
};
2020-04-05 14:01:21 +00:00
$.each(data.contributors, function (index, artist) {
2019-01-30 12:49:20 +00:00
if (artist.role != 'Main') return true;
let ac = {
artist_name: artist.name,
2020-04-05 14:01:21 +00:00
joinphrase: index == data.contributors.length - 1 ? '' : ', ',
2019-01-30 12:49:20 +00:00
};
if (artist.name == 'Various Artists') {
ac = MBImport.specialArtist('various_artists', ac);
}
release.artist_credit.push(ac);
});
let disc = {
format: 'Digital Media',
title: '',
2020-04-05 14:01:21 +00:00
tracks: [],
2019-01-30 12:49:20 +00:00
};
2020-04-05 14:01:21 +00:00
$.each(data.tracks.data, function (index, track) {
2019-01-30 12:49:20 +00:00
let t = {
number: index + 1,
title: track.title_short,
duration: track.duration * 1000,
2020-04-05 14:01:21 +00:00
artist_credit: [],
2019-01-30 12:49:20 +00:00
};
// ignore pointless "(Original Mix)" in title version
if (track.title_version && !track.title_version.match(/^\s*\(Original Mix\)\s*$/i)) {
t.title += ` ${track.title_version}`;
}
t.artist_credit.push({ artist_name: track.artist.name });
disc.tracks.push(t);
});
release.discs.push(disc);
release.urls.push({
link_type: MBImport.URL_TYPES.stream_for_free,
2020-04-05 14:01:21 +00:00
url: releaseUrl,
2019-01-30 12:49:20 +00:00
});
release.labels.push({ name: data.label });
release.type = data.record_type;
release.barcode = data.upc;
return release;
}
2023-01-11 17:47:52 +00:00
function waitForEl(selector, callback) {
if (jQuery(selector).length) {
callback();
} else {
setTimeout(function () {
waitForEl(selector, callback);
}, 100);
}
}
2019-01-30 12:49:20 +00:00
function insertLink(release, release_url) {
let editNote = MBImport.makeEditNote(release_url, 'Deezer');
let parameters = MBImport.buildFormParameters(release, editNote);
let mbUI = $(
`<div class="toolbar-item">
${MBImport.buildFormHTML(parameters)}
</div><div class="toolbar-item">
${MBImport.buildSearchButton(release)}
</div>`
).hide();
2023-01-11 17:47:52 +00:00
waitForEl('[data-testid="toolbar"]', function () {
$('[data-testid="toolbar"]').append(mbUI);
mbUI.show();
});
2019-01-30 12:49:20 +00:00
}