i5-add-tests (part 3) #9

Merged
kemitix merged 28 commits from i5-add-tests into main 2023-07-29 20:36:04 +01:00
5 changed files with 17 additions and 4 deletions
Showing only changes of commit 50da088518 - Show all commits

View file

@ -1,13 +1,15 @@
use crate::prelude::*;
pub fn find(site: &str, channel_name: &str) -> Result<String> {
use crate::fetch::FetchEnv;
pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result<String> {
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 response = (e.get)(&channel_url)?;
let rss_url = scraper::Html::parse_document(&response.text()?)
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
.next()

View file

@ -1,5 +1,7 @@
use crate::prelude::*;
use crate::FetchEnv;
mod find;
mod get;
@ -13,5 +15,5 @@ pub struct FeedEnv {
pub get: FeedGet,
}
pub type FeedFind = fn(&str, &str) -> Result<String>;
pub type FeedFind = fn(&str, &str, &FetchEnv) -> Result<String>;
pub type FeedGet = fn(&str) -> Result<Feed>;

View file

@ -5,9 +5,11 @@ use std::process::Command;
pub struct FetchEnv {
pub download: FetchDownload,
pub get: FetchGet,
}
pub type FetchDownload = fn(&Link) -> Result<()>;
pub type FetchGet = fn(&str) -> Result<Response>;
pub fn download(link: &Link) -> Result<()> {
let cmd = "yt-dlp";
@ -24,3 +26,9 @@ pub fn download(link: &Link) -> Result<()> {
}
Ok(())
}
pub type Response = reqwest::blocking::Response;
pub fn get(url: &str) -> Result<Response> {
Ok(reqwest::blocking::get(url)?)
}

View file

@ -19,7 +19,7 @@ pub struct Env {
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
for channel_name in subscriptions::lines_from(subscriptions)? {
println!("Channel: {}", channel_name);
let feed_url = (e.feed.find)(site, &channel_name)?;
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch)?;
for entry in (e.feed.get)(&feed_url)?.entries() {
if let Some(link) = entry.links().get(0).cloned() {
if !(e.history.find)(&link, history)? {

View file

@ -24,6 +24,7 @@ fn main() -> Result<()> {
},
fetch: FetchEnv {
download: podal::fetch::download,
get: podal::fetch::get,
},
},
)?;