-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathmb_discids_detector.user.js
More file actions
339 lines (307 loc) · 13 KB
/
Copy pathmb_discids_detector.user.js
File metadata and controls
339 lines (307 loc) · 13 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
// ==UserScript==
// @name Musicbrainz DiscIds Detector
// @namespace https://github.com/murdos/musicbrainz-userscripts
// @version 2026.07.05.7
// @description Generate MusicBrainz DiscIds from online EAC logs, and check existence in MusicBrainz database.
// @downloadURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/dist/mb_discids_detector.user.js
// @updateURL https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/dist/mb_discids_detector.user.js
// @match https://orpheus.network/torrents.php?id=*
// @match https://redacted.sh/torrents.php?id=*
// @match https://lztr.me/torrents.php?id=*
// @match https://notwhat.cd/torrents.php?id=*
// @require lib/logger.js
// ==/UserScript==
/* eslint-disable */
console.warn(
'[Musicbrainz DiscIds Detector]: ⚠️ This userscript has been rewritten in TypeScript and is now hosted at a new URL. It should auto-update to use the TS version automatically, but if it didn’t and you’re seeing this message, please upgrade it manually from https://raw.githubusercontent.com/murdos/musicbrainz-userscripts/dist/mb_discids_detector.user.js.',
);
LOGGER.setLevel('info');
function onReady(fn) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fn);
} else {
fn();
}
}
onReady(function () {
if (window.location.host.match(/orpheus\.network|redacted\.sh|lztr\.me|notwhat\.cd/)) {
LOGGER.info('Gazelle site detected');
gazellePageHandler();
}
});
function gazellePageHandler() {
const serverHost = window.location.host;
// Determine Artist name and Release title
const titleAndArtists = document.querySelector('#content div.thin h2')?.textContent ?? '';
let pattern = /(.*) - (.*) \[.*\] \[.*/;
if (serverHost.match(/orpheus/)) {
pattern = /(.*) [-–] (.*) \[.*\]( \[.*)?/;
}
let artistName, releaseName;
const m = titleAndArtists.match(pattern);
if (m) {
artistName = m[1];
releaseName = m[2];
}
LOGGER.debug('artist:', artistName, '- releaseName:', releaseName);
// Parse each torrent
for (const torrentRow of document.querySelectorAll('tr.group_torrent')) {
if (!torrentRow.id) {
continue;
}
const torrentInfo = torrentRow.nextElementSibling;
if (!torrentInfo) {
continue;
}
for (const link of torrentInfo.querySelectorAll('a')) {
if (!link.textContent.match(/View\s+Log/i)) {
continue;
}
LOGGER.debug('Log link', link);
let logAction;
const onclick = link.getAttribute('onclick') ?? '';
if (onclick.match(/show_logs/)) {
if (window.location.host.match(/orpheus/)) {
LOGGER.debug('Orpheus');
logAction = 'viewlog';
} else if (window.location.host.match(/redacted/)) {
LOGGER.debug('RED');
logAction = 'loglist';
}
} else if (onclick.match(/get_log/)) {
LOGGER.debug('LzTR');
logAction = 'log_ajax';
} else if (onclick.match(/show_log/)) {
LOGGER.debug('NotWhat.CD');
logAction = 'viewlog';
} else {
continue;
}
const targetContainer = link.closest('.linkbox');
const torrentId = /(show_logs|get_log|show_log)\('(\d+)/.exec(onclick)[2];
const logUrl = `/torrents.php?action=${logAction}&torrentid=${torrentId}`;
LOGGER.info('Log URL: ', logUrl);
LOGGER.debug('targetContainer: ', targetContainer);
// Get log content
fetch(logUrl)
.then(response => response.text())
.then(async data => {
const doc = new DOMParser().parseFromString(data, 'text/html');
const pres = doc.querySelectorAll('pre');
LOGGER.debug('Log content', pres);
const discs = await analyze_log_files(pres);
LOGGER.debug('Number of disc found', discs.length);
await check_and_display_discs(
artistName,
releaseName,
discs,
function (mb_toc_numbers, discid, discNumber) {
targetContainer?.insertAdjacentHTML(
'beforeend',
`<br /><strong>${
discs.length > 1 ? `Disc ${discNumber}: ` : ''
}MB DiscId: </strong><span id="${torrentId}_disc${discNumber}"></span>`,
);
},
function (mb_toc_numbers, discid, discNumber, found) {
const url = computeAttachURL(mb_toc_numbers, artistName, releaseName);
const html_element = document.createElement('a');
html_element.href = url;
html_element.textContent = discid;
if (found) {
html_element.style.backgroundColor = '#d0f1d0';
html_element.style.color = 'rgb(30, 70, 32)';
html_element.style.border = '1px solid rgb(30, 70, 32)';
html_element.style.paddingInline = '3px';
html_element.style.borderRadius = '3px';
}
LOGGER.debug(`#${torrentId}_disc${discNumber}`);
const el = document.getElementById(`${torrentId}_disc${discNumber}`);
if (el) el.appendChild(html_element);
},
);
})
.catch(err => LOGGER.error('Failed to fetch log', logUrl, err));
}
}
}
// Common functions
function computeAttachURL(mb_toc_numbers, artistName, releaseName) {
let url = `${'http://musicbrainz.org/cdtoc/attach?toc='}${mb_toc_numbers.join('%20')}&artist-name=${encodeURIComponent(
artistName,
)}&release-name=${encodeURIComponent(releaseName)}`;
return url;
}
async function sha1MusicBrainzDiscId(message) {
const hash = await crypto.subtle.digest('SHA-1', new TextEncoder().encode(message));
const b64 = btoa(String.fromCharCode(...new Uint8Array(hash)));
return b64.replace(/\+/g, '.').replace(/\//g, '_').replace(/=/g, '-');
}
async function analyze_log_files(log_files) {
let discs = [];
for (const log_file of log_files) {
const discsInLog = MBDiscid.log_input_to_entries(log_file.textContent);
for (const disc of discsInLog) {
discs.push(disc);
}
}
// Remove dupes discs
let keys = new Object();
let uniqueDiscs = new Array();
for (let i = 0; i < discs.length; i++) {
let discid = await MBDiscid.calculate_mb_discid(discs[i]);
if (discid in keys) {
continue;
} else {
keys[discid] = 1;
uniqueDiscs.push(discs[i]);
}
}
discs = uniqueDiscs;
return discs;
}
async function check_and_display_discs(artistName, releaseName, discs, displayDiscHandler, displayResultHandler) {
// For each disc, check if it's in MusicBrainz database
for (let i = 0; i < discs.length; i++) {
let entries = discs[i];
let discNumber = i + 1;
if (entries.length > 0) {
let mb_toc_numbers = MBDiscid.calculate_mb_toc_numbers(entries);
let discid = await MBDiscid.calculate_mb_discid(entries);
LOGGER.info(`Computed discid :${discid}`);
displayDiscHandler(mb_toc_numbers, discid, discNumber);
// Now check if this discid is known by MusicBrainz
(function (discid, discNumber, mb_toc_numbers) {
fetch(`https://musicbrainz.org/ws/2/discid/${discid}?cdstubs=no`, {
headers: { Accept: 'application/json' },
})
.then(response => {
if (!response.ok) {
displayResultHandler(mb_toc_numbers, discid, discNumber, false);
return null;
}
return response.json();
})
.then(data => {
if (!data) {
return;
}
let existsInMusicbrainz = !('error' in data) && data.error != 'Not found';
displayResultHandler(mb_toc_numbers, discid, discNumber, existsInMusicbrainz);
})
.catch(() => {
// If discid is not found, the webservice returns a 404 http code
displayResultHandler(mb_toc_numbers, discid, discNumber, false);
});
})(discid, discNumber, mb_toc_numbers);
}
}
}
/* -------------------------------------------- */
// MBDiscid code comes from https://gist.github.com/kolen/766668
// Copyright 2010, kolen
// Released under the MIT License
const MBDiscid = (function () {
this.PREGAP = 150;
this.DATA_TRACK_GAP = 11400;
this.toc_entry_matcher = new RegExp(
'^\\s*' +
'(\\d+)' + // 1 - track number
'\\s*\\|\\s*' +
'([0-9:.]+)' + // 2 - time start
'\\s*\\|\\s*' +
'([0-9:.]+)' + // 3 - time length
'\\s*\\|\\s*' +
'(\\d+)' + // 4 - start sector
'\\s*\\|\\s*' +
'(\\d+)' + // 5 - end sector
'\\s*$',
);
this.log_input_to_entries = function (text) {
let discs = [];
let entries = [];
for (const value of text.split('\n')) {
let m = toc_entry_matcher.exec(value);
if (m) {
// New disc
if (parseInt(m[1], 10) == 1) {
if (entries.length > 0) {
discs.push(entries);
}
entries = [];
}
entries.push(m);
}
}
if (entries.length > 0) {
discs.push(entries);
}
for (let i = 0; i < discs.length; i++) {
const discEntries = discs[i];
let layout_type = get_layout_type(discEntries);
let entries_audio;
if (layout_type === 'with_data') {
entries_audio = discEntries.slice(0, discEntries.length - 1);
} else {
entries_audio = discEntries;
}
discs[i] = entries_audio;
}
return discs;
};
this.get_layout_type = function (entries) {
let type = 'standard';
for (let i = 0; i < entries.length - 1; i++) {
let gap = parseInt(entries[i + 1][4], 10) - parseInt(entries[i][5], 10) - 1;
if (gap !== 0) {
if (i === entries.length - 2 && gap === this.DATA_TRACK_GAP) {
type = 'with_data';
} else {
type = 'unknown';
break;
}
}
}
return type;
};
this.calculate_mb_toc_numbers = function (entries) {
if (entries.length === 0) {
return null;
}
let leadout_offset = parseInt(entries[entries.length - 1][5], 10) + this.PREGAP + 1;
let offsets = entries.map(function (entry) {
return parseInt(entry[4], 10) + PREGAP;
});
return [1, entries.length, leadout_offset].concat(offsets);
};
this.calculate_mb_discid = async function (entries) {
let hex_left_pad = function (input, totalChars) {
input = `${parseInt(input, 10).toString(16).toUpperCase()}`;
let padWith = '0';
if (input.length < totalChars) {
while (input.length < totalChars) {
input = padWith + input;
}
}
if (input.length > totalChars) {
//if padWith was a multiple character string and num was overpadded
input = input.substring(input.length - totalChars, totalChars);
}
return input;
};
let mb_toc_numbers = calculate_mb_toc_numbers(entries);
let message = '';
let first_track = mb_toc_numbers[0];
let last_track = mb_toc_numbers[1];
let leadout_offset = mb_toc_numbers[2];
message = message + hex_left_pad(first_track, 2);
message = message + hex_left_pad(last_track, 2);
message = message + hex_left_pad(leadout_offset, 8);
for (let i = 0; i < 99; i++) {
let offset = i + 3 < mb_toc_numbers.length ? mb_toc_numbers[i + 3] : 0;
message = message + hex_left_pad(offset, 8);
}
return sha1MusicBrainzDiscId(message);
};
return this;
})();