diff --git a/src/history/find.rs b/src/history/find.rs new file mode 100644 index 0000000..254c41f --- /dev/null +++ b/src/history/find.rs @@ -0,0 +1,17 @@ +use crate::prelude::*; + +use atom_syndication::Link; +use std::fs::File; +use std::io::{BufRead, BufReader}; + +pub fn is_already_downloaded(link: &Link, file_name: &str) -> Result { + if let Ok(file) = File::open(file_name) { + let reader = BufReader::new(file); + for line in reader.lines() { + if line? == link.href { + return Ok(true); // is already downloaded + } + } + } + Ok(false) // is not already downloaded +} diff --git a/src/history/mod.rs b/src/history/mod.rs new file mode 100644 index 0000000..8bdd517 --- /dev/null +++ b/src/history/mod.rs @@ -0,0 +1,3 @@ +mod find; + +pub use find::is_already_downloaded; diff --git a/src/lib.rs b/src/lib.rs index f918b52..3f486ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use atom_syndication::Link; mod errors; mod feed; +mod history; pub mod prelude; use prelude::*; @@ -17,7 +18,7 @@ pub fn run(subscriptions: &str, history: &str, site: &str) -> Result<()> { let feed_url = feed::get_feed_url(site, &channel_name)?; for entry in feed::get_feed(feed_url)?.entries() { if let Some(link) = entry.links().get(0).cloned() { - if !is_already_downloaded(&link, history)? { + if !history::is_already_downloaded(&link, history)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); download_audio(&link)?; mark_as_downloaded(&link, history)?; @@ -37,20 +38,6 @@ fn lines_from(file_name: &str) -> Result Result { - use std::io::{BufRead, BufReader}; - - if let Ok(file) = File::open(file_name) { - let reader = BufReader::new(file); - for line in reader.lines() { - if line? == link.href { - return Ok(true); // is already downloaded - } - } - } - Ok(false) // is not already downloaded -} - fn download_audio(link: &Link) -> Result<()> { use std::process::Command;