Skip to content

Commit

Permalink
Added ability to pull cover art from album artist
Browse files Browse the repository at this point in the history
  • Loading branch information
ryze312 committed Feb 12, 2023
1 parent e95168c commit 9c854f0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/discord_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
32 changes: 26 additions & 6 deletions src/discord_client/music_brainz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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();
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<String>, album: &Option<String>) -> Option<String> {
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<String> {
let result = match ReleaseGroup::search(query).execute() {
Ok(res) => res,
Err(_) => return None
Expand Down
2 changes: 2 additions & 0 deletions src/mpv_event_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ impl MpvEventQueue {
fn get_file_info_event(&self) -> Option<MpvEvent> {
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
Expand Down
1 change: 1 addition & 0 deletions src/mpv_event_queue/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct FileInfo {

pub struct FileMetadata {
pub artist: Option<String>,
pub album_artist: Option<String>,
pub album: Option<String>,
pub title: Option<String>,
pub track: Option<String>
Expand Down

0 comments on commit 9c854f0

Please sign in to comment.