-
Notifications
You must be signed in to change notification settings - Fork 16
/
mb-reledit-guess_works.user.js
269 lines (251 loc) · 9 KB
/
mb-reledit-guess_works.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
/* global $ MB requests helper server relEditor */
'use strict';
// ==UserScript==
// @name MusicBrainz relation editor: Guess related works in batch
// @namespace mbz-loujine
// @author loujine
// @version 2024.11.25
// @downloadURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-guess_works.user.js
// @updateURL https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mb-reledit-guess_works.user.js
// @supportURL https://github.com/loujine/musicbrainz-scripts
// @icon https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/icon.png
// @description musicbrainz.org relation editor: Guess related works in batch
// @compatible firefox+tampermonkey
// @license MIT
// @require https://raw.githubusercontent.com/loujine/musicbrainz-scripts/master/mbz-loujine-common.js
// @include http*://*musicbrainz.org/release/*/edit-relationships
// @grant none
// @run-at document-end
// ==/UserScript==
const MBID_REGEX = /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/;
const repeatHelp = `Ways to associate subworks SW1, SW2, SW3... with selected tracks T1, T2, T3...
1,1,1 (or empty) -> SW1 on T1, SW2 on T2, SW3 on T3...
1,0,1 -> SW1 on T1, SW2 skipped, SW3 on T2...
1,2,1 -> SW1 on T1, SW2 on T2 and T3 (as partial); SW3 on T4...
1,-1,1 -> SW1 and SW2 on T1, SW3 on T2...
`;
const setWork = async (recording, work, partial) => {
const medium = MB.relationshipEditor.state.mediumsByRecordingId.get(recording.id)[0];
const tracks = medium.tracks
// if medium was unfolded manually, medium.tracks stays empty
// but relEditor.state.loadedTracks has the new data
? medium.tracks
: MB.relationshipEditor.state.loadedTracks.get(medium.position);
const track = tracks.filter(t => t.recording.id === recording.id)[0];
await helper.waitFor(() => !MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.dispatch({
type: 'update-dialog-location',
location: {
batchSelection: false,
source: recording,
targetType: 'work',
track: track,
},
});
await helper.waitFor(() => !!MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-target-entity',
source: recording,
action: {
type: 'update-autocomplete',
source: recording,
action: {
type: 'select-item',
item: {
type: 'option',
entity: work,
id: work.id,
name: work.name,
},
},
},
});
if (partial) {
const attrType = MB.linkedEntities.link_attribute_type[server.attr.partial];
MB.relationshipEditor.relationshipDialogDispatch({
type: 'set-attributes',
attributes: [{
type: {gid: attrType.gid},
typeID: attrType.id,
typeName: attrType.name,
}],
});
}
await helper.delay(1);
if (document.querySelector('.relationship-dialog p.error')) {
console.error('Dialog error, probably an identical relation already exists');
document.querySelector('.relationship-dialog button.negative').click();
} else {
document.querySelector('.relationship-dialog button.positive').click();
}
};
const replaceWork = async (recording, work) => {
const rel = recording.relationships.filter(rel => rel.target_type === 'work')[0];
await helper.waitFor(() => !MB.relationshipEditor.relationshipDialogDispatch, 1);
document.getElementById(`edit-relationship-recording-work-${rel.id}`).click();
await helper.waitFor(() => !!MB.relationshipEditor.relationshipDialogDispatch, 1);
MB.relationshipEditor.relationshipDialogDispatch({
type: 'update-target-entity',
source: recording,
action: {
type: 'update-autocomplete',
source: recording,
action: {
type: 'select-item',
item: {
type: 'option',
entity: work,
id: work.id,
name: work.name,
},
},
},
});
await helper.delay(1);
if (document.querySelector('.relationship-dialog p.error')) {
console.error('Dialog error, probably an identical relation already exists');
document.querySelector('.relationship-dialog button.negative').click();
} else {
document.querySelector('.relationship-dialog button.positive').click();
}
};
const guessWork = () => {
let idx = 0;
relEditor.orderedSelectedRecordings().forEach(recording => {
const url =
'/ws/js/work/?q=' +
encodeURIComponent(document.getElementById('prefix').value) +
' ' +
encodeURIComponent(recording.name) +
'&artist=' +
encodeURIComponent(recording.artist) +
'&fmt=json&limit=1';
if (!recording.related_works.length) {
idx += 1;
setTimeout(() => {
requests.GET(url, (resp) => {
setWork(recording, JSON.parse(resp)[0]);
});
}, idx * server.timeout);
}
});
};
const autoComplete = () => {
const input = document.getElementById('mainWork');
const match = input.value.match(MBID_REGEX);
if (match) {
const mbid = match[0];
requests.GET(`/ws/2/work/${mbid}?fmt=json`, (data) => {
data = JSON.parse(data);
input.setAttribute('mbid', mbid);
input.value = data.title || data.name;
input.style.backgroundColor = '#bbffbb';
});
} else {
input.style.backgroundColor = '#ffaaaa';
}
};
const fetchSubWorks = (workMbid, replace) => {
replace = replace || false;
if (workMbid.split('/').length > 1) {
workMbid = workMbid.split('/')[4];
}
requests.GET(`/ws/js/entity/${workMbid}?inc=rels`, (resp) => {
let repeats = document.getElementById('repeats').value.trim();
const subWorks = helper.sortSubworks(JSON.parse(resp));
let total = subWorks.length;
if (repeats) {
repeats = repeats.split(/[,; ]+/).map(s => Number.parseInt(s));
total = repeats.reduce((n, m) => Math.max(n,0) + Math.max(m,0), 0);
} else {
repeats = subWorks.map(() => 1);
}
const repeatedSubWorks = Array(total);
const partialSubWorks = Array(total);
let start = 0;
subWorks.forEach((sb, sbIdx) => {
if (repeats[sbIdx] < 0) {
repeatedSubWorks[start-1].push(sb);
partialSubWorks.fill(false, start-1, start);
} else {
repeatedSubWorks.fill([sb], start, start + repeats[sbIdx]);
partialSubWorks.fill(
repeats[sbIdx] > 1 ? true : false, start, start + repeats[sbIdx]);
start += repeats[sbIdx];
}
});
relEditor.orderedSelectedRecordings().forEach(async (recording, recIdx) => {
await helper.delay(recIdx * 200);
if (recIdx >= repeatedSubWorks.length) {
return;
}
repeatedSubWorks[recIdx].forEach(async (subw, subwIdx) => {
await helper.delay(subwIdx * 60);
if (replace && recording.related_works.length) {
replaceWork(recording, subw);
} else if (!recording.related_works.length) {
setWork(recording, subw, partialSubWorks[recIdx]);
}
});
});
});
};
(function displayToolbar() {
relEditor.container(document.querySelector('div.tabs')).insertAdjacentHTML('beforeend', `
<details open="">
<summary style="display: block;margin-left: 8px;cursor: pointer;">
<h3 style="display: list-item;">
Search for works
</h3>
</summary>
<div>
<span>
<abbr title="You can add an optional prefix (e.g. the misssing parent
work name) to help guessing the right work">prefix</abbr>:
</span>
<input type="text" id="prefix" value="" placeholder="optional">
<br />
<input type="button" id="searchWork" value="Guess works">
<br />
<h3>Link to parts of a main Work</h3>
<p>
Fill the main work mbid to link selected recordings to (ordered) parts of the work.
</p>
<span>
Repeats:
</span>
<input type="text" id="repeats" placeholder="n1,n2,n3... (optional)">
<span title="${repeatHelp}">🛈</span>
<br />
<label for="replaceSubworks">Replace work if pre-existing: </label>
<input type="checkbox" id="replaceSubworks">
<br />
<span>Main work name: </span>
<input type="text" id="mainWork" placeholder="main work mbid">
<input type="button" id="fetchSubworks" value="Load subworks">
</div>
</details>
`);
})();
$(document).ready(function() {
let appliedNote = false;
document.getElementById('searchWork').addEventListener('click', () => {
guessWork();
if (!appliedNote) {
relEditor.editNote(GM_info.script, 'Set guessed works');
appliedNote = true;
}
});
document.getElementById('mainWork').addEventListener('input', autoComplete);
document.getElementById('fetchSubworks').addEventListener('click', () => {
fetchSubWorks(
document.getElementById('mainWork').getAttribute('mbid'),
document.getElementById('replaceSubworks').checked,
);
if (!appliedNote) {
relEditor.editNote(GM_info.script, 'Set guessed subworks');
appliedNote = true;
}
});
return false;
});