Seperate stdout and stderr

This commit is contained in:
blank X 2021-06-29 10:57:37 +07:00
parent 2e029df64d
commit bd800f9b08
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
3 changed files with 17 additions and 22 deletions

View File

@ -200,7 +200,7 @@ async fn async_main() {
eprintln!("Failed to get video data: {:?}", err); eprintln!("Failed to get video data: {:?}", err);
waited = true; waited = true;
if let structs::Error::YoutubeDL(ref err) = err { if let structs::Error::YoutubeDL(ref err) = err {
let output = err.output.to_lowercase(); let output = err.stderr.to_lowercase();
if output.contains("429") if output.contains("429")
|| output.contains("too many request") || output.contains("too many request")
|| output.contains("technical difficult") || output.contains("technical difficult")
@ -210,12 +210,12 @@ async fn async_main() {
*guard += 1; *guard += 1;
continue; continue;
} else if err } else if err
.output .stderr
.starts_with("ERROR: autoytarchivers:") .starts_with("ERROR: autoytarchivers:")
{ {
drop(guard); drop(guard);
let time: u64 = err let time: u64 = err
.output .stderr
.splitn(5, &[':', ' '][..]) .splitn(5, &[':', ' '][..])
.nth(3) .nth(3)
.unwrap() .unwrap()

View File

@ -45,7 +45,8 @@ pub struct VideoData {
#[derive(Debug)] #[derive(Debug)]
pub struct YoutubeDLError { pub struct YoutubeDLError {
pub status: ExitStatus, pub status: ExitStatus,
pub output: String, pub stdout: String,
pub stderr: String,
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -6,7 +6,7 @@ use rand::{thread_rng, Rng};
use reqwest::Client; use reqwest::Client;
use std::io::Cursor; use std::io::Cursor;
use std::process::Stdio; use std::process::Stdio;
use tokio::io::{copy_buf, AsyncWriteExt, BufReader}; use tokio::io::AsyncWriteExt;
use tokio::process::Command; use tokio::process::Command;
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
extern crate grammers_client; extern crate grammers_client;
@ -39,7 +39,7 @@ def try_get(src, getter, expected_type=None):
youtube_dl.extractor.youtube.try_get = try_get youtube_dl.extractor.youtube.try_get = try_get
ytdl = youtube_dl.YoutubeDL({"skip_download": True, "no_color": True, "quiet": True}) ytdl = youtube_dl.YoutubeDL({"skip_download": True, "no_color": True, "quiet": True})
try: try:
print(json.dumps(ytdl.extract_info("https://www.youtube.com/watch?v=" + sys.argv[1]), indent=4), file=sys.stderr) print(json.dumps(ytdl.extract_info("https://www.youtube.com/watch?v=" + sys.argv[1]), indent=4))
except Exception as e: except Exception as e:
sys.exit(str(e))"#; sys.exit(str(e))"#;
@ -120,6 +120,7 @@ pub async fn get_video(video_id: &str) -> Result<Option<VideoData>> {
let mut process = command let mut process = command
.args(&["-", video_id]) .args(&["-", video_id])
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.spawn()?; .spawn()?;
let mut stdin = process.stdin.take().unwrap(); let mut stdin = process.stdin.take().unwrap();
@ -129,21 +130,13 @@ pub async fn get_video(video_id: &str) -> Result<Option<VideoData>> {
} }
drop(stdin) drop(stdin)
}); });
let stderr = process.stderr.take().unwrap(); let output = process.wait_with_output().await?;
let task: tokio::task::JoinHandle<std::result::Result<_, std::io::Error>> = if output.status.success() {
tokio::spawn(async move { let mut data: VideoData = serde_json::from_slice(&output.stdout)?;
let mut stderr = BufReader::new(stderr); data.json = String::from_utf8(output.stdout)?;
let mut writer = vec![];
copy_buf(&mut stderr, &mut writer).await?;
Ok(writer)
});
let status = process.wait().await?;
let stderr = String::from_utf8(task.await??)?;
if status.success() {
let mut data: VideoData = serde_json::from_str(&stderr)?;
data.json = stderr;
Ok(Some(data)) Ok(Some(data))
} else { } else {
let stderr = String::from_utf8(output.stderr)?;
let stderr_lowercase = stderr.to_lowercase(); let stderr_lowercase = stderr.to_lowercase();
if stderr_lowercase.contains("private video") if stderr_lowercase.contains("private video")
|| stderr_lowercase.contains("unavailable") || stderr_lowercase.contains("unavailable")
@ -152,8 +145,9 @@ pub async fn get_video(video_id: &str) -> Result<Option<VideoData>> {
Ok(None) Ok(None)
} else { } else {
Err(Error::YoutubeDL(YoutubeDLError { Err(Error::YoutubeDL(YoutubeDLError {
status, status: output.status,
output: stderr, stdout: String::from_utf8(output.stdout)?,
stderr,
})) }))
} }
} }
@ -170,7 +164,7 @@ pub async fn get_video_retry(
Err(err) => { Err(err) => {
eprintln!("Failed to get video data: {:?}", err); eprintln!("Failed to get video data: {:?}", err);
if let Error::YoutubeDL(ref err) = err { if let Error::YoutubeDL(ref err) = err {
let output = err.output.to_lowercase(); let output = err.stderr.to_lowercase();
if output.contains("429") if output.contains("429")
|| output.contains("too many requests") || output.contains("too many requests")
|| output.contains("technical difficult") || output.contains("technical difficult")