Added ability to pull cover art from album artist

This commit is contained in:
Ryze 2023-02-12 15:02:18 +03:00
parent e95168ccf4
commit 9c854f0c5b
Signed by: ryze
GPG key ID: 9B296C5CEAEAAAC1
4 changed files with 31 additions and 8 deletions

View file

@ -157,7 +157,7 @@ impl DiscordClient {
return ("logo".to_string(), "mpv".to_string()) 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 { let large_image = match cover_art_url {
Some(url) => url, Some(url) => url,
None => "logo".to_string() None => "logo".to_string()

View file

@ -2,16 +2,18 @@ use musicbrainz_rs::entity::release_group::{ReleaseGroup, ReleaseGroupSearchQuer
use musicbrainz_rs::entity::CoverartResponse; use musicbrainz_rs::entity::CoverartResponse;
use musicbrainz_rs::{Search, FetchCoverart}; use musicbrainz_rs::{Search, FetchCoverart};
pub fn get_cover_art_url(title: &Option<String>, album: &Option<String>, artist: &Option<String>) -> Option<String>{ pub fn get_cover_art_url(title: &Option<String>, album: &Option<String>, artist: &Option<String>, album_artist: &Option<String>) -> Option<String>{
get_track_cover_art(artist, title).or(
get_album_cover_art(album_artist, album)
)
}
fn get_track_cover_art(artist: &Option<String>, title: &Option<String>) -> Option<String> {
let mut builder = ReleaseGroupSearchQuery::query_builder(); let mut builder = ReleaseGroupSearchQuery::query_builder();
if let Some(ref title) = title { if let Some(ref title) = title {
builder.release_group(title); builder.release_group(title);
} }
if let Some(ref album) = album {
builder.or().release_group(album);
}
if let Some(ref artist) = artist { if let Some(ref artist) = artist {
// Some artist fields might contain + characters // Some artist fields might contain + characters
// Pointing at multiple artists // Pointing at multiple artists
@ -19,9 +21,27 @@ pub fn get_cover_art_url(title: &Option<String>, album: &Option<String>, artist:
builder.and().artist(part); builder.and().artist(part);
} }
} }
get_cover_art_from_query(builder.build())
}
let query = builder.build(); fn get_album_cover_art(album_artist: &Option<String>, album: &Option<String>) -> Option<String> {
let mut builder = ReleaseGroupSearchQuery::query_builder();
if let Some(ref album) = album {
builder.release_group(album);
}
if let Some(ref album_artist) = album_artist {
// Some artist fields might contain + characters
// Pointing at multiple artists
for part in album_artist.split("+") {
builder.and().artist(part);
}
}
get_cover_art_from_query(builder.build())
}
fn get_cover_art_from_query(query: String) -> Option<String> {
let result = match ReleaseGroup::search(query).execute() { let result = match ReleaseGroup::search(query).execute() {
Ok(res) => res, Ok(res) => res,
Err(_) => return None Err(_) => return None

View file

@ -78,12 +78,14 @@ impl MpvEventQueue {
fn get_file_info_event(&self) -> Option<MpvEvent> { fn get_file_info_event(&self) -> Option<MpvEvent> {
let filename = self.mpv.get_property("filename").unwrap(); let filename = self.mpv.get_property("filename").unwrap();
let artist = self.mpv.get_property("metadata/by-key/artist").ok(); 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 album = self.mpv.get_property("metadata/by-key/album").ok();
let title = self.mpv.get_property("metadata/by-key/title").ok(); let title = self.mpv.get_property("metadata/by-key/title").ok();
let track = self.mpv.get_property("metadata/by-key/track").ok(); let track = self.mpv.get_property("metadata/by-key/track").ok();
let metadata = FileMetadata { let metadata = FileMetadata {
artist, artist,
album_artist,
album, album,
title, title,
track track

View file

@ -5,6 +5,7 @@ pub struct FileInfo {
pub struct FileMetadata { pub struct FileMetadata {
pub artist: Option<String>, pub artist: Option<String>,
pub album_artist: Option<String>,
pub album: Option<String>, pub album: Option<String>,
pub title: Option<String>, pub title: Option<String>,
pub track: Option<String> pub track: Option<String>