Downloading prototype works
This commit is contained in:
28
xmpd-cache/Cargo.toml
Normal file
28
xmpd-cache/Cargo.toml
Normal file
@@ -0,0 +1,28 @@
|
||||
[package]
|
||||
name = "xmpd-cache"
|
||||
edition = "2021"
|
||||
readme="README.md"
|
||||
authors.workspace = true
|
||||
version.workspace = true
|
||||
repository.workspace = true
|
||||
license.workspace = true
|
||||
autobins = false
|
||||
autotests = false
|
||||
autoexamples = false
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib"]
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
xmpd-settings.path = "../xmpd-settings"
|
||||
xmpd-manifest.path = "../xmpd-manifest"
|
||||
xmpd-cliargs.path = "../xmpd-cliargs"
|
||||
anyhow.workspace = true
|
||||
camino.workspace = true
|
||||
lazy_static.workspace = true
|
||||
log.workspace = true
|
||||
uuid.workspace = true
|
||||
0
xmpd-cache/README.md
Normal file
0
xmpd-cache/README.md
Normal file
0
xmpd-cache/src/downloader/icon.rs
Normal file
0
xmpd-cache/src/downloader/icon.rs
Normal file
0
xmpd-cache/src/downloader/metadata.rs
Normal file
0
xmpd-cache/src/downloader/metadata.rs
Normal file
4
xmpd-cache/src/downloader/mod.rs
Normal file
4
xmpd-cache/src/downloader/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
pub mod song;
|
||||
pub mod icon;
|
||||
pub mod metadata;
|
||||
127
xmpd-cache/src/downloader/song.rs
Normal file
127
xmpd-cache/src/downloader/song.rs
Normal file
@@ -0,0 +1,127 @@
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
122
xmpd-cache/src/lib.rs
Normal file
122
xmpd-cache/src/lib.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
use std::{collections::HashMap, str::FromStr, sync::{Arc, Mutex, MutexGuard}, time::Duration};
|
||||
use downloader::song::SongStatus;
|
||||
use xmpd_manifest::song::Song;
|
||||
|
||||
pub mod downloader;
|
||||
|
||||
type Result<T> = anyhow::Result<T>;
|
||||
|
||||
lazy_static::lazy_static!(
|
||||
static ref CACHE: Arc<Mutex<Cache>> = Arc::new(Mutex::new(Cache::default()));
|
||||
);
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Cache {
|
||||
cache_dir: camino::Utf8PathBuf,
|
||||
song_cache: HashMap<uuid::Uuid, DlStatus>,
|
||||
queue: Vec<(uuid::Uuid, Song)>
|
||||
// TODO: Add Icon, metadata cache
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DlStatus {
|
||||
Done(camino::Utf8PathBuf),
|
||||
Downloading,
|
||||
Error(String),
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn get() -> crate::Result<MutexGuard<'static, Self>> {
|
||||
match CACHE.lock() {
|
||||
Ok(l) => Ok(l),
|
||||
Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) -> Result<()> {
|
||||
start_cache_mv_thread();
|
||||
self.cache_dir = xmpd_cliargs::CLIARGS.cache_path();
|
||||
|
||||
{ // Get cached songs
|
||||
let mut song_cache_dir = self.cache_dir.clone();
|
||||
song_cache_dir.push("songs");
|
||||
for file in song_cache_dir.read_dir_utf8()? {
|
||||
if let Ok(file) = file {
|
||||
if !file.file_type()?.is_file() {
|
||||
log::warn!("Non song file in: {}", file.path());
|
||||
continue;
|
||||
}
|
||||
let file_path = file.path();
|
||||
let file2 = file_path.with_extension("");
|
||||
if let Some(file_name) = file2.file_name() {
|
||||
let id = uuid::Uuid::from_str(file_name)?;
|
||||
log::debug!("Found song {id}");
|
||||
// TODO: Check if id is in manifest
|
||||
self.song_cache.insert(id, DlStatus::Done(file_path.to_path_buf()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
{ // Get cached icons
|
||||
}
|
||||
{ // Get Cached meta
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn download_to_cache(&mut self, sid: uuid::Uuid, song: Song) {
|
||||
self.queue.push((sid, song));
|
||||
self.song_cache.insert(sid, DlStatus::Downloading);
|
||||
}
|
||||
|
||||
pub fn download_to_cache_multiple(&mut self, mut songs: Vec<(uuid::Uuid, Song)>) {
|
||||
while let Some((sid, song)) = songs.pop() {
|
||||
self.download_to_cache(sid, song);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_cached_song_status(&mut self, sid: &uuid::Uuid) -> Option<DlStatus> {
|
||||
Some(self.song_cache.get(sid)?.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn start_cache_mv_thread() {
|
||||
std::thread::spawn(|| {
|
||||
loop {
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
|
||||
let mut done_jobs = Vec::new();
|
||||
let mut dlc = downloader::song::SongCacheDl::get().unwrap();
|
||||
for (sid, status) in &dlc.jobs {
|
||||
if *status == SongStatus::Done {
|
||||
let mut cache = CACHE.lock().unwrap();
|
||||
let mut song_p = xmpd_cliargs::CLIARGS.cache_path().clone();
|
||||
song_p.push("songs");
|
||||
song_p.push(sid.clone().to_string());
|
||||
let song_p = song_p.with_extension(&song_format);
|
||||
log::debug!("Found done: {:?}: {}", song_p, song_p.exists());
|
||||
if song_p.exists() {
|
||||
cache.song_cache.insert(sid.clone(), DlStatus::Done(song_p));
|
||||
done_jobs.push(sid.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
for sid in done_jobs {
|
||||
dlc.jobs.remove(&sid);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut cache = Cache::get().unwrap();
|
||||
let mut dlc = downloader::song::SongCacheDl::get().unwrap();
|
||||
if !dlc.is_job_list_full() {
|
||||
if let Some((sid, song)) = cache.queue.pop() {
|
||||
dlc.download(sid, song).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user