use tokio process instead of sync one

This commit is contained in:
mykola2312 2024-02-19 13:12:52 +02:00
parent 5f3f2096e8
commit 2e57b92e32
4 changed files with 17 additions and 30 deletions

View file

@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.75"
dotenv = "0.15.0"
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros"] }
tokio = { version = "1.32.0", features = ["rt-multi-thread", "macros", "process"] }
teloxide = { version = "0.12.2", git ="https://github.com/teloxide/teloxide", features = ["macros"] }
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.113"

View file

@ -1,2 +1 @@
pub mod spawn;
pub mod yt_dlp;

View file

@ -1,24 +0,0 @@
use std::process::{Command, Output};
use tokio::task::{spawn_blocking, JoinError};
pub enum SpawnError {
CommandError(std::io::Error),
SpawnBlockingError(JoinError),
}
pub async fn spawn_process(program: String, args: Vec<String>) -> Result<Output, SpawnError> {
let output = spawn_blocking(move || {
let output = Command::new(program).args(args).output();
match output {
Ok(output) => Ok(output),
Err(e) => Err(SpawnError::CommandError(e)),
}
})
.await;
match output {
Ok(output) => output,
Err(e) => Err(SpawnError::SpawnBlockingError(e)),
}
}

View file

@ -1,6 +1,7 @@
use serde::Deserialize;
use serde_json;
use std::process::Command;
use std::process::Output;
use tokio::process::Command;
#[derive(Deserialize, Debug)]
pub struct YtDlpFormat {
@ -23,12 +24,23 @@ pub struct YtDlpInfo {
pub formats: Vec<YtDlpFormat>,
}
enum YtDlpError {
SpawnError,
pub enum YtDlpError {
CommandError(std::io::Error),
}
pub struct YtDlp {}
impl YtDlp {
pub async fn load_info(url: &str) {}
pub async fn load_info(url: &str) -> Result<(), YtDlpError> {
let output = match Command::new("python")
.args(["-m", "yt_dlp", url, "-j"])
.output()
.await
{
Ok(output) => output,
Err(e) => return Err(YtDlpError::CommandError(e)),
};
Ok(())
}
}