From 81b7026e10cb4aa131e61920449cd52a54897952 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 23 Apr 2025 12:00:09 +0200 Subject: more work on acoustid and import existing musicbrainz tags --- common/src/config.rs | 1 + import/src/acoustid.rs | 101 +++++++++++++++++++++++++++++++++++++++++++++---- import/src/lib.rs | 31 +++++++++++++++ 3 files changed, 125 insertions(+), 8 deletions(-) diff --git a/common/src/config.rs b/common/src/config.rs index 4ec43eb..e2f4e62 100644 --- a/common/src/config.rs +++ b/common/src/config.rs @@ -73,6 +73,7 @@ pub struct FederationAccount { #[derive(Serialize, Deserialize, Debug, Default)] pub struct ApiSecrets { + pub acoustid: Option, pub tmdb: Option, pub tvdb: Option, pub imdb: Option, diff --git a/import/src/acoustid.rs b/import/src/acoustid.rs index b5a466a..8e8a603 100644 --- a/import/src/acoustid.rs +++ b/import/src/acoustid.rs @@ -6,16 +6,105 @@ 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::{path::Path, process::Stdio, sync::Arc}; -use tokio::{io::AsyncReadExt, process::Command}; +use std::{path::Path, process::Stdio, sync::Arc, time::Duration}; +use tokio::{ + io::AsyncReadExt, + process::Command, + sync::Semaphore, + time::{sleep_until, Instant}, +}; -#[derive(Debug, Encode, Decode, Deserialize)] +pub(crate) struct AcoustID { + client: Client, + key: String, + rate_limit: Arc, +} + +#[derive(Debug, Hash, Clone, Encode, Decode, Deserialize)] pub(crate) struct Fingerprint { - duration: f32, + duration: u32, fingerprint: String, } +#[derive(Deserialize, Encode, Decode)] +pub(crate) struct AcoustIDLookupResultRecording { + id: String, +} +#[derive(Deserialize, Encode, Decode)] +pub(crate) struct AcoustIDLookupResult { + id: String, + score: f32, + recordings: Vec, +} +#[derive(Deserialize, Encode, Decode)] +pub(crate) struct AcoustIDLookupResponse { + status: String, + results: Vec, +} + +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"), + )])) + .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 async fn get_atid_mbid(&self, fp: Fingerprint) -> Result> { + let res = self.lookup(fp).await?; + for r in &res.results { + if let Some(k) = r.recordings.get(0) { + return Ok(Some((r.id.clone(), k.id.clone()))); + } + } + Ok(None) + } + + pub async fn lookup(&self, fp: Fingerprint) -> Result> { + async_cache_memory("api-acoustid", fp.clone(), || async move { + let _permit = self.rate_limit.clone().acquire_owned().await?; + let permit_drop_ts = Instant::now() + Duration::SECOND; + + let duration = fp.duration; + let fingerprint = &fp.fingerprint; + let client = &self.key; + let meta = "recordingids"; + let body = format!("format=json&client={client}&duration={duration}&fingerprint={fingerprint}&meta={meta}"); + + let resp = self + .client + .post(format!("https://api.acoustid.org/v2/lookup")) + .body(body) + .send() + .await?.error_for_status()?.json::().await?; + + + tokio::task::spawn(async move { + sleep_until(permit_drop_ts).await; + drop(_permit); + }); + + Ok(resp) + }) + .await + } +} + #[allow(unused)] pub(crate) async fn acoustid_fingerprint(path: &Path) -> Result> { async_cache_memory("fpcalc", path, || async move { @@ -33,7 +122,3 @@ pub(crate) async fn acoustid_fingerprint(path: &Path) -> Result }) .await } - -// pub(crate) async fn acoustid_mbid(fingerprint: Fingerprint) -> Result>> { -// async_cache_memory(&["api-acoustid", fingerprint], generate) -// } diff --git a/import/src/lib.rs b/import/src/lib.rs index 8b7dc8f..4ee4a6e 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -3,6 +3,8 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin */ +#![feature(duration_constants)] +use acoustid::AcoustID; use anyhow::{anyhow, bail, Context, Result}; use infojson::YVideo; use jellybase::{ @@ -48,6 +50,7 @@ static RE_EPISODE_FILENAME: LazyLock = struct Apis { trakt: Option, tmdb: Option, + acoustid: Option, } pub fn is_importing() -> bool { @@ -73,6 +76,7 @@ fn import(db: &Database, incremental: bool) -> Result<()> { let apis = Apis { trakt: SECRETS.api.trakt.as_ref().map(|key| Trakt::new(key)), tmdb: SECRETS.api.tmdb.as_ref().map(|key| Tmdb::new(key)), + acoustid: SECRETS.api.acoustid.as_ref().map(|key| AcoustID::new(key)), }; let rthandle = Handle::current(); @@ -365,6 +369,33 @@ fn import_media_file( } } + for (key, value) in &tags { + match key.as_str() { + "MUSICBRAINZ_TRACKID" => node + .external_ids + .insert("musicbrainz:track".to_string(), value.to_owned()), + "MUSICBRAINZ_ARTISTID" => node + .external_ids + .insert("musicbrainz:artist".to_string(), value.to_owned()), + "MUSICBRAINZ_ALBUMID" => node + .external_ids + .insert("musicbrainz:album".to_string(), value.to_owned()), + "MUSICBRAINZ_ALBUMARTISTID" => node + .external_ids + .insert("musicbrainz:albumarists".to_string(), value.to_owned()), + "MUSICBRAINZ_RELEASEGROUPID" => node + .external_ids + .insert("musicbrainz:releasegroup".to_string(), value.to_owned()), + "ISRC" => node + .external_ids + .insert("isrc".to_string(), value.to_owned()), + "BARCODE" => node + .external_ids + .insert("barcode".to_string(), value.to_owned()), + _ => None, + }; + } + let tracks = tracks .entries .into_iter() -- cgit v1.2.3-70-g09d2