fix(suggestions): add Qobuz similar artists fallback and accent normalization
Two improvements: 1. Accent normalization in artist validation - Now "Mikael Åkerfeldt" matches "Mikael Akerfeldt" in Qobuz - Uses existing normalize_name() function for comparison 2. Qobuz similar artists as fallback (step 4c) - When pool has <20 tracks after MusicBrainz relationships - Fetches similar artists from Qobuz API for each playlist artist - These artists are guaranteed to exist in Qobuz - Still applies genre compatibility filter This ensures playlists with few MusicBrainz relationships (like solo artists) still get meaningful suggestions from Qobuz's own similarity data.
This commit is contained in:
1 parent
c30b367e07
commit
f385e3a75b
1 file changed
+170
-8
@@ -269,6 +269,95 @@ impl SuggestionsEngine {
|
||||
}
|
||||
log::debug!("[SuggestionsEngine] Step 4b completed in {:?}, got {} total tracks", step4b_start.elapsed(), all_tracks.len());
|
||||
|
||||
// 4c. If pool is still small, use Qobuz's "similar artists" API as fallback
|
||||
// This gives us artists that definitely exist in Qobuz
|
||||
const MIN_TRACKS_BEFORE_QOBUZ_SIMILAR: usize = 20;
|
||||
if all_tracks.len() < MIN_TRACKS_BEFORE_QOBUZ_SIMILAR {
|
||||
log::info!(
|
||||
"[SuggestionsEngine] Step 4c: Pool too small ({}), fetching Qobuz similar artists",
|
||||
all_tracks.len()
|
||||
);
|
||||
let step4c_start = Instant::now();
|
||||
|
||||
// Get client for Qobuz API calls
|
||||
let client = self.qobuz_client.lock().await;
|
||||
|
||||
// For each playlist artist, get their Qobuz similar artists
|
||||
let mut qobuz_similar_ids: HashSet<u64> = HashSet::new();
|
||||
|
||||
for (_mbid, name) in playlist_artists {
|
||||
// First, find the Qobuz artist ID for this playlist artist
|
||||
if let Some((qobuz_id, _)) = self.validate_qobuz_artist(&client, name).await {
|
||||
// Get similar artists from Qobuz (up to 10 per playlist artist)
|
||||
match client.get_similar_artists(qobuz_id, 10, 0).await {
|
||||
Ok(similar_page) => {
|
||||
for similar_artist in similar_page.items {
|
||||
// Skip if we've already processed this artist
|
||||
if qobuz_similar_ids.contains(&similar_artist.id) {
|
||||
continue;
|
||||
}
|
||||
qobuz_similar_ids.insert(similar_artist.id);
|
||||
|
||||
// Check genre compatibility
|
||||
if self.has_incompatible_genre(&client, similar_artist.id, &similar_artist.name).await {
|
||||
log::debug!(
|
||||
"[SuggestionsEngine] Skipping Qobuz similar '{}' - incompatible genre",
|
||||
similar_artist.name
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if !source_artists.contains(&similar_artist.name) {
|
||||
source_artists.push(similar_artist.name.clone());
|
||||
}
|
||||
|
||||
// Search tracks for this similar artist (use empty MBID since we have Qobuz ID)
|
||||
let tracks = self.search_artist_tracks_by_qobuz_id(
|
||||
similar_artist.id,
|
||||
&similar_artist.name,
|
||||
0.8, // High similarity since Qobuz says they're similar
|
||||
).await;
|
||||
|
||||
for mut track in tracks {
|
||||
if exclude_track_ids.contains(&track.track_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if include_reasons {
|
||||
track.reason = Some(format!("Similar to {} (Qobuz)", name));
|
||||
}
|
||||
|
||||
all_tracks.push(track);
|
||||
}
|
||||
|
||||
// Stop if we have enough
|
||||
if all_tracks.len() >= self.config.max_pool_size {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"[SuggestionsEngine] Failed to get Qobuz similar artists for '{}': {}",
|
||||
name, e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if all_tracks.len() >= self.config.max_pool_size {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!(
|
||||
"[SuggestionsEngine] Step 4c completed in {:?}, now have {} tracks from {} Qobuz similar artists",
|
||||
step4c_start.elapsed(),
|
||||
all_tracks.len(),
|
||||
qobuz_similar_ids.len()
|
||||
);
|
||||
}
|
||||
|
||||
// 5. Deduplicate by title+artist (keeps highest similarity version)
|
||||
let mut seen_titles: HashSet<String> = HashSet::new();
|
||||
all_tracks.retain(|track| {
|
||||
@@ -320,6 +409,77 @@ impl SuggestionsEngine {
|
||||
self.search_artist_tracks_with_limit(artist_mbid, artist_name, similarity, self.config.tracks_per_artist).await
|
||||
}
|
||||
|
||||
/// Search Qobuz for tracks by Qobuz artist ID (more reliable when we already validated the artist)
|
||||
async fn search_artist_tracks_by_qobuz_id(
|
||||
&self,
|
||||
qobuz_artist_id: u64,
|
||||
artist_name: &str,
|
||||
similarity: f32,
|
||||
) -> Vec<SuggestedTrack> {
|
||||
let client = self.qobuz_client.lock().await;
|
||||
let limit = self.config.tracks_per_artist;
|
||||
|
||||
// Search by artist name but verify tracks belong to this specific Qobuz artist ID
|
||||
match client.search_tracks(artist_name, (limit * 3) as u32, 0, None).await {
|
||||
Ok(results) => {
|
||||
let mut tracks = Vec::new();
|
||||
|
||||
for item in results.items {
|
||||
// Only accept tracks from this exact artist (by ID)
|
||||
let performer_id = item.performer.as_ref().map(|p| p.id);
|
||||
if performer_id != Some(qobuz_artist_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
tracks.push(self.track_to_suggested_with_qobuz_id(&item, qobuz_artist_id, similarity));
|
||||
if tracks.len() >= limit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
tracks
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Failed to search tracks for {} (Qobuz ID {}): {}", artist_name, qobuz_artist_id, e);
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a Track to a SuggestedTrack (using Qobuz artist ID instead of MBID)
|
||||
fn track_to_suggested_with_qobuz_id(&self, track: &Track, _qobuz_artist_id: u64, similarity: f32) -> SuggestedTrack {
|
||||
let (album_title, album_id, album_image_url) = match &track.album {
|
||||
Some(album) => {
|
||||
let image_url = album.image.thumbnail
|
||||
.as_ref()
|
||||
.or(album.image.small.as_ref())
|
||||
.or(album.image.large.as_ref())
|
||||
.cloned();
|
||||
(album.title.clone(), album.id.clone(), image_url)
|
||||
}
|
||||
None => (String::new(), String::new(), None),
|
||||
};
|
||||
|
||||
let (track_artist, artist_id) = match &track.performer {
|
||||
Some(p) => (p.name.clone(), Some(p.id)),
|
||||
None => (String::new(), None),
|
||||
};
|
||||
|
||||
SuggestedTrack {
|
||||
track_id: track.id,
|
||||
title: track.title.clone(),
|
||||
artist_name: track_artist,
|
||||
artist_id,
|
||||
artist_mbid: None, // No MBID for Qobuz-sourced similar artists
|
||||
album_title,
|
||||
album_id,
|
||||
album_image_url,
|
||||
duration: track.duration,
|
||||
similarity_score: similarity,
|
||||
reason: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Search Qobuz for tracks by an artist with custom limit
|
||||
///
|
||||
/// First validates that the artist EXISTS in Qobuz (has a dedicated artist page).
|
||||
@@ -414,7 +574,8 @@ impl SuggestionsEngine {
|
||||
client: &QobuzClient,
|
||||
name: &str,
|
||||
) -> Option<(u64, String)> {
|
||||
let name_lower = name.to_lowercase();
|
||||
// Normalize name for comparison (removes accents: å→a, é→e, etc.)
|
||||
let name_normalized = normalize_name(name);
|
||||
|
||||
// Search Qobuz for artist
|
||||
let results = match client.search_artists(name, 10, 0, None).await {
|
||||
@@ -425,14 +586,15 @@ impl SuggestionsEngine {
|
||||
}
|
||||
};
|
||||
|
||||
// Look for exact name match
|
||||
// Look for exact name match (comparing normalized versions)
|
||||
let mut candidate: Option<(u64, String)> = None;
|
||||
|
||||
for artist in &results.items {
|
||||
let artist_lower = artist.name.to_lowercase();
|
||||
let artist_normalized = normalize_name(&artist.name);
|
||||
|
||||
// Exact match
|
||||
if artist_lower == name_lower && artist.albums_count.unwrap_or(0) > 0 {
|
||||
// Exact match (after accent normalization)
|
||||
// This allows "Mikael Åkerfeldt" to match "Mikael Akerfeldt"
|
||||
if artist_normalized == name_normalized && artist.albums_count.unwrap_or(0) > 0 {
|
||||
candidate = Some((artist.id, artist.name.clone()));
|
||||
break;
|
||||
}
|
||||
@@ -440,10 +602,10 @@ impl SuggestionsEngine {
|
||||
|
||||
// Also try "The X" variant (e.g., "Beatles" -> "The Beatles")
|
||||
if candidate.is_none() {
|
||||
let the_name_lower = format!("the {}", name_lower);
|
||||
let the_name_normalized = format!("the {}", name_normalized);
|
||||
for artist in &results.items {
|
||||
let artist_lower = artist.name.to_lowercase();
|
||||
if artist_lower == the_name_lower && artist.albums_count.unwrap_or(0) > 0 {
|
||||
let artist_normalized = normalize_name(&artist.name);
|
||||
if artist_normalized == the_name_normalized && artist.albums_count.unwrap_or(0) > 0 {
|
||||
candidate = Some((artist.id, artist.name.clone()));
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user