Search by RGID if available
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 2m0s
Cargo CI / Lint (pull_request) Successful in 1m5s

This commit is contained in:
Wojciech Kozlowski 2024-03-17 14:38:33 +01:00
parent 3ed13ca0e9
commit e008bf1b10
2 changed files with 52 additions and 7 deletions

View File

@ -21,6 +21,20 @@ struct Opt {
#[structopt(help = "Release group's artist MBID")]
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")]
title: String,
@ -28,6 +42,12 @@ struct Opt {
date: Option<Date>,
}
#[derive(StructOpt)]
struct OptRgid {
#[structopt(help = "Release group MBID")]
rgid: Uuid,
}
struct Date(AlbumDate);
impl FromStr for Date {
@ -64,8 +84,24 @@ fn main() {
let mut api = MusicBrainzApi::new(client);
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
.search_release_group(&arid, album)
.expect("failed to make API call");

View File

@ -11,7 +11,7 @@ use mockall::automock;
use crate::core::{
collection::{
album::{Album, AlbumDate, AlbumPrimaryType, AlbumSecondaryType},
musicbrainz::MbAlbumRef,
musicbrainz::{IMusicBrainzRef, MbAlbumRef},
},
interface::musicbrainz::{Error, IMusicBrainz, Match, Mbid},
};
@ -74,10 +74,19 @@ impl<Mbc: IMusicBrainzApiClient> IMusicBrainz for MusicBrainzApi<Mbc> {
) -> Result<Vec<Match<Album>>, Error> {
let title = &album.id.title;
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 {
query.push_str(&format!(" AND firstreleasedate:{year}"));
match album.musicbrainz {
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();
@ -292,7 +301,7 @@ mod tests {
let url = format!(
"https://musicbrainz.org/ws/2\
/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",
arid = "00000000-0000-0000-0000-000000000000",
year = "1986"