fix yt-dlp's "none" string value to real None variant

This commit is contained in:
mykola2312 2024-02-19 14:37:19 +02:00
parent 685320336b
commit 5dace36e03
3 changed files with 29 additions and 4 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target /target
.env .env
*.json *.json
*.txt

View file

@ -12,12 +12,24 @@ pub struct YtDlpFormat {
pub width: Option<u16>, pub width: Option<u16>,
pub height: Option<u16>, pub height: Option<u16>,
pub ext: String, pub ext: String,
pub vcodec: Option<String>,
pub acodec: Option<String>, pub acodec: Option<String>,
pub vcodec: Option<String>,
pub abr: Option<f32>, pub abr: Option<f32>,
pub vbr: Option<f32>, pub vbr: Option<f32>,
} }
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)] #[derive(Deserialize, Debug)]
pub struct YtDlpInfo { pub struct YtDlpInfo {
pub id: String, pub id: String,
@ -25,6 +37,14 @@ pub struct YtDlpInfo {
pub formats: Vec<YtDlpFormat>, pub formats: Vec<YtDlpFormat>,
} }
impl YtDlpInfo {
pub fn process(&mut self) {
for format in &mut self.formats {
format.process()
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum YtDlpError { pub enum YtDlpError {
CommandError(std::io::Error), CommandError(std::io::Error),
@ -77,7 +97,8 @@ impl YtDlp {
return Err(YtDlpError::ErrorMessage(message.to_string())); 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) Ok(info)
} }

View file

@ -36,7 +36,10 @@ where
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
dotenv().ok(); 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(()) Ok(())
//bot_main().await //bot_main().await