musichoard/examples/musicbrainz_api/browse.rs

95 lines
2.7 KiB
Rust

use std::{thread, time};
use musichoard::{
collection::musicbrainz::Mbid,
external::musicbrainz::{
api::{browse::BrowseReleaseGroupRequest, MusicBrainzClient, NextPage, PageSettings},
http::MusicBrainzHttp,
},
};
use structopt::StructOpt;
use uuid::Uuid;
const USER_AGENT: &str = concat!(
"MusicHoard---examples---musicbrainz-api---browse/",
env!("CARGO_PKG_VERSION"),
" ( musichoard@thenineworlds.net )"
);
#[derive(StructOpt)]
struct Opt {
#[structopt(subcommand)]
entity: OptEntity,
}
#[derive(StructOpt)]
enum OptEntity {
#[structopt(about = "Browse release groups")]
ReleaseGroup(OptReleaseGroup),
}
#[derive(StructOpt)]
enum OptReleaseGroup {
#[structopt(about = "Browse release groups of an artist")]
Artist(OptMbid),
}
#[derive(StructOpt)]
struct OptMbid {
#[structopt(help = "MBID of the entity")]
mbid: Uuid,
}
fn main() {
let opt = Opt::from_args();
println!("USER_AGENT: {USER_AGENT}");
let http = MusicBrainzHttp::new(USER_AGENT).expect("failed to create API client");
let mut client = MusicBrainzClient::new(http);
match opt.entity {
OptEntity::ReleaseGroup(opt_release_group) => match opt_release_group {
OptReleaseGroup::Artist(opt_mbid) => {
let mbid: Mbid = opt_mbid.mbid.into();
let request = BrowseReleaseGroupRequest::artist(&mbid);
let mut paging = PageSettings::with_max_limit();
let mut response_counts: Vec<usize> = Vec::new();
loop {
let response = client
.browse_release_group(&request, &paging)
.expect("failed to make API call");
for rg in response.release_groups.iter() {
println!("{rg:?}\n");
}
let count = response.release_groups.len();
response_counts.push(count);
println!("Release groups in this response: {count}");
match response.page.next_page_offset(count) {
NextPage::Offset(next_offset) => paging.with_offset(next_offset),
NextPage::Complete => break,
}
thread::sleep(time::Duration::from_secs(1));
}
println!(
"Total: {}={} release groups",
response_counts
.iter()
.map(|i| i.to_string())
.collect::<Vec<_>>()
.join("+"),
response_counts.iter().sum::<usize>(),
);
}
},
}
}