extract prelude and feed::find

This commit is contained in:
Paul Campbell 2023-07-25 10:46:47 +01:00
parent ec20864dac
commit 507447f6ad
5 changed files with 33 additions and 29 deletions

20
src/feed/find.rs Normal file
View file

@ -0,0 +1,20 @@
use crate::prelude::*;
pub fn get_feed_url(site: &str, channel_name: &str) -> 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 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)
}

3
src/feed/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod find;
pub use find::get_feed_url;

View file

@ -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<T> = std::result::Result<T, Error>;
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<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 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<Link> {
item.links().get(0).cloned()
}

View file

@ -1,4 +1,6 @@
fn main() -> podal::Result<()> {
use podal::prelude::*;
fn main() -> Result<()> {
println!("Podal");
let subscriptions = "subscriptions.txt";
let history = "downloaded.txt";

3
src/prelude.rs Normal file
View file

@ -0,0 +1,3 @@
use crate::errors::Error;
pub type Result<T> = std::result::Result<T, Error>;