From 17a92e45e49775e6a40d15ebbd73068f4514e597 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 25 Jul 2023 14:47:33 +0100 Subject: [PATCH] provide feed find/get functions as parameters from main --- src/feed/find.rs | 2 +- src/feed/get.rs | 2 +- src/feed/mod.rs | 11 +++++++++-- src/lib.rs | 14 ++++++++++---- src/main.rs | 8 +++++++- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/feed/find.rs b/src/feed/find.rs index da4ccd6..b735e05 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub fn get_feed_url(site: &str, channel_name: &str) -> Result { +pub fn find(site: &str, channel_name: &str) -> Result { if let Some(channel_prefix) = channel_name.chars().next() { if channel_prefix != '@' { return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); diff --git a/src/feed/get.rs b/src/feed/get.rs index 5288c6b..dad28b0 100644 --- a/src/feed/get.rs +++ b/src/feed/get.rs @@ -2,7 +2,7 @@ use atom_syndication::Feed; use crate::prelude::*; -pub fn get_feed(url: String) -> Result { +pub fn get(url: &str) -> Result { let content = reqwest::blocking::get(url)?.bytes()?; let channel = Feed::read_from(&content[..])?; Ok(channel) diff --git a/src/feed/mod.rs b/src/feed/mod.rs index 1b9943a..8b43067 100644 --- a/src/feed/mod.rs +++ b/src/feed/mod.rs @@ -1,5 +1,12 @@ +use crate::prelude::*; + mod find; mod get; -pub use find::get_feed_url; -pub use get::get_feed; +pub use find::find; +pub use get::get; + +type Feed = atom_syndication::Feed; + +pub type FeedFind = fn(&str, &str) -> Result; +pub type FeedGet = fn(&str) -> Result; diff --git a/src/lib.rs b/src/lib.rs index 15b61f6..a3447e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ mod errors; -mod feed; +pub mod feed; mod fetch; mod history; pub mod prelude; @@ -7,12 +7,18 @@ mod subscriptions; use prelude::*; -pub fn run(subscriptions: &str, history: &str, site: &str) -> Result<()> { +pub fn run( + subscriptions: &str, + history: &str, + site: &str, + feed_find: feed::FeedFind, + feed_get: feed::FeedGet, +) -> Result<()> { for channel_name in subscriptions::lines_from(subscriptions)? { let channel_name = channel_name?; println!("Channel: {}", channel_name); - let feed_url = feed::get_feed_url(site, &channel_name)?; - for entry in feed::get_feed(feed_url)?.entries() { + 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)? { println!("Downloading {}: {}", &channel_name, entry.title().as_str()); diff --git a/src/main.rs b/src/main.rs index 10e10e3..4ebdc95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,13 @@ fn main() -> Result<()> { let history = "downloaded.txt"; let site = "https://www.youtube.com/"; - podal::run(subscriptions, history, site)?; + podal::run( + subscriptions, + history, + site, + podal::feed::find, + podal::feed::get, + )?; println!("Done"); Ok(())