generating html from 12 feeds works
This commit is contained in:
parent
b422ece932
commit
3bee93ecb3
7 changed files with 165 additions and 41 deletions
|
|
@ -3,14 +3,17 @@ extern crate log;
|
|||
|
||||
use crate::feed_store::FeedStore;
|
||||
use crate::fetcher::Fetcher;
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
use serde::Deserialize;
|
||||
use simple_entry::SimpleEntry;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use url::Url;
|
||||
|
||||
mod feed_store;
|
||||
mod fetcher;
|
||||
mod simple_entry;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
|
|
@ -21,6 +24,8 @@ struct Args {
|
|||
default_value_t = String::from("mars.toml")
|
||||
)]
|
||||
config: String,
|
||||
#[arg(long, default_value_t = false)]
|
||||
no_fetch: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
@ -39,7 +44,7 @@ struct Config {
|
|||
templates_dir: String,
|
||||
}
|
||||
|
||||
pub fn to_checked_pathbuf(dir: String) -> PathBuf {
|
||||
pub fn to_checked_pathbuf(dir: &str) -> PathBuf {
|
||||
let dir: PathBuf = PathBuf::from(dir);
|
||||
|
||||
let m = dir
|
||||
|
|
@ -54,7 +59,54 @@ struct FeedConfig {
|
|||
url: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn fetch(config: &Config, feed_store: &FeedStore) -> Result<bool> {
|
||||
let fetcher = Fetcher::new(&config.bot_name, &config.from);
|
||||
let mut rebuild = false;
|
||||
for feed in &config.feeds {
|
||||
let url = match Url::parse(&feed.url) {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
error!("Error parsing url '{}': {e:?}", feed.url);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
rebuild |= fetcher.fetch(url, feed_store);
|
||||
}
|
||||
info!("Done fetching. Rebuild needed: {rebuild}");
|
||||
Ok(rebuild)
|
||||
}
|
||||
|
||||
fn build(config: &Config, feed_store: &FeedStore) -> Result<()> {
|
||||
let templates_dir = to_checked_pathbuf(&config.templates_dir);
|
||||
let out_dir = to_checked_pathbuf(&config.out_dir);
|
||||
|
||||
let mut tera = match tera::Tera::new(&format!("{}/*", &templates_dir.display())) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Parsing error(s): {}", e);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
};
|
||||
// disable autoescape as this would corrupt urls or the entriy contents. todo check this!
|
||||
tera.autoescape_on(vec![]);
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
let entries: Vec<SimpleEntry> = feed_store
|
||||
.collect(&config.feeds)
|
||||
.into_iter()
|
||||
.map(SimpleEntry::from_feed_entry)
|
||||
.collect();
|
||||
context.insert("entries", &entries);
|
||||
|
||||
for name in tera.get_template_names() {
|
||||
debug!("Processing template {name}");
|
||||
let file = fs::File::create(format!("{}/{name}", out_dir.display()))?;
|
||||
tera.render_to(name, &context, file)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
env_logger::init();
|
||||
info!("starting up");
|
||||
|
||||
|
|
@ -64,35 +116,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
panic!("Configuration file {config_path} does not exist!");
|
||||
}
|
||||
let config: Config = toml::from_str(&fs::read_to_string(config_path)?)?;
|
||||
let templates_dir = to_checked_pathbuf(config.templates_dir);
|
||||
let out_dir = to_checked_pathbuf(config.out_dir);
|
||||
// only check here to avoid fetching with broken config
|
||||
// todo: get a config lib that provides validation!
|
||||
let _ = to_checked_pathbuf(&config.templates_dir);
|
||||
let _ = to_checked_pathbuf(&config.out_dir);
|
||||
|
||||
let feed_store = FeedStore::new(config.feed_dir);
|
||||
let fetcher = Fetcher::new(&config.bot_name, &config.from);
|
||||
let feed_store = FeedStore::new(&config.feed_dir);
|
||||
let should_build = if args.no_fetch {
|
||||
true
|
||||
} else {
|
||||
fetch(&config, &feed_store)?
|
||||
};
|
||||
|
||||
let mut rebuild = false;
|
||||
for feed in &config.feeds {
|
||||
let url = Url::parse(&feed.url)?;
|
||||
rebuild |= fetcher.fetch(url, &feed_store);
|
||||
}
|
||||
info!("Done fetching. Rebuild needed: {rebuild}");
|
||||
if rebuild {
|
||||
let entries = feed_store.collect(&config.feeds);
|
||||
let mut tera = match tera::Tera::new(&format!("{}/*", &templates_dir.display())) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Parsing error(s): {}", e);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
};
|
||||
tera.autoescape_on(vec![]);
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("entries", &entries);
|
||||
for name in tera.get_template_names() {
|
||||
debug!("Processing template {name}");
|
||||
let file = fs::File::create(&format!("{}/{name}", out_dir.display()))?;
|
||||
let _ = tera.render_to(name, &context, file)?;
|
||||
}
|
||||
if should_build {
|
||||
build(&config, &feed_store)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue