137 lines
5.4 KiB
Rust
137 lines
5.4 KiB
Rust
use std::{collections::HashMap, ffi::OsStr, process::{Command, Stdio}, sync::{Arc, Mutex, MutexGuard}};
|
|
use xmpd_manifest::song::{Song, SourceType};
|
|
|
|
|
|
lazy_static::lazy_static!(
|
|
static ref SONG_CACHE_DL: Arc<Mutex<SongCacheDl>> = Arc::default();
|
|
);
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
|
|
pub enum SongStatus {
|
|
Downloading,
|
|
Converting,
|
|
Done
|
|
}
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
pub struct SongCacheDl {
|
|
pub jobs: HashMap<uuid::Uuid, SongStatus>,
|
|
pub current_jobs: usize,
|
|
}
|
|
|
|
impl SongCacheDl {
|
|
pub fn get() -> crate::Result<MutexGuard<'static, Self>> {
|
|
match SONG_CACHE_DL.lock() {
|
|
Ok(v) => Ok(v),
|
|
Err(e) => anyhow::bail!(format!("{e:?}"))
|
|
}
|
|
}
|
|
pub fn is_job_list_full(&self) -> bool {
|
|
self.current_jobs >= 5
|
|
}
|
|
|
|
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
|
|
self.current_jobs += 1;
|
|
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
|
|
let tooling = xmpd_settings::Settings::get()?.tooling.clone();
|
|
let mut song_cache_d = xmpd_cliargs::CLIARGS.cache_path();
|
|
song_cache_d.push("songs");
|
|
match song.source_type() {
|
|
SourceType::Youtube |
|
|
SourceType::Soundcloud => {
|
|
let mut song_p = song_cache_d.clone();
|
|
song_p.push(sid.to_string());
|
|
let song_p = song_p.with_extension(&song_format);
|
|
|
|
let mut dl_cmd = Command::new(&tooling.ytdlp_path);
|
|
dl_cmd.arg(song.url().as_str());
|
|
dl_cmd.args(["-x", "--audio-format", &song_format]);
|
|
dl_cmd.arg("-o");
|
|
dl_cmd.arg(&song_p);
|
|
|
|
if xmpd_cliargs::CLIARGS.debug {
|
|
dl_cmd.stdout(Stdio::piped());
|
|
dl_cmd.stderr(Stdio::piped());
|
|
} else {
|
|
dl_cmd.stdout(Stdio::null());
|
|
dl_cmd.stderr(Stdio::null());
|
|
}
|
|
let dl_child = dl_cmd.spawn()?;
|
|
self.jobs.insert(sid, SongStatus::Downloading);
|
|
std::thread::spawn(move || {
|
|
if let Ok(output) = dl_child.wait_with_output() {
|
|
for line in String::from_utf8(output.stdout).unwrap().lines() {
|
|
log::info!("CMD: {}", line);
|
|
}
|
|
for line in String::from_utf8(output.stderr).unwrap().lines() {
|
|
log::error!("CMD: {}", line);
|
|
}
|
|
}
|
|
let mut cache = SONG_CACHE_DL.lock().unwrap();
|
|
cache.jobs.insert(sid, SongStatus::Done);
|
|
cache.current_jobs -= 1;
|
|
});
|
|
}
|
|
|
|
SourceType::Spotify => {
|
|
// Spotdl doesnt have webm as a format so its fucking annoying, oh well
|
|
let mut song_p = song_cache_d.clone();
|
|
song_p.push(sid.to_string());
|
|
song_p.push("{output-ext}");
|
|
let mut dl_cmd = Command::new(&tooling.spotdl_path);
|
|
dl_cmd.arg(song.url().as_str());
|
|
dl_cmd.arg("--ffmpeg");
|
|
dl_cmd.arg(&tooling.ffmpeg_path);
|
|
dl_cmd.args(["--format", &song_format, "--output"]);
|
|
dl_cmd.arg(&song_p);
|
|
let arg_str = dl_cmd.get_args();
|
|
let arg_str: Vec<_> = arg_str.collect();
|
|
let arg_str = arg_str.join(OsStr::new(" ")).to_string_lossy().to_string();
|
|
log::debug!("spotify cli: {} {}", tooling.spotdl_path, arg_str);
|
|
if xmpd_cliargs::CLIARGS.debug {
|
|
dl_cmd.stdout(Stdio::piped());
|
|
dl_cmd.stderr(Stdio::piped());
|
|
} else {
|
|
dl_cmd.stdout(Stdio::null());
|
|
dl_cmd.stderr(Stdio::null());
|
|
}
|
|
let child = dl_cmd.spawn()?;
|
|
self.jobs.insert(sid, SongStatus::Downloading);
|
|
std::thread::spawn(move || {
|
|
if let Ok(output) = child.wait_with_output() {
|
|
for line in String::from_utf8(output.stdout).unwrap().lines() {
|
|
log::info!("CMD: {}", line);
|
|
}
|
|
for line in String::from_utf8(output.stderr).unwrap().lines() {
|
|
log::error!("CMD: {}", line);
|
|
}
|
|
}
|
|
let mut from = song_p.clone();
|
|
from.pop();
|
|
from.push("{song_format}.{song_format}");
|
|
|
|
let mut to = song_p.clone();
|
|
to.pop();
|
|
to.set_extension(&song_format);
|
|
std::fs::copy(&from, &to).unwrap();
|
|
from.pop();
|
|
std::fs::remove_dir_all(from).unwrap();
|
|
let mut cache = SONG_CACHE_DL.lock().unwrap();
|
|
cache.jobs.insert(sid, SongStatus::Done);
|
|
cache.current_jobs -= 1;
|
|
});
|
|
}
|
|
SourceType::HttpBare => {
|
|
todo!()
|
|
}
|
|
SourceType::Http7z => {
|
|
todo!()
|
|
}
|
|
SourceType::HttpZip => {
|
|
todo!()
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|