From 5dace36e032318a919e2cf1e0e9c939757e4e9c3 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Mon, 19 Feb 2024 14:37:19 +0200 Subject: [PATCH] fix yt-dlp's "none" string value to real None variant --- .gitignore | 3 ++- src/dl/yt_dlp.rs | 25 +++++++++++++++++++++++-- src/main.rs | 5 ++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9b2aa00..fb3c5b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target .env -*.json \ No newline at end of file +*.json +*.txt \ No newline at end of file diff --git a/src/dl/yt_dlp.rs b/src/dl/yt_dlp.rs index 4b2909e..87a859a 100644 --- a/src/dl/yt_dlp.rs +++ b/src/dl/yt_dlp.rs @@ -12,12 +12,24 @@ pub struct YtDlpFormat { pub width: Option, pub height: Option, pub ext: String, - pub vcodec: Option, pub acodec: Option, + pub vcodec: Option, pub abr: Option, pub vbr: Option, } +impl YtDlpFormat { + pub fn process(&mut self) { + if self.acodec.as_ref().is_some_and(|v| v == "none") { + self.acodec = None + } + + if self.vcodec.as_ref().is_some_and(|v| v == "none") { + self.vcodec = None + } + } +} + #[derive(Deserialize, Debug)] pub struct YtDlpInfo { pub id: String, @@ -25,6 +37,14 @@ pub struct YtDlpInfo { pub formats: Vec, } +impl YtDlpInfo { + pub fn process(&mut self) { + for format in &mut self.formats { + format.process() + } + } +} + #[derive(Debug)] pub enum YtDlpError { CommandError(std::io::Error), @@ -77,7 +97,8 @@ impl YtDlp { return Err(YtDlpError::ErrorMessage(message.to_string())); } - let info: YtDlpInfo = serde_json::from_slice(&output.stdout)?; + let mut info: YtDlpInfo = serde_json::from_slice(&output.stdout)?; + info.process(); Ok(info) } diff --git a/src/main.rs b/src/main.rs index 3d1e4d3..6f1db97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,10 @@ where async fn main() -> anyhow::Result<()> { dotenv().ok(); - let _ = YtDlp::load_info(env::var("TEST_URL")?.as_str()).await; + let info = YtDlp::load_info(env::var("TEST_URL")?.as_str()) + .await + .expect("load_info"); + dbg!(info); Ok(()) //bot_main().await