From 507447f6ad2e50aea6c5ade75ce8bffbd13a33be Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 25 Jul 2023 10:46:47 +0100 Subject: [PATCH] extract prelude and feed::find --- src/feed/find.rs | 20 ++++++++++++++++++++ src/feed/mod.rs | 3 +++ src/lib.rs | 32 ++++---------------------------- src/main.rs | 4 +++- src/prelude.rs | 3 +++ 5 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 src/feed/find.rs create mode 100644 src/feed/mod.rs create mode 100644 src/prelude.rs diff --git a/src/feed/find.rs b/src/feed/find.rs new file mode 100644 index 0000000..da4ccd6 --- /dev/null +++ b/src/feed/find.rs @@ -0,0 +1,20 @@ +use crate::prelude::*; + +pub fn get_feed_url(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()); + } + } + let channel_url = format!("{}{}", site, channel_name); + let response = reqwest::blocking::get(channel_url)?; + let rss_url = scraper::Html::parse_document(&response.text()?) + .select(&scraper::Selector::parse("link[title='RSS']").unwrap()) + .next() + .unwrap() + .value() + .attr("href") + .unwrap() + .to_string(); + Ok(rss_url) +} diff --git a/src/feed/mod.rs b/src/feed/mod.rs new file mode 100644 index 0000000..385ab7d --- /dev/null +++ b/src/feed/mod.rs @@ -0,0 +1,3 @@ +mod find; + +pub use find::get_feed_url; diff --git a/src/lib.rs b/src/lib.rs index ad4e909..3702f3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,21 +5,16 @@ use std::fs::File; use atom_syndication::{Entry, Feed, Link}; mod errors; -use errors::Error; +mod feed; +pub mod prelude; -// -// RESULTS -// -pub type Result = std::result::Result; +use prelude::*; -// -// MAIN -// pub fn run(subscriptions: &str, history: &str, site: &str) -> Result<()> { for channel_name in lines_from(subscriptions)? { let channel_name = channel_name?; println!("Channel: {}", channel_name); - let feed_url = get_feed_url(site, &channel_name)?; + let feed_url = feed::get_feed_url(site, &channel_name)?; for entry in get_feed(feed_url)?.entries() { if let Some(link) = get_link(entry) { if !is_already_downloaded(&link, history)? { @@ -33,25 +28,6 @@ pub fn run(subscriptions: &str, history: &str, site: &str) -> Result<()> { Ok(()) } -fn get_feed_url(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()); - } - } - let channel_url = format!("{}{}", site, channel_name); - let response = reqwest::blocking::get(channel_url)?; - let rss_url = scraper::Html::parse_document(&response.text()?) - .select(&scraper::Selector::parse("link[title='RSS']").unwrap()) - .next() - .unwrap() - .value() - .attr("href") - .unwrap() - .to_string(); - Ok(rss_url) -} - fn get_link(item: &Entry) -> Option { item.links().get(0).cloned() } diff --git a/src/main.rs b/src/main.rs index 83b79ec..10e10e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -fn main() -> podal::Result<()> { +use podal::prelude::*; + +fn main() -> Result<()> { println!("Podal"); let subscriptions = "subscriptions.txt"; let history = "downloaded.txt"; diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..476ec26 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,3 @@ +use crate::errors::Error; + +pub type Result = std::result::Result;