implementing some downloady stuff

This commit is contained in:
mykola2312 2024-02-21 19:19:54 +02:00
parent 4bc93de87e
commit 02265981d8
2 changed files with 28 additions and 7 deletions

View file

@ -1,12 +1,16 @@
use std::path::Path;
use self::spawn::SpawnError; use self::spawn::SpawnError;
use self::yt_dlp::YtDlpError; use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo};
pub mod ffmpeg; pub mod ffmpeg;
mod spawn; mod spawn;
pub mod yt_dlp; pub mod yt_dlp;
pub enum DownloadError { pub enum DownloadError {
Message(String) Message(String),
NoFormatFound,
MakePathError
} }
impl From<SpawnError> for DownloadError { impl From<SpawnError> for DownloadError {
@ -21,7 +25,24 @@ impl From<YtDlpError> for DownloadError {
} }
} }
fn make_download_path(info: &YtDlpInfo, format: &YtDlpFormat) -> Option<String> {
std::env::temp_dir()
.join(format!("{}.{}", info.id, format.ext))
.into_os_string()
.into_string()
.ok()
}
pub async fn download(url: &str) -> Result<String, DownloadError> { pub async fn download(url: &str) -> Result<String, DownloadError> {
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!() todo!()
} }

View file

@ -1,9 +1,9 @@
use super::spawn::{spawn, SpawnError}; use super::spawn::{spawn, SpawnError};
use std::fs;
use core::fmt; use core::fmt;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use serde::Deserialize; use serde::Deserialize;
use serde_json; use serde_json;
use std::fs;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct YtDlpFormat { pub struct YtDlpFormat {
@ -134,7 +134,7 @@ pub enum YtDlpError {
SpawnError(SpawnError), SpawnError(SpawnError),
ErrorMessage(String), // keep it separate type if we ever plan to parse yt-dlp errors ErrorMessage(String), // keep it separate type if we ever plan to parse yt-dlp errors
JsonError, JsonError,
NoFilePresent NoFilePresent,
} }
// ^(?:ERROR: \[.*\] \S* )(.*$) - regex for matching yt-dlp's youtube errors // ^(?: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::SpawnError(e) => write!(f, "{}", e),
YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg), YTE::ErrorMessage(msg) => write!(f, "yt-dlp error - {}", msg),
YTE::JsonError => write!(f, "json parsing error"), 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) { match fs::metadata(output_path) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err(YtDlpError::NoFilePresent) Err(_) => Err(YtDlpError::NoFilePresent),
} }
} }
} }