-
Notifications
You must be signed in to change notification settings - Fork 92
/
deezer_importer.user.js
156 lines (134 loc) · 4.89 KB
/
deezer_importer.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// ==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);
$(document).ready(function () {
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
window.setTimeout(function () {
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,
onload: function (resp) {
try {
let release = parseDeezerRelease(releaseUrl, JSON.parse(resp.responseText));
insertLink(release, releaseUrl);
} catch (e) {
LOGGER.error('Failed to parse release: ', e);
}
},
onerror: function (resp) {
LOGGER.error('AJAX status:', resp.status);
LOGGER.error('AJAX response:', resp.responseText);
},
});
}, 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: [],
discs: [],
};
$.each(data.contributors, function (index, artist) {
if (artist.role != 'Main') return true;
let ac = {
artist_name: artist.name,
joinphrase: index == data.contributors.length - 1 ? '' : ', ',
};
if (artist.name == 'Various Artists') {
ac = MBImport.specialArtist('various_artists', ac);
}
release.artist_credit.push(ac);
});
let disc = {
format: 'Digital Media',
title: '',
tracks: [],
};
$.each(data.tracks.data, function (index, track) {
let t = {
number: index + 1,
title: track.title_short,
duration: track.duration * 1000,
artist_credit: [],
};
// 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,
url: releaseUrl,
});
release.labels.push({ name: data.label });
release.type = data.record_type;
release.barcode = data.upc;
return release;
}
function waitForEl(selector, callback) {
if (jQuery(selector).length) {
callback();
} else {
setTimeout(function () {
waitForEl(selector, callback);
}, 100);
}
}
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();
waitForEl('[data-testid="toolbar"]', function () {
$('[data-testid="toolbar"]').append(mbUI);
mbUI.show();
});
}