fold FetchEnv into NetworkEnv
This commit is contained in:
parent
f5974acd25
commit
7c752b7ba7
5 changed files with 26 additions and 36 deletions
|
@ -32,6 +32,7 @@ mod tests {
|
||||||
let network_env = NetworkEnv {
|
let network_env = NetworkEnv {
|
||||||
fetch_as_text: dummy_fetch_as_text,
|
fetch_as_text: dummy_fetch_as_text,
|
||||||
fetch_as_bytes: dummy_fetch_as_bytes,
|
fetch_as_bytes: dummy_fetch_as_bytes,
|
||||||
|
download_as_mp3: dummy_download_as_mp3,
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "@channel", &network_env)?;
|
let result = find("site", "@channel", &network_env)?;
|
||||||
|
@ -46,6 +47,7 @@ mod tests {
|
||||||
let network_env = NetworkEnv {
|
let network_env = NetworkEnv {
|
||||||
fetch_as_text: dummy_fetch_as_text,
|
fetch_as_text: dummy_fetch_as_text,
|
||||||
fetch_as_bytes: dummy_fetch_as_bytes,
|
fetch_as_bytes: dummy_fetch_as_bytes,
|
||||||
|
download_as_mp3: dummy_download_as_mp3,
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "invalid-channel-name", &network_env);
|
let result = find("site", "invalid-channel-name", &network_env);
|
||||||
|
@ -65,4 +67,7 @@ mod tests {
|
||||||
fn dummy_fetch_as_bytes(_url: &str) -> Result<bytes::Bytes> {
|
fn dummy_fetch_as_bytes(_url: &str) -> Result<bytes::Bytes> {
|
||||||
Err(Error::message("Not implemented"))
|
Err(Error::message("Not implemented"))
|
||||||
}
|
}
|
||||||
|
fn dummy_download_as_mp3(_url: &str) -> Result<()> {
|
||||||
|
Err(Error::message("Not implemented"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
26
src/fetch.rs
26
src/fetch.rs
|
@ -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<String>;
|
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
use prelude::*;
|
||||||
|
|
||||||
mod errors;
|
mod errors;
|
||||||
pub mod feed;
|
pub mod feed;
|
||||||
pub mod fetch;
|
|
||||||
pub mod file;
|
pub mod file;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
@ -9,14 +10,11 @@ pub mod prelude;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_utils;
|
mod test_utils;
|
||||||
|
|
||||||
use fetch::FetchEnv;
|
|
||||||
use file::FileEnv;
|
use file::FileEnv;
|
||||||
use network::NetworkEnv;
|
use network::NetworkEnv;
|
||||||
use prelude::*;
|
|
||||||
|
|
||||||
pub struct Env {
|
pub struct Env {
|
||||||
pub network: NetworkEnv,
|
pub network: NetworkEnv,
|
||||||
pub fetch: FetchEnv,
|
|
||||||
pub file: FileEnv,
|
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 let Some(link) = entry.links().get(0).cloned() {
|
||||||
if !history::find(&link, history, &e.file)? {
|
if !history::find(&link, history, &e.file)? {
|
||||||
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
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)?;
|
history::add(&link, history, &e.file)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ use podal::file::FileEnv;
|
||||||
use podal::network::NetworkEnv;
|
use podal::network::NetworkEnv;
|
||||||
use podal::prelude::*;
|
use podal::prelude::*;
|
||||||
|
|
||||||
use podal::fetch::FetchEnv;
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
println!("Podal");
|
println!("Podal");
|
||||||
let subscriptions = "subscriptions.txt";
|
let subscriptions = "subscriptions.txt";
|
||||||
|
@ -15,9 +13,6 @@ fn main() -> Result<()> {
|
||||||
history,
|
history,
|
||||||
site,
|
site,
|
||||||
podal::Env {
|
podal::Env {
|
||||||
fetch: FetchEnv {
|
|
||||||
download: podal::fetch::download,
|
|
||||||
},
|
|
||||||
network: NetworkEnv::default(),
|
network: NetworkEnv::default(),
|
||||||
file: FileEnv::default(),
|
file: FileEnv::default(),
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,14 +1,32 @@
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub struct NetworkEnv {
|
pub struct NetworkEnv {
|
||||||
pub fetch_as_text: fn(url: &str) -> Result<String>,
|
pub fetch_as_text: fn(url: &str) -> Result<String>,
|
||||||
pub fetch_as_bytes: fn(url: &str) -> Result<bytes::Bytes>,
|
pub fetch_as_bytes: fn(url: &str) -> Result<bytes::Bytes>,
|
||||||
|
pub download_as_mp3: fn(url: &str) -> Result<()>,
|
||||||
}
|
}
|
||||||
impl Default for NetworkEnv {
|
impl Default for NetworkEnv {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?),
|
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?),
|
||||||
fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?),
|
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(())
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue