provide feed find/get functions as parameters from main

This commit is contained in:
Paul Campbell 2023-07-25 14:47:33 +01:00
parent e1ea5a81ec
commit 17a92e45e4
5 changed files with 28 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
pub fn get_feed_url(site: &str, channel_name: &str) -> Result<String> { pub fn find(site: &str, channel_name: &str) -> Result<String> {
if let Some(channel_prefix) = channel_name.chars().next() { if let Some(channel_prefix) = channel_name.chars().next() {
if channel_prefix != '@' { if channel_prefix != '@' {
return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); return Err(format!("Channel Name must begin with an '@': {}", channel_name).into());

View file

@ -2,7 +2,7 @@ use atom_syndication::Feed;
use crate::prelude::*; use crate::prelude::*;
pub fn get_feed(url: String) -> Result<Feed> { pub fn get(url: &str) -> Result<Feed> {
let content = reqwest::blocking::get(url)?.bytes()?; let content = reqwest::blocking::get(url)?.bytes()?;
let channel = Feed::read_from(&content[..])?; let channel = Feed::read_from(&content[..])?;
Ok(channel) Ok(channel)

View file

@ -1,5 +1,12 @@
use crate::prelude::*;
mod find; mod find;
mod get; mod get;
pub use find::get_feed_url; pub use find::find;
pub use get::get_feed; pub use get::get;
type Feed = atom_syndication::Feed;
pub type FeedFind = fn(&str, &str) -> Result<String>;
pub type FeedGet = fn(&str) -> Result<Feed>;

View file

@ -1,5 +1,5 @@
mod errors; mod errors;
mod feed; pub mod feed;
mod fetch; mod fetch;
mod history; mod history;
pub mod prelude; pub mod prelude;
@ -7,12 +7,18 @@ mod subscriptions;
use prelude::*; 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)? { for channel_name in subscriptions::lines_from(subscriptions)? {
let channel_name = channel_name?; let channel_name = channel_name?;
println!("Channel: {}", channel_name); println!("Channel: {}", channel_name);
let feed_url = feed::get_feed_url(site, &channel_name)?; let feed_url = feed_find(site, &channel_name)?;
for entry in feed::get_feed(feed_url)?.entries() { for entry in feed_get(&feed_url)?.entries() {
if let Some(link) = entry.links().get(0).cloned() { if let Some(link) = entry.links().get(0).cloned() {
if !history::is_already_downloaded(&link, history)? { if !history::is_already_downloaded(&link, history)? {
println!("Downloading {}: {}", &channel_name, entry.title().as_str()); println!("Downloading {}: {}", &channel_name, entry.title().as_str());

View file

@ -6,7 +6,13 @@ fn main() -> Result<()> {
let history = "downloaded.txt"; let history = "downloaded.txt";
let site = "https://www.youtube.com/"; let site = "https://www.youtube.com/";
podal::run(subscriptions, history, site)?; podal::run(
subscriptions,
history,
site,
podal::feed::find,
podal::feed::get,
)?;
println!("Done"); println!("Done");
Ok(()) Ok(())