The MusicBrainz API search call should use the MBID if available #171

Merged
wojtek merged 2 commits from 169---the-musicbrainz-api-search-call-should-use-the-mbid-if-available into main 2024-03-17 17:18:06 +01:00
2 changed files with 52 additions and 7 deletions
Showing only changes of commit e008bf1b10 - Show all commits

View File

@ -21,6 +21,20 @@ struct Opt {
#[structopt(help = "Release group's artist MBID")] #[structopt(help = "Release group's artist MBID")]
arid: Uuid, arid: Uuid,
#[structopt(subcommand)]
command: OptCommand,
}
#[derive(StructOpt)]
enum OptCommand {
#[structopt(about = "Search by title (and date)")]
Title(OptTitle),
#[structopt(about = "Search by release group MBID")]
Rgid(OptRgid),
}
#[derive(StructOpt)]
struct OptTitle {
#[structopt(help = "Release group title")] #[structopt(help = "Release group title")]
title: String, title: String,
@ -28,6 +42,12 @@ struct Opt {
date: Option<Date>, date: Option<Date>,
} }
#[derive(StructOpt)]
struct OptRgid {
#[structopt(help = "Release group MBID")]
rgid: Uuid,
}
struct Date(AlbumDate); struct Date(AlbumDate);
impl FromStr for Date { impl FromStr for Date {
@ -64,8 +84,24 @@ fn main() {
let mut api = MusicBrainzApi::new(client); let mut api = MusicBrainzApi::new(client);
let arid: Mbid = opt.arid.into(); let arid: Mbid = opt.arid.into();
let date: AlbumDate = opt.date.map(Into::into).unwrap_or_default();
let album = Album::new(AlbumId::new(opt.title), date, None, vec![]); let album = match opt.command {
OptCommand::Title(opt_title) => {
let date: AlbumDate = opt_title.date.map(Into::into).unwrap_or_default();
Album::new(AlbumId::new(opt_title.title), date, None, vec![])
}
OptCommand::Rgid(opt_rgid) => {
let mut album = Album::new(
AlbumId::new(String::default()),
AlbumDate::default(),
None,
vec![],
);
album.set_musicbrainz_ref(opt_rgid.rgid.into());
album
}
};
let matches = api let matches = api
.search_release_group(&arid, album) .search_release_group(&arid, album)
.expect("failed to make API call"); .expect("failed to make API call");

View File

@ -11,7 +11,7 @@ use mockall::automock;
use crate::core::{ use crate::core::{
collection::{ collection::{
album::{Album, AlbumDate, AlbumPrimaryType, AlbumSecondaryType}, album::{Album, AlbumDate, AlbumPrimaryType, AlbumSecondaryType},
musicbrainz::MbAlbumRef, musicbrainz::{IMusicBrainzRef, MbAlbumRef},
}, },
interface::musicbrainz::{Error, IMusicBrainz, Match, Mbid}, interface::musicbrainz::{Error, IMusicBrainz, Match, Mbid},
}; };
@ -74,10 +74,19 @@ impl<Mbc: IMusicBrainzApiClient> IMusicBrainz for MusicBrainzApi<Mbc> {
) -> Result<Vec<Match<Album>>, Error> { ) -> Result<Vec<Match<Album>>, Error> {
let title = &album.id.title; let title = &album.id.title;
let arid = arid.uuid().as_hyphenated().to_string(); let arid = arid.uuid().as_hyphenated().to_string();
let mut query = format!("releasegroup:\"{title}\" AND arid:{arid}"); let mut query = format!("arid:{arid}");
if let Some(year) = album.date.year { match album.musicbrainz {
query.push_str(&format!(" AND firstreleasedate:{year}")); Some(mbref) => {
let rgid = mbref.mbid().uuid().as_hyphenated().to_string();
query.push_str(&format!(" AND rgid:{rgid}"));
}
None => {
query.push_str(&format!(" AND releasegroup:\"{title}\""));
if let Some(year) = album.date.year {
query.push_str(&format!(" AND firstreleasedate:{year}"));
}
}
} }
let query: String = form_urlencoded::byte_serialize(query.as_bytes()).collect(); let query: String = form_urlencoded::byte_serialize(query.as_bytes()).collect();
@ -292,7 +301,7 @@ mod tests {
let url = format!( let url = format!(
"https://musicbrainz.org/ws/2\ "https://musicbrainz.org/ws/2\
/release-group\ /release-group\
?query=releasegroup%3A%22{title}%22+AND+arid%3A{arid}+AND+firstreleasedate%3A{year}", ?query=arid%3A{arid}+AND+releasegroup%3A%22{title}%22+AND+firstreleasedate%3A{year}",
title = "an+album", title = "an+album",
arid = "00000000-0000-0000-0000-000000000000", arid = "00000000-0000-0000-0000-000000000000",
year = "1986" year = "1986"