From 4bc93de87e977dbe6ed87140d0165aa897cc0540 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 21 Feb 2024 19:06:23 +0200 Subject: [PATCH] implement yt-dlp download --- src/dl/yt_dlp.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index c83c0f8..e3a2395 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -1,4 +1,5 @@ use super::spawn::{spawn, SpawnError}; +use std::fs; use core::fmt; use ordered_float::OrderedFloat; use serde::Deserialize; @@ -133,6 +134,7 @@ pub enum YtDlpError { SpawnError(SpawnError), ErrorMessage(String), // keep it separate type if we ever plan to parse yt-dlp errors JsonError, + NoFilePresent } // ^(?:ERROR: \[.*\] \S* )(.*$) - regex for matching yt-dlp's youtube errors @@ -158,6 +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") } } } @@ -170,6 +173,28 @@ impl YtDlp { Ok(YtDlpInfo::parse(&output.stdout)?) } + + pub async fn download(url: &str, format_id: &str, output_path: &str) -> Result<(), YtDlpError> { + spawn( + "python", + [ + "-m", + "yt_dlp", + url, + "-f", + format_id, + "-o", + output_path, + "--force-overwrites", + ], + ) + .await?; + + match fs::metadata(output_path) { + Ok(_) => Ok(()), + Err(_) => Err(YtDlpError::NoFilePresent) + } + } } #[cfg(test)]