diff --git a/src/discord_client.rs b/src/discord_client.rs index df6eb38..7385fd0 100644 --- a/src/discord_client.rs +++ b/src/discord_client.rs @@ -157,7 +157,7 @@ impl DiscordClient { return ("logo".to_string(), "mpv".to_string()) } - let cover_art_url = music_brainz::get_cover_art_url(&metadata.title, &metadata.album, &metadata.artist); + let cover_art_url = music_brainz::get_cover_art_url(&metadata.title, &metadata.album, &metadata.artist, &metadata.album_artist); let large_image = match cover_art_url { Some(url) => url, None => "logo".to_string() diff --git a/src/discord_client/music_brainz.rs b/src/discord_client/music_brainz.rs index f682b74..0139c57 100644 --- a/src/discord_client/music_brainz.rs +++ b/src/discord_client/music_brainz.rs @@ -2,26 +2,46 @@ use musicbrainz_rs::entity::release_group::{ReleaseGroup, ReleaseGroupSearchQuer use musicbrainz_rs::entity::CoverartResponse; use musicbrainz_rs::{Search, FetchCoverart}; -pub fn get_cover_art_url(title: &Option, album: &Option, artist: &Option) -> Option{ +pub fn get_cover_art_url(title: &Option, album: &Option, artist: &Option, album_artist: &Option) -> Option{ + get_track_cover_art(artist, title).or( + get_album_cover_art(album_artist, album) + ) +} + +fn get_track_cover_art(artist: &Option, title: &Option) -> Option { let mut builder = ReleaseGroupSearchQuery::query_builder(); if let Some(ref title) = title { builder.release_group(title); } + if let Some(ref artist) = artist { + // Some artist fields might contain + characters + // Pointing at multiple artists + for part in artist.split("+") { + builder.and().artist(part); + } + } + get_cover_art_from_query(builder.build()) +} + +fn get_album_cover_art(album_artist: &Option, album: &Option) -> Option { + let mut builder = ReleaseGroupSearchQuery::query_builder(); if let Some(ref album) = album { - builder.or().release_group(album); + builder.release_group(album); } - if let Some(ref artist) = artist { + if let Some(ref album_artist) = album_artist { // Some artist fields might contain + characters // Pointing at multiple artists - for part in artist.split("+") { + for part in album_artist.split("+") { builder.and().artist(part); } } - let query = builder.build(); - + get_cover_art_from_query(builder.build()) +} + +fn get_cover_art_from_query(query: String) -> Option { let result = match ReleaseGroup::search(query).execute() { Ok(res) => res, Err(_) => return None diff --git a/src/mpv_event_queue.rs b/src/mpv_event_queue.rs index c53312f..3494dae 100644 --- a/src/mpv_event_queue.rs +++ b/src/mpv_event_queue.rs @@ -78,12 +78,14 @@ impl MpvEventQueue { fn get_file_info_event(&self) -> Option { let filename = self.mpv.get_property("filename").unwrap(); let artist = self.mpv.get_property("metadata/by-key/artist").ok(); + let album_artist = self.mpv.get_property("metadata/by-key/album_artist").ok(); let album = self.mpv.get_property("metadata/by-key/album").ok(); let title = self.mpv.get_property("metadata/by-key/title").ok(); let track = self.mpv.get_property("metadata/by-key/track").ok(); let metadata = FileMetadata { artist, + album_artist, album, title, track diff --git a/src/mpv_event_queue/events.rs b/src/mpv_event_queue/events.rs index a11aaf6..f88f07e 100644 --- a/src/mpv_event_queue/events.rs +++ b/src/mpv_event_queue/events.rs @@ -5,6 +5,7 @@ pub struct FileInfo { pub struct FileMetadata { pub artist: Option, + pub album_artist: Option, pub album: Option, pub title: Option, pub track: Option