diff --git a/src/fetch.rs b/src/fetch.rs index 38fd8ff..1f95f96 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -1,10 +1,11 @@ use crate::prelude::*; use atom_syndication::Link; +use std::process::Command; -pub fn download_audio(link: &Link) -> Result<()> { - use std::process::Command; +pub type FetchDownload = fn(&Link) -> 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) diff --git a/src/history/add.rs b/src/history/add.rs index e186644..e904004 100644 --- a/src/history/add.rs +++ b/src/history/add.rs @@ -4,7 +4,7 @@ use atom_syndication::Link; use std::fs::OpenOptions; use std::io::prelude::*; -pub fn mark_as_downloaded(link: &Link, file_name: &str) -> Result<()> { +pub fn add(link: &Link, file_name: &str) -> Result<()> { let mut file = OpenOptions::new() .write(true) .append(true) diff --git a/src/history/find.rs b/src/history/find.rs index 254c41f..5b760b8 100644 --- a/src/history/find.rs +++ b/src/history/find.rs @@ -4,7 +4,7 @@ use atom_syndication::Link; use std::fs::File; use std::io::{BufRead, BufReader}; -pub fn is_already_downloaded(link: &Link, file_name: &str) -> Result { +pub fn find(link: &Link, file_name: &str) -> Result { if let Ok(file) = File::open(file_name) { let reader = BufReader::new(file); for line in reader.lines() { diff --git a/src/history/mod.rs b/src/history/mod.rs index 87d3186..e36428d 100644 --- a/src/history/mod.rs +++ b/src/history/mod.rs @@ -1,5 +1,12 @@ +use crate::prelude::*; + mod add; mod find; -pub use add::mark_as_downloaded; -pub use find::is_already_downloaded; +pub use add::add; +pub use find::find; + +type Link = atom_syndication::Link; + +pub type HistoryFind = fn(&Link, &str) -> Result; +pub type HistoryAdd = fn(&Link, &str) -> Result<()>; diff --git a/src/lib.rs b/src/lib.rs index a3447e0..b7bee6f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,24 @@ mod errors; pub mod feed; -mod fetch; -mod history; +pub mod fetch; +pub mod history; pub mod prelude; mod subscriptions; +use feed::{FeedFind, FeedGet}; +use fetch::FetchDownload; +use history::{HistoryAdd, HistoryFind}; use prelude::*; pub fn run( subscriptions: &str, history: &str, site: &str, - feed_find: feed::FeedFind, - feed_get: feed::FeedGet, + feed_find: FeedFind, + feed_get: FeedGet, + history_find: HistoryFind, + history_add: HistoryAdd, + fetch_download: FetchDownload, ) -> Result<()> { for channel_name in subscriptions::lines_from(subscriptions)? { let channel_name = channel_name?; @@ -20,10 +26,10 @@ pub fn run( let feed_url = feed_find(site, &channel_name)?; for entry in feed_get(&feed_url)?.entries() { if let Some(link) = entry.links().get(0).cloned() { - if !history::is_already_downloaded(&link, history)? { + if !history_find(&link, history)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); - fetch::download_audio(&link)?; - history::mark_as_downloaded(&link, history)?; + fetch_download(&link)?; + history_add(&link, history)?; } } } diff --git a/src/main.rs b/src/main.rs index 4ebdc95..3e4a741 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,9 @@ fn main() -> Result<()> { site, podal::feed::find, podal::feed::get, + podal::history::find, + podal::history::add, + podal::fetch::download, )?; println!("Done");