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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{
USER_AGENT,
helpers::get_or_insert_slug,
plugins::{
ImportPlugin, PluginContext, PluginInfo,
musicbrainz::reltypes::{VGMDB, WIKIDATA},
},
source_rank::ObjectImportSourceExt,
};
use anyhow::{Context, Result};
use jellycache::Cache;
use jellycommon::{jellyobject::ObjectBuffer, *};
use jellydb::RowNum;
use log::info;
use reqwest::{
Client, ClientBuilder,
header::{HeaderMap, HeaderName, HeaderValue},
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, sync::Arc, time::Duration};
use tokio::{
runtime::Handle,
sync::Semaphore,
time::{Instant, sleep_until},
};
pub mod reltypes {
pub const MUSIC_VIDEO: &str = "ce3de655-7451-44d1-9224-87eb948c205d";
pub const INSTRUMENTAL: &str = "9fc01a58-7801-4bd2-b07d-61cc7ffacf90";
pub const VOCAL: &str = "0fdbe3c6-7700-4a31-ae54-b53f06ae1cfa";
pub const RECORDING: &str = "a01ee869-80a8-45ef-9447-c59e91aa7926";
pub const PROGRAMMING: &str = "36c50022-44e0-488d-994b-33f11d20301e";
pub const PRODUCER: &str = "5c0ceac3-feb4-41f0-868d-dc06f6e27fc0";
pub const ARTIST: &str = "5c0ceac3-feb4-41f0-868d-dc06f6e27fc0";
pub const PHONOGRAPHIC_COPYRIGHT: &str = "7fd5fbc0-fbf4-4d04-be23-417d50a4dc30";
pub const MIX: &str = "3e3102e1-1896-4f50-b5b2-dd9824e46efe";
pub const INSTRUMENT: &str = "59054b12-01ac-43ee-a618-285fd397e461";
pub const WIKIDATA: &str = "689870a4-a1e4-4912-b17f-7b2664215698";
pub const VGMDB: &str = "0af15ab3-c615-46d6-b95b-a5fcd2a92ed9";
}
pub struct MusicBrainz {
client: Client,
rate_limit: Arc<Semaphore>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbRecordingRel {
pub id: String,
pub first_release_date: Option<String>,
pub title: String,
pub isrcs: Vec<String>,
pub video: bool,
pub disambiguation: String,
pub length: Option<u32>,
pub relations: Vec<MbRelation>,
pub artist_credit: Vec<MbArtistCredit>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbArtistRel {
pub id: String,
pub isnis: Vec<String>,
pub ipis: Vec<String>,
pub name: String,
pub disambiguation: String,
pub country: Option<String>,
pub sort_name: String,
pub gender_id: Option<String>,
pub area: Option<MbArea>,
pub begin_area: Option<MbArea>,
pub end_area: Option<MbArea>,
pub life_span: MbTimespan,
pub relations: Vec<MbRelation>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbArtistCredit {
pub name: String,
pub artist: MbArtist,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbRelation {
pub direction: String,
pub r#type: String,
pub type_id: String,
pub begin: Option<String>,
pub end: Option<String>,
pub ended: bool,
pub target_type: String,
pub target_credit: String,
pub source_credit: String,
pub attributes: Vec<String>,
pub attribute_ids: BTreeMap<String, String>,
pub attribute_values: BTreeMap<String, String>,
pub work: Option<MbWork>,
pub artist: Option<MbArtist>,
pub url: Option<MbUrl>,
pub recording: Option<MbRecording>,
pub series: Option<MbSeries>,
pub event: Option<MbEvent>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbSeries {
pub id: String,
pub r#type: Option<String>,
pub type_id: Option<String>,
pub name: String,
pub disambiguation: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbRecording {
pub id: String,
pub title: String,
#[serde(default)]
pub isrcs: Vec<String>,
pub video: bool,
pub disambiguation: String,
pub length: Option<u32>,
#[serde(default)]
pub artist_credit: Vec<MbArtistCredit>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbWork {
pub id: String,
pub r#type: Option<String>,
pub type_id: Option<String>,
pub languages: Vec<String>,
pub iswcs: Vec<String>,
pub language: Option<String>,
pub title: String,
pub attributes: Vec<String>,
pub disambiguation: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbEvent {
pub id: String,
pub r#type: Option<String>,
pub type_id: Option<String>,
pub name: String,
pub time: String,
pub cancelled: bool,
pub setlist: String,
pub life_span: MbTimespan,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbArtist {
pub id: String,
pub r#type: Option<String>,
pub type_id: Option<String>,
pub name: String,
pub disambiguation: String,
pub country: Option<String>,
pub sort_name: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbTimespan {
pub begin: Option<String>,
pub end: Option<String>,
pub ended: bool,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbArea {
pub name: String,
pub sort_name: String,
#[serde(default)]
pub iso_3166_1_codes: Vec<String>,
pub id: String,
pub disambiguation: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MbUrl {
pub id: String,
pub resource: String,
}
impl Default for MusicBrainz {
fn default() -> Self {
Self::new()
}
}
impl MusicBrainz {
const MAX_PAR_REQ: usize = 4;
pub fn new() -> Self {
let client = ClientBuilder::new()
.default_headers(HeaderMap::from_iter([
(
HeaderName::from_static("accept"),
HeaderValue::from_static("application/json"),
),
(
HeaderName::from_static("user-agent"),
HeaderValue::from_static(USER_AGENT),
),
]))
.build()
.unwrap();
Self {
client,
// send at most 1 req/s according to musicbrainz docs, each lock is held for 10s
// this implementation also never sends more than MAX_PAR_REQ requests in-flight.
rate_limit: Arc::new(Semaphore::new(Self::MAX_PAR_REQ)),
}
}
pub fn lookup_recording(
&self,
cache: &Cache,
id: &str,
rt: &Handle,
) -> Result<Arc<MbRecordingRel>> {
cache
.cache_memory(&format!("ext/musicbrainz/recording/{id}.json"), move || {
rt.block_on(async {
let _permit = self.rate_limit.clone().acquire_owned().await?;
let permit_drop_ts =
Instant::now() + Duration::from_secs(Self::MAX_PAR_REQ as u64);
info!("recording lookup: {id}");
let inc = [
"isrcs",
"artists",
"area-rels",
"artist-rels",
"event-rels",
"genre-rels",
"instrument-rels",
"label-rels",
"place-rels",
"recording-rels",
"release-rels",
"release-group-rels",
"series-rels",
"url-rels",
"work-rels",
]
.join("+");
let resp = self
.client
.get(format!(
"https://musicbrainz.org/ws/2/recording/{id}?inc={inc}"
))
.send()
.await?
.error_for_status()?
.json::<MbRecordingRel>()
.await?;
tokio::task::spawn(async move {
sleep_until(permit_drop_ts).await;
drop(_permit);
});
Ok(resp)
})
})
.context("musicbrainz recording lookup")
}
pub fn lookup_artist(&self, cache: &Cache, id: &str, rt: &Handle) -> Result<Arc<MbArtistRel>> {
cache
.cache_memory(&format!("ext/musicbrainz/artist/{id}.json"), move || {
rt.block_on(async {
let _permit = self.rate_limit.clone().acquire_owned().await?;
let permit_drop_ts =
Instant::now() + Duration::from_secs(Self::MAX_PAR_REQ as u64);
info!("artist lookup: {id}");
let inc = [
"area-rels",
"artist-rels",
"event-rels",
"genre-rels",
"instrument-rels",
"label-rels",
"place-rels",
"recording-rels",
"release-rels",
"release-group-rels",
"series-rels",
"url-rels",
"work-rels",
]
.join("+");
let resp = self
.client
.get(format!(
"https://musicbrainz.org/ws/2/artist/{id}?inc={inc}"
))
.send()
.await?
.error_for_status()?
.json::<MbArtistRel>()
.await?;
tokio::task::spawn(async move {
sleep_until(permit_drop_ts).await;
drop(_permit);
});
Ok(resp)
})
})
.context("musicbrainz artist lookup")
}
}
impl ImportPlugin for MusicBrainz {
fn info(&self) -> PluginInfo {
PluginInfo {
name: "musicbrainz",
tag: MSOURCE_MUSICBRAINZ,
handle_process: true,
..Default::default()
}
}
fn process(&self, ct: &PluginContext, node_row: RowNum) -> Result<()> {
self.process_recording(ct, node_row)?;
self.process_artist(ct, node_row)?;
Ok(())
}
}
impl MusicBrainz {
fn process_recording(&self, ct: &PluginContext, node_row: RowNum) -> Result<()> {
let data = ct.ic.get_node(node_row)?.unwrap();
let data = data.as_object();
let Some(mbid) = data
.get(NO_IDENTIFIERS)
.unwrap_or_default()
.get(IDENT_MUSICBRAINZ_RECORDING)
else {
return Ok(());
};
let rec = self.lookup_recording(&ct.ic.cache, mbid, ct.rt)?;
ct.ic.db.transaction(&mut |txn| {
let mut node = txn.get(node_row)?.unwrap();
node = node.as_object().insert_s(ct.is, NO_TITLE, &rec.title);
if let Some(a) = rec.artist_credit.first() {
node = node
.as_object()
.insert_s(ct.is, NO_SUBTITLE, &a.artist.name);
}
node = node.as_object().update(NO_IDENTIFIERS, |ids| {
ids.insert_multi(
IDENT_ISRC,
&rec.isrcs.iter().map(|e| e.as_str()).collect::<Vec<_>>(),
)
});
for rel in &rec.relations {
use reltypes::*;
let Some((role, cat)) = (match rel.type_id.as_str() {
INSTRUMENT => Some(("", CRCAT_INSTRUMENT)),
VOCAL => Some(("", CRCAT_VOCAL)),
PRODUCER => Some(("", CRCAT_PRODUCER)),
MIX => Some(("mix ", CRCAT_ENGINEER)),
PHONOGRAPHIC_COPYRIGHT => Some(("phonographic copyright ", CRCAT_ENGINEER)),
PROGRAMMING => Some(("programming ", CRCAT_ENGINEER)),
_ => None,
}) else {
continue;
};
let artist = rel.artist.as_ref().unwrap();
let artist_slug = format!("musicbrainz-artist-{}", artist.id);
let artist_row = get_or_insert_slug(txn, &artist_slug)?;
let mut artist_node = txn.get(artist_row)?.unwrap();
artist_node = artist_node.as_object().update(NO_IDENTIFIERS, |ids| {
ids.insert(IDENT_MUSICBRAINZ_ARTIST, &artist.id)
});
artist_node = artist_node.as_object().insert(NO_KIND, KIND_PERSON);
txn.update(artist_row, artist_node)?;
ct.pending_nodes.lock().unwrap().insert(artist_row);
let credit = ObjectBuffer::new(&mut [
(CR_NODE.0, &artist_row),
(CR_KIND.0, &cat),
(CR_ROLE.0, &role),
]);
node = node
.as_object()
.extend_object(NO_CREDIT, CR_NODE.0, Some(credit))
}
txn.update(node_row, node)
})?;
Ok(())
}
fn process_artist(&self, ct: &PluginContext, node_row: RowNum) -> Result<()> {
let data = ct.ic.get_node(node_row)?.unwrap();
let data = data.as_object();
let Some(mbid) = data
.get(NO_IDENTIFIERS)
.unwrap_or_default()
.get(IDENT_MUSICBRAINZ_ARTIST)
else {
return Ok(());
};
let artist = self.lookup_artist(&ct.ic.cache, mbid, ct.rt)?;
ct.ic.db.transaction(&mut |txn| {
let mut node = txn.get(node_row)?.unwrap();
node = node.as_object().insert_s(ct.is, NO_TITLE, &artist.name);
for rel in &artist.relations {
let url = rel.url.as_ref().map(|u| u.resource.clone());
match rel.type_id.as_str() {
WIKIDATA => {
if let Some(url) = url
&& let Some(id) = url.strip_prefix("https://www.wikidata.org/wiki/")
{
node = node
.as_object()
.update(NO_IDENTIFIERS, |ids| ids.insert(IDENT_WIKIDATA, id))
}
}
VGMDB => {
if let Some(url) = url
&& let Some(id) = url.strip_prefix("https://vgmdb.net/artist/")
{
node = node
.as_object()
.update(NO_IDENTIFIERS, |ids| ids.insert(IDENT_VGMDB_ARTIST, id))
}
}
_ => (),
}
}
txn.update(node_row, node)
})?;
Ok(())
}
}
|