From dc01b36771a0c32b591656e6c0d98c78d27d06ed Mon Sep 17 00:00:00 2001 From: MCorange Date: Thu, 19 Sep 2024 01:06:34 +0300 Subject: [PATCH] :3 --- Cargo.lock | 1 + Cargo.toml | 2 +- src/config/downloader/links.rs | 37 +++++++++++ src/config/downloader/mod.rs | 111 +++++++++++++++++++++++++++++++++ src/config/mod.rs | 4 +- 5 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/config/downloader/links.rs create mode 100644 src/config/downloader/mod.rs diff --git a/Cargo.lock b/Cargo.lock index d948534..dd1ec66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3272,6 +3272,7 @@ dependencies = [ "base64", "bytes", "encoding_rs", + "futures-channel", "futures-core", "futures-util", "h2", diff --git a/Cargo.toml b/Cargo.toml index e24a30d..97c7301 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ libc = "0.2.153" log = "0.4.21" notify-rust = "4.11.3" open = "5.3.0" -reqwest = "0.12.3" +reqwest = { version = "0.12.3", features = ["blocking"] } serde = { version = "1.0.197", features = ["derive"] } serde_json = "1.0.115" # serde_traitobject = "0.2.8" diff --git a/src/config/downloader/links.rs b/src/config/downloader/links.rs new file mode 100644 index 0000000..6cd1e9e --- /dev/null +++ b/src/config/downloader/links.rs @@ -0,0 +1,37 @@ + + +#[cfg(all(target_os="windows", target_arch="x86_64"))] +pub const YTDLP: &str = "https://github.com/yt-dlp/yt-dlp/releases/download/2024.08.06/yt-dlp_min.exe"; + +#[cfg(all(target_os="linux", target_arch="x86_64"))] +pub const YTDLP: &str = "https://github.com/yt-dlp/yt-dlp/releases/download/2024.08.06/yt-dlp_linux"; + +#[cfg(all(target_os="linux", target_arch="aarch64"))] +pub const YTDLP: &str = "https://github.com/yt-dlp/yt-dlp/releases/download/2024.08.06/yt-dlp_linux_aarch64"; + +#[cfg(all(target_os="windows", target_arch="x86_64"))] +pub const SPOTDL: &str = "https://github.com/spotDL/spotify-downloader/releases/download/v4.2.8/spotdl-4.2.8-win32.exe"; + +#[cfg(all(target_os="linux", target_arch="x86_64"))] +pub const SPOTDL: &str = "https://github.com/spotDL/spotify-downloader/releases/download/v4.2.8/spotdl-4.2.8-linux"; + +#[cfg(all(target_os="linux", target_arch="aarch64"))] +pub const SPOTDL: &str = ""; + +#[cfg(all(target_os="windows", target_arch="x86_64"))] +pub const FFMPEG: &str = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"; + +#[cfg(all(target_os="linux", target_arch="x86_64"))] +pub const FFMPEG: &str = "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"; + +#[cfg(all(target_os="linux", target_arch="aarch64"))] +pub const FFMPEG: &str = "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-arm64-static.tar.xz"; + + + +#[cfg(not(any( + all(target_os="windows", target_arch="x86_64"), + all(target_os="linux", target_arch="x86_64"), + all(target_os="linux", target_arch="aarch64"), +)))] +compile_error!("Target and or Architecture is not supported"); diff --git a/src/config/downloader/mod.rs b/src/config/downloader/mod.rs new file mode 100644 index 0000000..3fc09fb --- /dev/null +++ b/src/config/downloader/mod.rs @@ -0,0 +1,111 @@ +use std::path::{Path, PathBuf}; + +use crate::constants::EXEC_EXT; + +mod links; + +pub struct DepDownloader { + ytdlp: PathBuf, + spotdl: PathBuf, + ffmpeg: PathBuf, +} + + +impl DepDownloader { + pub fn new() -> anyhow::Result { + if !Path::new(".dep").exists() { + std::fs::create_dir(".dep")?; + } + + let ytdlp = Self::download_ytdlp()?; + + todo!() + } + + fn download_ytdlp() -> anyhow::Result { + if let Some(p) = crate::util::is_program_in_path("yt-dlp") { + return Ok(p); // Already exists on pc + } + + let mut fp = PathBuf::new(); + fp.push(".dep"); + fp.push("yt-dlp"); + fp.set_extension(EXEC_EXT); + + if fp.exists() { + return Ok(fp); + } + + let mut f = std::fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&fp)?; + + let mut req = reqwest::blocking::get(links::YTDLP)?; + req.copy_to(&mut f)?; + + Ok(fp) + } + + fn download_spotdl() -> anyhow::Result { + if let Some(p) = crate::util::is_program_in_path("spotdl") { + return Ok(p); // already exists on pc + } + + let mut fp = PathBuf::new(); + fp.push(".dep"); + fp.push("spotdl"); + fp.set_extension(EXEC_EXT); + + if fp.exists() { + return Ok(fp); + } + + if links::SPOTDL.is_empty() { + log::error!("spotdl doesnt support this architecture (probably aarch64)"); + log::error!("please install it manually, if you have installed it, make sure its accessible in path"); + anyhow::bail!(""); + } + + let mut f = std::fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&fp)?; + + let mut req = reqwest::blocking::get(links::SPOTDL)?; + req.copy_to(&mut f)?; + + Ok(fp) + } + + fn download_ffmpeg() -> anyhow::Result { + if let Some(p) = crate::util::is_program_in_path("ffmpeg") { + return Ok(p); // already exists on pc + } + + let mut fp = PathBuf::new(); + fp.push(".dep"); + fp.push("ffmpeg"); + fp.set_extension(EXEC_EXT); + + if fp.exists() { + return Ok(fp); + } + + if links::FFMPEG.is_empty() { + log::error!("spotdl doesnt support this architecture (probably aarch64)"); + log::error!("please install it manually, if you have installed it, make sure its accessible in path"); + anyhow::bail!(""); + } + + let mut f = std::fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(&fp)?; + + let mut req = reqwest::blocking::get(links::FFMPEG)?; + req.copy_to(&mut f)?; + + Ok(fp) + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 869fee5..fb40d73 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,12 +1,10 @@ pub mod cli; - +pub mod downloader; use std::path::PathBuf; - use clap::Parser; use serde::{Deserialize, Serialize}; use anyhow::Result; use crate::util::{self, isatty}; - use self::cli::CliArgs; // const YTDLP_DL_URL: &'static str = "https://github.com/yt-dlp/yt-dlp/archive/refs/heads/master.zip";