forked from murdos/musicbrainz-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgmdb_importer.user.js
327 lines (285 loc) · 9.78 KB
/
vgmdb_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// ==UserScript==
// @name Import VGMdb releases into MusicBrainz
// @namespace https://github.com/murdos/musicbrainz-userscripts/
// @description One-click importing of releases from vgmdb.net into MusicBrainz
// @version 2021.11.12.1
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/vgmdb_importer.user.js
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/master/vgmdb_importer.user.js
// @include /^https://vgmdb.net/(album|artist|org)/\d+/
// @require https://code.jquery.com/jquery-3.5.1.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
// ==/UserScript==
$(document).ready(function () {
MBImportStyle();
MBSearchItStyle();
let apiUrl = window.location.href.replace('net', 'info').concat('', '?format=json');
GM.xmlHttpRequest({
method: 'GET',
url: apiUrl,
onload: function (resp) {
const release = parseApi(resp.responseText);
insertButtons(release);
},
});
});
function parseApi(apiResponse) {
const apiDict = JSON.parse(apiResponse);
const releaseDate = mapDate(apiDict.release_date);
const release = {
title: apiDict.name,
artist_credit: [],
status: mapStatus(apiDict.publish_format),
year: releaseDate.year,
month: releaseDate.month,
day: releaseDate.day,
labels: [{ name: mapLabel(apiDict.organizations), catno: apiDict.catalog }],
barcode: apiDict.barcode,
urls: mapUrls(apiDict.vgmdb_link, apiDict.stores, apiDict.websites),
discs: mapDiscs(apiDict.discs, apiDict.media_format),
};
return release;
}
function insertButtons(release) {
const editNote = MBImport.makeEditNote(window.location.href, 'VGMdb');
console.log(editNote);
const parameters = MBImport.buildFormParameters(release, editNote);
const formHtml = $(MBImport.buildFormHTML(parameters)).attr('style', 'margin: 5px 0 0 5px; display: inline-block').prop('outerHTML');
const linkHtml = $(MBImport.buildSearchButton(release)).attr('style', 'margin: 5px 0 0 5px; display: inline-block').prop('outerHTML');
const vgmdbHtml =
'<div style="width: 250px; background-color: #1B273D">' +
'<b class="rtop"><b></b></b>' +
'<div style="padding: 6px 10px 0px 10px">' +
'<h3>MusicBrainz</h3>' +
'</div>' +
'</div>' +
`<div style="width: 250px; background-color: #2F364F;">${formHtml}${linkHtml}` +
'<b class="rbot"><b></b></b> ' +
'</div>' +
'<br style="clear: left" />';
$('#rightcolumn').prepend(vgmdbHtml);
}
/*
* Returns MusicBrainz status based on VGMdb publish_format.
*
* MusicBrainz: official, promotion, bootleg, pseudo
* VGMdb, comma separated:
* * one of Commercial, Doujin/Indie, Bootleg
* * one of Limited Edition, Enclosure, First Press Bonus, Preorder Bonus,
* Retailer Bonus, Event Only, Promo/Gift/Reward, Rental (General does not appear in API)
*/
function mapStatus(publishFormat) {
if (publishFormat.includes('Bootleg')) {
// Overrides promo
return 'bootleg';
} else if (publishFormat.includes('Promo/Gift/Reward')) {
return 'promotion';
} else {
return 'official';
}
}
/*
* Returns year, month and day dict based on ISO 8601 date string.
*/
function mapDate(releaseDate) {
const d = new Date(releaseDate);
return { year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate() };
}
/*
* Returns a likely label based on a list of VGMdb organizations.
*/
function mapLabel(organizations) {
const labelOrganization = getLabelOrganization(organizations);
if (labelOrganization) {
return labelOrganization['names']['en'];
} else if (organizations.length == 1) {
// If only one, assume that's the one
return organizations[0]['names']['en'];
} else {
return null;
}
}
/*
* Returns a list of MusicBrainz URLs based on the websites, stores and
* vgmdb_link values of the VGMdb API.
*/
function mapUrls(vgmdbLink, stores, websites) {
const urls = [];
urls.push({ url: vgmdbLink, link_type: 86 });
if (stores) {
for (const store of stores) {
const linkTypes = mapLinkTypes(store['name']);
for (const linkType of linkTypes) {
// Filter out links to internal marketplace
if (store['link'].startsWith('http')) {
urls.push({ url: store['link'], link_type: linkType });
}
}
}
}
if (websites && websites['Commercial']) {
for (const commercial of websites['Commercial']) {
// Seems to fill same purpose as stores for albums
urls.push({ url: commercial['link'] });
}
}
return urls;
}
/**
* Returns an array of appropriate link_types given storeName from the VGMdb API.
*/
function mapLinkTypes(storeName) {
const amazonAsin = 77;
const purchaseForMailOrder = 79;
const purchaseForDownload = 74;
const freeStreaming = 85;
const linkTypes = [];
switch (storeName) {
case 'Amazon':
case 'Amazon.co.jp':
linkTypes.push(amazonAsin);
break;
case 'iTunes':
case 'e-onkyo':
case 'OTOTOY':
linkTypes.push(purchaseForDownload);
break;
case 'CDJapan':
case 'Play-Asia':
case 'YesAsia':
linkTypes.push(purchaseForMailOrder);
break;
case 'Spotify':
linkTypes.push(freeStreaming);
break;
default:
linkTypes.push(null);
break;
}
return linkTypes;
}
/*
* Returns a list of disc objects each featuring title, format and tracklist
* based on VGMdb disc data and media format.
*/
function mapDiscs(vgmdbDiscs, mediaFormat) {
const discs = [];
let multipleMedia;
let generalMediaFormat;
if (mediaFormat.includes('+')) {
multipleMedia = true;
} else {
generalMediaFormat = extractVgmdbMedia(mediaFormat);
}
for (const vgmdbDisc of vgmdbDiscs) {
const disc = {};
if (multipleMedia) {
const discMediaFormat = extractVgmdbMedia(vgmdbDisc['name']);
disc['format'] = mapFormat(discMediaFormat);
} else {
disc['format'] = mapFormat(generalMediaFormat);
}
disc['tracks'] = mapTracks(vgmdbDisc['tracks']);
discs.push(disc);
}
return discs;
}
/*
* Returns a MusicBrainz format based on a VGMdb media format.
*/
function mapFormat(mediaFormat) {
switch (mediaFormat) {
case 'Flexi Disc':
return 'Vinyl';
case 'Digital':
case 'Download Card':
return 'Digital Media';
case 'SA-CD':
return 'SACD';
case 'CD Video':
return 'CDV';
case 'Laser Disc':
return 'LaserDisc';
case 'Floppy Disc':
return 'Other';
case 'USB':
return 'USB Flash Drive';
case 'UHQCD':
case 'Blu-spec CD':
case 'Blu-spec CD2':
case 'HQCD':
case 'SHM-CD':
return 'CD';
default:
return mediaFormat;
}
}
/*
* Returns a MusicBrainz tracklist based on a VGMdb tracklist.
*/
function mapTracks(vgmdbTracks) {
const tracks = [];
const language = getTracklistLanguage(vgmdbTracks);
for (const vgmdbTrack of vgmdbTracks) {
tracks.push({ title: vgmdbTrack['names'][language], duration: vgmdbTrack['track_length'] });
}
return tracks;
}
/*
* Returns the VGMdb style media format part of a string, or null if none is
* found. If an album has only one type of media, the disc name won't contain
* media format.
*
* Doesn't actually match every VGMdb media because they can have subformats
* other than this.
*/
function extractVgmdbMedia(s) {
const match = s.match(
/(Cassette|Vinyl|Flexi Disc|DVD|Digital|SA-CD|Other|CD Video|VHS|Blu-ray|Laser Disc|Floppy Disc|USB|Download Card|UHQCD|Blu-spec CD|Blu-spec CD2|HQCD|SHM-CD|PLAYBUTTON|MiniDisc|CD)/g
);
return match ? match[0] : null;
}
/*
* Returns an organization element with the "label" role, or null if none exists.
*/
function getLabelOrganization(organizations) {
for (const organization of organizations) {
if (organization['role'] === 'label') {
return organization;
}
}
return null;
}
/*
* Return the language used as key for the VGMdb tracklist.
*/
function getTracklistLanguage(vgmdbTracks) {
let language;
// VGMdb stores tracklists by album, not by disc. Thus it should be enough
// to examine the first track to figure out which one to use.
const track = vgmdbTracks[0];
const languages = Object.keys(track['names']);
const foreignLanguages = getForeignLanguages(languages);
if (foreignLanguages.length) {
// Should be the one printed tracklist, probably Japanese or in
// some cases Korean
language = foreignLanguages[0];
} else if (languages.includes('English')) {
// Either printed tracklist or pseudo-release translation
language = 'English';
} else if (languages.includes('Romaji')) {
// Should be pseudo-release transliteration
language = 'Romaji';
}
return language;
}
/*
* Returns a list of languages other than English and Romaji in the input list.
* This should be a list with zero or one elements.
*/
function getForeignLanguages(languages) {
return languages.filter(language => language !== 'English' && language !== 'Romaji');
}