i5-add-tests (part 3) #9
7 changed files with 36 additions and 22 deletions
|
@ -1,15 +1,14 @@
|
||||||
|
use crate::network::NetworkEnv;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use crate::fetch::FetchGet;
|
pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
||||||
|
|
||||||
pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let channel_url = format!("{}{}", site, channel_name);
|
let channel_url = format!("{}{}", site, channel_name);
|
||||||
let response = (e)(&channel_url)?;
|
let response = (e.fetch_as_text)(&channel_url)?;
|
||||||
let rss_url = scraper::Html::parse_document(&response)
|
let rss_url = scraper::Html::parse_document(&response)
|
||||||
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
||||||
.next()
|
.next()
|
||||||
|
@ -23,15 +22,16 @@ pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result<String> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::fetch::Response;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn finds_rss_url() -> Result<()> {
|
fn finds_rss_url() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let fetch_get = &(get as FetchGet);
|
let network_env = NetworkEnv {
|
||||||
|
fetch_as_text: dummy_fetch_as_text,
|
||||||
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "@channel", fetch_get)?;
|
let result = find("site", "@channel", &network_env)?;
|
||||||
//then
|
//then
|
||||||
assert_eq!(result, "the-rss-url");
|
assert_eq!(result, "the-rss-url");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -40,15 +40,17 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn error_if_channel_name_is_invalid() -> Result<()> {
|
fn error_if_channel_name_is_invalid() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let fetch_get = &(get as FetchGet);
|
let network_env = NetworkEnv {
|
||||||
|
fetch_as_text: dummy_fetch_as_text,
|
||||||
|
};
|
||||||
//when
|
//when
|
||||||
let result = find("site", "invalid-channel-name", fetch_get);
|
let result = find("site", "invalid-channel-name", &network_env);
|
||||||
//then
|
//then
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(_url: &str) -> Result<Response> {
|
fn dummy_fetch_as_text(_url: &str) -> Result<String> {
|
||||||
Ok(r#"
|
Ok(r#"
|
||||||
<html>
|
<html>
|
||||||
<link title="RSS" href="the-rss-url">
|
<link title="RSS" href="the-rss-url">
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use crate::fetch::FetchGet;
|
use crate::network::NetworkEnv;
|
||||||
|
|
||||||
mod find;
|
mod find;
|
||||||
mod get;
|
mod get;
|
||||||
|
@ -14,5 +14,5 @@ pub struct FeedEnv {
|
||||||
pub get: FeedGet,
|
pub get: FeedGet,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type FeedFind = fn(&str, &str, &FetchGet) -> Result<String>;
|
pub type FeedFind = fn(&str, &str, &NetworkEnv) -> Result<String>;
|
||||||
pub type FeedGet = fn(&str) -> Result<Feed>;
|
pub type FeedGet = fn(&str) -> Result<Feed>;
|
||||||
|
|
|
@ -5,11 +5,10 @@ use std::process::Command;
|
||||||
|
|
||||||
pub struct FetchEnv {
|
pub struct FetchEnv {
|
||||||
pub download: FetchDownload,
|
pub download: FetchDownload,
|
||||||
pub get: FetchGet,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type FetchDownload = fn(&Link) -> Result<()>;
|
pub type FetchDownload = fn(&Link) -> Result<()>;
|
||||||
pub type FetchGet = fn(&str) -> Result<Response>;
|
pub type FetchGet = fn(&str) -> Result<String>;
|
||||||
|
|
||||||
pub fn download(link: &Link) -> Result<()> {
|
pub fn download(link: &Link) -> Result<()> {
|
||||||
let cmd = "yt-dlp";
|
let cmd = "yt-dlp";
|
||||||
|
@ -26,9 +25,3 @@ pub fn download(link: &Link) -> Result<()> {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Response = String;
|
|
||||||
|
|
||||||
pub fn get(url: &str) -> Result<Response> {
|
|
||||||
Ok(reqwest::blocking::get(url)?.text()?)
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ pub mod feed;
|
||||||
pub mod fetch;
|
pub mod fetch;
|
||||||
pub mod file;
|
pub mod file;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
|
pub mod network;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -12,10 +13,12 @@ use feed::FeedEnv;
|
||||||
use fetch::FetchEnv;
|
use fetch::FetchEnv;
|
||||||
use file::FileEnv;
|
use file::FileEnv;
|
||||||
use history::HistoryEnv;
|
use history::HistoryEnv;
|
||||||
|
use network::NetworkEnv;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
pub struct Env {
|
pub struct Env {
|
||||||
pub feed: FeedEnv,
|
pub feed: FeedEnv,
|
||||||
|
pub network: NetworkEnv,
|
||||||
pub history: HistoryEnv,
|
pub history: HistoryEnv,
|
||||||
pub fetch: FetchEnv,
|
pub fetch: FetchEnv,
|
||||||
pub file: FileEnv,
|
pub file: FileEnv,
|
||||||
|
@ -24,7 +27,7 @@ pub struct Env {
|
||||||
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
||||||
for channel_name in file::read::lines_from(subscriptions, &e.file)? {
|
for channel_name in file::read::lines_from(subscriptions, &e.file)? {
|
||||||
println!("Channel: {}", channel_name);
|
println!("Channel: {}", channel_name);
|
||||||
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
|
let feed_url = (e.feed.find)(site, &channel_name, &e.network)?;
|
||||||
for entry in (e.feed.get)(&feed_url)?.entries() {
|
for entry in (e.feed.get)(&feed_url)?.entries() {
|
||||||
if let Some(link) = entry.links().get(0).cloned() {
|
if let Some(link) = entry.links().get(0).cloned() {
|
||||||
if !(e.history.find)(&link, history, &e.file)? {
|
if !(e.history.find)(&link, history, &e.file)? {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use podal::file::FileEnv;
|
use podal::file::FileEnv;
|
||||||
|
use podal::network::NetworkEnv;
|
||||||
use podal::prelude::*;
|
use podal::prelude::*;
|
||||||
|
|
||||||
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
|
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
|
||||||
|
@ -25,8 +26,8 @@ fn main() -> Result<()> {
|
||||||
},
|
},
|
||||||
fetch: FetchEnv {
|
fetch: FetchEnv {
|
||||||
download: podal::fetch::download,
|
download: podal::fetch::download,
|
||||||
get: podal::fetch::get,
|
|
||||||
},
|
},
|
||||||
|
network: NetworkEnv::default(),
|
||||||
file: FileEnv::default(),
|
file: FileEnv::default(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
12
src/network/env.rs
Normal file
12
src/network/env.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub struct NetworkEnv {
|
||||||
|
pub fetch_as_text: fn(url: &str) -> Result<String>,
|
||||||
|
}
|
||||||
|
impl Default for NetworkEnv {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
src/network/mod.rs
Normal file
3
src/network/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
mod env;
|
||||||
|
|
||||||
|
pub use env::NetworkEnv;
|
Loading…
Add table
Reference in a new issue