feat: Make feeds path customizable
This commit is contained in:
parent
8bc7556cf8
commit
b178103560
2 changed files with 7 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
bind = "127.0.0.1:8085"
|
bind = "127.0.0.1:8085"
|
||||||
item_tpl = "./assets/item.html"
|
item_tpl = "./assets/item.html"
|
||||||
page_tpl = "./assets/index.html"
|
page_tpl = "./assets/index.html"
|
||||||
|
feeds_path = "./feeds/"
|
||||||
|
|
||||||
[feeds.tc]
|
[feeds.tc]
|
||||||
url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCy0tKL1T7wFoYcxCe0xjN6Q"
|
url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCy0tKL1T7wFoYcxCe0xjN6Q"
|
||||||
|
|
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -3,12 +3,13 @@ use reqwest;
|
||||||
#[derive(Debug, Clone, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Deserialize)]
|
||||||
struct FeedConfig {
|
struct FeedConfig {
|
||||||
interval_secs: Option<u64>,
|
interval_secs: Option<u64>,
|
||||||
url: String
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Deserialize)]
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
bind: String,
|
bind: String,
|
||||||
|
feeds_path: String,
|
||||||
item_tpl: String,
|
item_tpl: String,
|
||||||
page_tpl: String,
|
page_tpl: String,
|
||||||
feeds: std::collections::HashMap<String, FeedConfig>
|
feeds: std::collections::HashMap<String, FeedConfig>
|
||||||
|
|
@ -23,7 +24,7 @@ struct AppState<'a> {
|
||||||
fn read_config() -> anyhow::Result<AppConfig> {
|
fn read_config() -> anyhow::Result<AppConfig> {
|
||||||
let config_path = std::env::var("RSS_CONFIG").unwrap_or("./config.toml".into());
|
let config_path = std::env::var("RSS_CONFIG").unwrap_or("./config.toml".into());
|
||||||
let cfg = std::fs::read_to_string(&config_path)?;
|
let cfg = std::fs::read_to_string(&config_path)?;
|
||||||
let mut cfg: AppConfig = toml::from_str(&cfg)?;
|
let cfg: AppConfig = toml::from_str(&cfg)?;
|
||||||
Ok(cfg)
|
Ok(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,9 +52,9 @@ async fn file_needs_update(
|
||||||
Ok(time.elapsed()?.as_secs() >= expiration_timeout.as_secs())
|
Ok(time.elapsed()?.as_secs() >= expiration_timeout.as_secs())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_rss(name: &str, cfg: &FeedConfig) -> anyhow::Result<feed_rs::model::Feed> {
|
async fn get_rss(name: &str, cfg: &FeedConfig, feed_folder: &str) -> anyhow::Result<feed_rs::model::Feed> {
|
||||||
|
|
||||||
let filename = std::path::PathBuf::from(format!("./feeds/{}.last_feed", name));
|
let filename = std::path::PathBuf::from(format!("./{}/{}.last_feed", &feed_folder, name));
|
||||||
let data;
|
let data;
|
||||||
if file_needs_update(&filename, std::time::Duration::from_secs(cfg.interval_secs.unwrap_or(0))).await.unwrap_or(true) {
|
if file_needs_update(&filename, std::time::Duration::from_secs(cfg.interval_secs.unwrap_or(0))).await.unwrap_or(true) {
|
||||||
data = reqwest::get(cfg.url.clone()).await?.text().await?;
|
data = reqwest::get(cfg.url.clone()).await?.text().await?;
|
||||||
|
|
@ -71,7 +72,7 @@ async fn render_rss_feeds(axum::extract::State(state): axum::extract::State<std:
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for (name, cfg) in state.config.feeds.iter() {
|
for (name, cfg) in state.config.feeds.iter() {
|
||||||
|
|
||||||
let feed = match get_rss(&name, cfg).await {
|
let feed = match get_rss(&name, cfg, &state.config.feeds_path).await {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
res.push(format!("Error fetching feed: {}", &name));
|
res.push(format!("Error fetching feed: {}", &name));
|
||||||
log::error!("Error fetching feed: {}", e);
|
log::error!("Error fetching feed: {}", e);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue