Audiphi/examples/probe.rs
2026-10-24 17:58:16 -05:00

53 lines
3.2 KiB
Rust

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Command-line probe of the core: `cargo run --example probe -- resolve <url|text> [top|discography]`
//! or `... download <url|text>` to run the whole pipeline into /tmp.
use audiphi::core::{downloader::*, models::*, resolvers, settings::Settings};
use std::sync::{mpsc, Arc};
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
let mut snap = Settings::load().snapshot();
if let Ok(id) = std::env::var("SPOT_ID") { snap["spotify"]["client_id"] = id.into(); snap["spotify"]["client_secret"] = std::env::var("SPOT_SECRET").unwrap_or_default().into(); }
let log = |k: &str, t: &str| eprintln!("[{k}] {t}");
match args[0].as_str() {
"resolve" | "download" => {
let mode = args.get(2).map(|s| s.as_str()).unwrap_or("top");
let r = match resolvers::resolve(&args[1], &snap, &log, mode) {
Ok(r) => r,
Err(e) => { println!("ERROR: {e}"); std::process::exit(1) }
};
println!("{} | {} | {} | {} tracks | service {} | note: {}", r.kind, r.title, r.subtitle, r.tracks.len(), r.service, r.note);
for t in r.tracks.iter().take(6) {
println!(" - {} – {} [{}] {:?}s isrc={} dl={} cover={}", t.artist(), t.title, t.album, t.duration, t.isrc, !t.download_url.is_empty(), !t.cover_url.is_empty());
}
if args[0] == "download" {
let (tx, rx) = mpsc::channel();
let mut m = Manager::new(2, tx, Arc::new(|| {}));
let o = DownloadOptions { output_dir: "/tmp/audiphi-test".into(), template: "{artist} - {title}".into(), format: args.get(3).cloned().unwrap_or("mp3".into()), bitrate: "192".into(), sample_rate: "source".into(), bit_depth: "16".into(), lyrics: "synced".into(), save_lrc: true, collection_folder: false };
let n = r.tracks.len().min(1);
m.enqueue(&r.tracks[..n], &o, &snap);
loop {
while let Ok(l) = rx.try_recv() { if l.kind != "progress" { eprintln!("[{}] {}", l.kind, l.text); } }
if m.counts().0 == 0 { break; }
std::thread::sleep(std::time::Duration::from_millis(200));
}
while let Ok(l) = rx.try_recv() { eprintln!("[{}] {}", l.kind, l.text); }
for h in &m.jobs { let j = h.job.lock().unwrap(); println!("{:?} {} {}", j.state, j.status, j.output_path); }
}
}
"meta" => {
let mut t = Track::new();
t.title = args[1].clone();
t.artists = vec![args[2].clone()];
let r = audiphi::core::metadata::search_all(&t, &["deezer".into(), "itunes".into(), "musicbrainz".into()], 1200, &log);
for (k, v) in r {
println!("{k}: {} candidates", v.len());
for c in v.iter().take(2) {
println!(" {:.2} {} – {} [{}] {} #{:?}/{:?} {} cover={}", c.score, c.artist(), c.title, c.album, c.year, c.track_number, c.track_total, c.genre, !c.cover_url.is_empty());
}
}
let l = audiphi::core::lyrics::fetch(&args[1], &args[2], "", Some(213.0));
println!("lyrics: {:?}", l.map(|l| (l.plain.len(), l.synced.len(), l.instrumental)));
}
_ => {}
}
}