diff --git a/src/feed/find.rs b/src/feed/find.rs index 9703c94..27cef15 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -32,6 +32,7 @@ mod tests { let network_env = NetworkEnv { fetch_as_text: dummy_fetch_as_text, fetch_as_bytes: dummy_fetch_as_bytes, + download_as_mp3: dummy_download_as_mp3, }; //when let result = find("site", "@channel", &network_env)?; @@ -46,6 +47,7 @@ mod tests { let network_env = NetworkEnv { fetch_as_text: dummy_fetch_as_text, fetch_as_bytes: dummy_fetch_as_bytes, + download_as_mp3: dummy_download_as_mp3, }; //when let result = find("site", "invalid-channel-name", &network_env); @@ -65,4 +67,7 @@ mod tests { fn dummy_fetch_as_bytes(_url: &str) -> Result { Err(Error::message("Not implemented")) } + fn dummy_download_as_mp3(_url: &str) -> Result<()> { + Err(Error::message("Not implemented")) + } } diff --git a/src/fetch.rs b/src/fetch.rs deleted file mode 100644 index e34dea4..0000000 --- a/src/fetch.rs +++ /dev/null @@ -1,26 +0,0 @@ -use crate::{history::Link, prelude::*}; - -use std::process::Command; - -pub struct FetchEnv { - pub download: FetchDownload, -} - -pub type FetchDownload = fn(&Link) -> Result<()>; -pub type FetchGet = fn(&str) -> Result; - -pub fn download(link: &Link) -> Result<()> { - let cmd = "yt-dlp"; - // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); - let output = Command::new(cmd) - .arg("--extract-audio") - .arg("--audio-format") - .arg("mp3") - .arg(&link.href) - .output()?; - if !output.stderr.is_empty() { - eprintln!("Error: {}", String::from_utf8(output.stderr)?); - println!("{}", String::from_utf8(output.stdout)?); - } - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index 8f4d03a..b7f1a9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ +use prelude::*; + mod errors; pub mod feed; -pub mod fetch; pub mod file; pub mod history; pub mod network; @@ -9,14 +10,11 @@ pub mod prelude; #[cfg(test)] mod test_utils; -use fetch::FetchEnv; use file::FileEnv; use network::NetworkEnv; -use prelude::*; pub struct Env { pub network: NetworkEnv, - pub fetch: FetchEnv, pub file: FileEnv, } @@ -28,7 +26,7 @@ pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> if let Some(link) = entry.links().get(0).cloned() { if !history::find(&link, history, &e.file)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); - (e.fetch.download)(&link)?; + (e.network.download_as_mp3)(&link.href)?; history::add(&link, history, &e.file)?; } } diff --git a/src/main.rs b/src/main.rs index 83d2966..c1c31ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,6 @@ use podal::file::FileEnv; use podal::network::NetworkEnv; use podal::prelude::*; -use podal::fetch::FetchEnv; - fn main() -> Result<()> { println!("Podal"); let subscriptions = "subscriptions.txt"; @@ -15,9 +13,6 @@ fn main() -> Result<()> { history, site, podal::Env { - fetch: FetchEnv { - download: podal::fetch::download, - }, network: NetworkEnv::default(), file: FileEnv::default(), }, diff --git a/src/network/env.rs b/src/network/env.rs index 8ea937f..473fac3 100644 --- a/src/network/env.rs +++ b/src/network/env.rs @@ -1,14 +1,32 @@ +use std::process::Command; + use crate::prelude::*; pub struct NetworkEnv { pub fetch_as_text: fn(url: &str) -> Result, pub fetch_as_bytes: fn(url: &str) -> Result, + pub download_as_mp3: fn(url: &str) -> Result<()>, } impl Default for NetworkEnv { fn default() -> Self { Self { fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?), fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?), + download_as_mp3: |url| { + let cmd = "yt-dlp"; + // println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href); + let output = Command::new(cmd) + .arg("--extract-audio") + .arg("--audio-format") + .arg("mp3") + .arg(&url) + .output()?; + if !output.stderr.is_empty() { + eprintln!("Error: {}", String::from_utf8(output.stderr)?); + println!("{}", String::from_utf8(output.stdout)?); + } + Ok(()) + }, } } }