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
|
/*
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,
plugins::{ImportPlugin, PluginContext, PluginInfo},
source_rank::ObjectImportSourceExt,
};
use anyhow::{Context, Result};
use jellycache::{Cache, HashKey};
use jellycommon::*;
use jellydb::RowNum;
use jellyremuxer::matroska::Segment;
use log::info;
use reqwest::{
Client, ClientBuilder,
header::{HeaderMap, HeaderName, HeaderValue},
};
use serde::{Deserialize, Serialize};
use std::{
io::Read,
path::Path,
process::{Command, Stdio},
sync::Arc,
time::Duration,
};
use tokio::{
runtime::Handle,
sync::Semaphore,
time::{Instant, sleep_until},
};
pub(crate) struct AcoustID {
client: Client,
key: String,
rate_limit: Arc<Semaphore>,
}
#[derive(Debug, Hash, Clone, Serialize, Deserialize)]
pub(crate) struct Fingerprint {
duration: u32,
fingerprint: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct FpCalcOutput {
duration: f32,
fingerprint: String,
}
#[derive(Serialize, Deserialize)]
pub(crate) struct AcoustIDLookupResultRecording {
id: String,
}
#[derive(Serialize, Deserialize)]
pub(crate) struct AcoustIDLookupResult {
id: String,
score: f32,
#[serde(default)]
recordings: Vec<AcoustIDLookupResultRecording>,
}
#[derive(Serialize, Deserialize)]
pub(crate) struct AcoustIDLookupResponse {
status: String,
results: Vec<AcoustIDLookupResult>,
}
impl AcoustID {
pub fn new(api_key: &str) -> 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 3 req/s according to acoustid docs, each lock is therefore held for 1s
// this implementation also never sends more than 3 requests in-flight.
rate_limit: Arc::new(Semaphore::new(3)),
key: api_key.to_owned(),
}
}
pub fn get_atid_mbid(
&self,
cache: &Cache,
fp: &Fingerprint,
rt: &Handle,
) -> Result<Option<(String, String)>> {
let res = self.lookup(cache, fp.to_owned(), rt)?;
for r in &res.results {
if let Some(k) = r.recordings.first() {
return Ok(Some((r.id.clone(), k.id.clone())));
}
}
Ok(None)
}
pub fn lookup(
&self,
cache: &Cache,
fp: Fingerprint,
rt: &Handle,
) -> Result<Arc<AcoustIDLookupResponse>> {
cache.cache_memory(&format!("ext/acoustid/{}.json", HashKey(&fp)) , move || rt.block_on(async {
let _permit = self.rate_limit.clone().acquire_owned().await?;
let permit_drop_ts = Instant::now() + Duration::SECOND;
info!("acoustid lookup");
let duration = fp.duration;
let fingerprint = fp.fingerprint.replace("=", "%3D");
let client = &self.key;
let body = format!("format=json&meta=recordingids&client={client}&duration={duration}&fingerprint={fingerprint}");
let resp = self
.client
.post("https://api.acoustid.org/v2/lookup".to_string())
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await?.error_for_status()?.json::<AcoustIDLookupResponse>().await?;
tokio::task::spawn(async move {
sleep_until(permit_drop_ts).await;
drop(_permit);
});
Ok(resp)
}))
.context("acoustid lookup")
}
}
pub(crate) fn acoustid_fingerprint(cache: &Cache, path: &Path) -> Result<Arc<Fingerprint>> {
cache.cache_memory(
&format!("media/chromaprint/{}.json", HashKey(path)),
move || {
info!("fpcalc {path:?}");
let child = Command::new("fpcalc")
.arg("-json")
.arg(path)
.stdout(Stdio::piped())
.spawn()
.context("fpcalc")?;
let mut buf = Vec::new();
child
.stdout
.unwrap()
.read_to_end(&mut buf)
.context("read fpcalc output")?;
let out: FpCalcOutput =
serde_json::from_slice(&buf).context("parsing fpcalc output")?;
let out = Fingerprint {
duration: out.duration as u32,
fingerprint: out.fingerprint,
};
Ok(out)
},
)
}
impl ImportPlugin for AcoustID {
fn info(&self) -> PluginInfo {
PluginInfo {
name: "acoustid",
tag: MSOURCE_ACOUSTID,
handle_media: true,
..Default::default()
}
}
fn media(&self, ct: &PluginContext, node: RowNum, path: &Path, seg: &Segment) -> Result<()> {
if !ct.iflags.use_acoustid {
return Ok(());
}
let duration = (seg.info.duration.unwrap_or_default() * seg.info.timestamp_scale as f64)
/ 1_000_000_000.;
if duration > 600. || duration < 10. {
return Ok(());
}
let fp = acoustid_fingerprint(&ct.ic.cache, path)?;
let Some((atid, mbid)) = self.get_atid_mbid(&ct.ic.cache, &fp, &ct.rt)? else {
return Ok(());
};
ct.ic.update_node(node, |node| {
node.as_object().update(NO_IDENTIFIERS, |ids| {
ids.insert_s(ct.is, IDENT_ACOUST_ID_TRACK, &atid)
.as_object()
.insert_s(ct.is, IDENT_MUSICBRAINZ_RECORDING, &mbid)
})
})?;
Ok(())
}
}
|