From a03b39fbfc9990f13359b2e8e071aecad6440f40 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Tue, 30 Sep 2025 16:44:59 +0300 Subject: [PATCH] fix: add get_album_by_uri and get_artist_by_uri functions for fetching albums and artists by URI --- crates/scrobbler/src/repo/album.rs | 14 ++++++++++++++ crates/scrobbler/src/repo/artist.rs | 14 ++++++++++++++ crates/scrobbler/src/scrobbler.rs | 10 +++++++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/scrobbler/src/repo/album.rs b/crates/scrobbler/src/repo/album.rs index 820d6730..f87f979a 100644 --- a/crates/scrobbler/src/repo/album.rs +++ b/crates/scrobbler/src/repo/album.rs @@ -17,3 +17,17 @@ pub async fn get_album_by_track_id(pool: &Pool, track_id: &str) -> Res Ok(results[0].clone()) } + +pub async fn get_album_by_uri(pool: &Pool, uri: &str) -> Result { + let results: Vec = sqlx::query_as( + r#" + SELECT * FROM albums + WHERE albums.uri = $1 + "#, + ) + .bind(uri) + .fetch_all(pool) + .await?; + + Ok(results[0].clone()) +} diff --git a/crates/scrobbler/src/repo/artist.rs b/crates/scrobbler/src/repo/artist.rs index 96e9938e..ecc00de9 100644 --- a/crates/scrobbler/src/repo/artist.rs +++ b/crates/scrobbler/src/repo/artist.rs @@ -20,3 +20,17 @@ pub async fn get_artist_by_track_id( Ok(results[0].clone()) } + +pub async fn get_artist_by_uri(pool: &Pool, uri: &str) -> Result { + let results: Vec = sqlx::query_as( + r#" + SELECT * FROM artists + WHERE artists.uri = $1 + "#, + ) + .bind(uri) + .fetch_all(pool) + .await?; + + Ok(results[0].clone()) +} diff --git a/crates/scrobbler/src/scrobbler.rs b/crates/scrobbler/src/scrobbler.rs index 6dfc0dd8..48f887df 100644 --- a/crates/scrobbler/src/scrobbler.rs +++ b/crates/scrobbler/src/scrobbler.rs @@ -12,7 +12,7 @@ use crate::{ musicbrainz::{ client::MusicbrainzClient, get_best_release_from_recordings, recording::Recording, }, - repo::{self, album}, + repo::{self}, rocksky, spotify::{client::SpotifyClient, refresh_token}, types::{Scrobble, Track}, @@ -579,8 +579,12 @@ pub async fn scrobble_listenbrainz( if let Some(track) = result { tracing::info!(id = %track.xata_id, artist = %track.artist, album = %track.album, album_atist = %track.album_artist, album_uri = ?track.album_uri, artist_uri = ?track.artist_uri, "Xata (track)"); scrobble.album = Some(track.album.clone()); - let album = repo::album::get_album_by_track_id(pool, &track.xata_id).await?; - let artist = repo::artist::get_artist_by_track_id(pool, &track.xata_id).await?; + let album = + repo::album::get_album_by_uri(pool, &track.album_uri.clone().unwrap_or_default()) + .await?; + let artist = + repo::artist::get_artist_by_uri(pool, &track.artist_uri.clone().unwrap_or_default()) + .await?; let mut track: Track = track.into(); track.year = match album.year { Some(year) => Some(year as u32), -- 2.51.2