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
|
/*
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) 2025 metamuffin <metamuffin.org>
*/
use crate::USER_AGENT;
use anyhow::Result;
use bincode::{Decode, Encode};
use jellybase::cache::async_cache_memory;
use reqwest::{
header::{HeaderMap, HeaderName, HeaderValue},
Client, ClientBuilder,
};
use serde::Deserialize;
use std::{collections::BTreeMap, sync::Arc, time::Duration};
use tokio::{
sync::Semaphore,
time::{sleep_until, Instant},
};
pub struct MusicBrainz {
client: Client,
rate_limit: Arc<Semaphore>,
}
#[derive(Debug, Deserialize, Encode, Decode)]
#[serde(rename_all = "kebab-case")]
pub struct MbRecording {
pub id: String,
pub first_release_date: String,
pub title: String,
pub isrcs: Vec<String>,
pub video: bool,
pub disambiguation: String,
pub length: u32,
pub relations: Vec<MbRelation>,
}
#[derive(Debug, Deserialize, Encode, Decode)]
#[serde(rename_all = "kebab-case")]
pub struct MbRelation {
direction: String,
r#type: String,
type_id: String,
begin: Option<String>,
end: Option<String>,
ended: bool,
target_type: String,
target_credit: String,
source_credit: String,
attributes: Vec<String>,
attribute_ids: BTreeMap<String, String>,
attribute_values: BTreeMap<String, String>,
work: Option<MbWork>,
artist: Option<MbArtist>,
url: Option<MbUrl>,
}
#[derive(Debug, Deserialize, Encode, Decode)]
#[serde(rename_all = "kebab-case")]
pub struct MbWork {
id: String,
r#type: String,
type_id: String,
languages: Vec<String>,
iswcs: Vec<String>,
language: Option<String>,
title: String,
attributes: Vec<String>,
disambiguation: String,
}
#[derive(Debug, Deserialize, Encode, Decode)]
#[serde(rename_all = "kebab-case")]
pub struct MbArtist {
id: String,
r#type: String,
type_id: String,
name: String,
disambiguation: String,
country: String,
sort_name: String,
}
#[derive(Debug, Deserialize, Encode, Decode)]
#[serde(rename_all = "kebab-case")]
pub struct MbUrl {
id: String,
resource: String,
}
impl MusicBrainz {
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 10 requests in-flight.
rate_limit: Arc::new(Semaphore::new(10)),
}
}
pub async fn lookup_recording(&self, id: String) -> Result<Arc<MbRecording>> {
async_cache_memory("api-musicbrainz-recording", id.clone(), || async move {
let _permit = self.rate_limit.clone().acquire_owned().await?;
let permit_drop_ts = Instant::now() + Duration::from_secs(10);
let inc = [
"isrcs",
"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::<MbRecording>()
.await?;
tokio::task::spawn(async move {
sleep_until(permit_drop_ts).await;
drop(_permit);
});
Ok(resp)
})
.await
}
}
|