implementing some downloady stuff
This commit is contained in:
parent
c2027ba0f8
commit
2aa3974896
2 changed files with 28 additions and 7 deletions
25
src/dl.rs
25
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<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> {
|
||||
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!()
|
||||
}
|
||||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue