musichoard/examples/musicbrainz_api/search_release_group.rs

111 lines
2.7 KiB
Rust
Raw Normal View History

#![allow(non_snake_case)]
use std::{num::ParseIntError, str::FromStr};
use musichoard::{
collection::album::{Album, AlbumDate, AlbumId},
external::musicbrainz::api::{client::MusicBrainzApiClient, MusicBrainzApi},
interface::musicbrainz::{IMusicBrainz, Mbid},
};
use structopt::StructOpt;
use uuid::Uuid;
const USER_AGENT: &str = concat!(
"MusicHoard---examples---musicbrainz-api---search-release-group/",
env!("CARGO_PKG_VERSION"),
" ( musichoard@thenineworlds.net )"
);
#[derive(StructOpt)]
struct Opt {
#[structopt(help = "Release group's artist MBID")]
arid: Uuid,
2024-03-17 14:38:33 +01:00
#[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,
#[structopt(help = "Release group release date")]
date: Option<Date>,
}
2024-03-17 14:38:33 +01:00
#[derive(StructOpt)]
struct OptRgid {
#[structopt(help = "Release group MBID")]
rgid: Uuid,
}
struct Date(AlbumDate);
impl FromStr for Date {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut elems = s.split('-');
let elem = elems.next();
let year = elem.map(|s| s.parse()).transpose()?;
let elem = elems.next();
let month = elem.map(|s| s.parse()).transpose()?;
let elem = elems.next();
let day = elem.map(|s| s.parse()).transpose()?;
Ok(Date(AlbumDate::new(year, month, day)))
}
}
impl From<Date> for AlbumDate {
fn from(value: Date) -> Self {
value.0
}
}
fn main() {
let opt = Opt::from_args();
println!("USER_AGENT: {USER_AGENT}");
let client = MusicBrainzApiClient::new(USER_AGENT).expect("failed to create API client");
let mut api = MusicBrainzApi::new(client);
let arid: Mbid = opt.arid.into();
2024-03-17 14:38:33 +01:00
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
2024-03-17 17:14:15 +01:00
.search_release_group(&arid, &album)
.expect("failed to make API call");
println!("{matches:#?}");
}