From 02265981d80cc2b9eab2b487fc0ba711e2e1a0e5 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 21 Feb 2024 19:19:54 +0200 Subject: [PATCH] implementing some downloady stuff --- src/dl.rs | 27 ++++++++++++++++++++++++--- src/dl/yt_dlp.rs | 8 ++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/dl.rs b/src/dl.rs index 2643cdd..b885e76 100644 --- a/src/dl.rs +++ b/src/dl.rs @@ -1,12 +1,16 @@ +use std::path::Path; + use self::spawn::SpawnError; -use self::yt_dlp::YtDlpError; +use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo}; pub mod ffmpeg; mod spawn; pub mod yt_dlp; pub enum DownloadError { - Message(String) + Message(String), + NoFormatFound, + MakePathError } impl From for DownloadError { @@ -21,7 +25,24 @@ impl From for DownloadError { } } +fn make_download_path(info: &YtDlpInfo, format: &YtDlpFormat) -> Option { + std::env::temp_dir() + .join(format!("{}.{}", info.id, format.ext)) + .into_os_string() + .into_string() + .ok() +} + pub async fn download(url: &str) -> Result { + let info = YtDlp::load_info(url).await?; + let av = match info.best_av_format() { + Some(av) => av, + None => return Err(DownloadError::NoFormatFound), + }; + let output_path = match make_download_path(&info, &av) { + Some(path) => path, + None => return Err(DownloadError::MakePathError) + }; todo!() -} \ No newline at end of file +} diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index e3a2395..491de3e 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -1,9 +1,9 @@ use super::spawn::{spawn, SpawnError}; -use std::fs; use core::fmt; use ordered_float::OrderedFloat; use serde::Deserialize; use serde_json; +use std::fs; #[derive(Deserialize, Debug)] pub struct YtDlpFormat { @@ -134,7 +134,7 @@ pub enum YtDlpError { SpawnError(SpawnError), ErrorMessage(String), // keep it separate type if we ever plan to parse yt-dlp errors JsonError, - NoFilePresent + NoFilePresent, } // ^(?:ERROR: \[.*\] \S* )(.*$) - regex for matching yt-dlp's youtube errors @@ -160,7 +160,7 @@ impl fmt::Display for YtDlpError { YTE::SpawnError(e) => write!(f, "{}", e), YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg), YTE::JsonError => write!(f, "json parsing error"), - YTE::NoFilePresent => write!(f, "downloaded file doesn't exists") + YTE::NoFilePresent => write!(f, "downloaded file doesn't exists"), } } } @@ -192,7 +192,7 @@ impl YtDlp { match fs::metadata(output_path) { Ok(_) => Ok(()), - Err(_) => Err(YtDlpError::NoFilePresent) + Err(_) => Err(YtDlpError::NoFilePresent), } } }