Specify maximum age of feed items to be displayed in feed file
This commit is contained in:
23
src/main.rs
23
src/main.rs
@@ -56,11 +56,13 @@ fn fetch_feed(url: impl IntoUrl) -> Result<Feed> {
|
||||
|
||||
struct FeedFile {
|
||||
name: String,
|
||||
skip_items_older_than: Option<chrono::Duration>,
|
||||
urls: Vec<Url>,
|
||||
}
|
||||
|
||||
fn read_feed_file(path: &str) -> Result<FeedFile> {
|
||||
let mut name = String::from(path);
|
||||
let mut skip_items_older_than_seconds: Option<i64> = None;
|
||||
let mut urls = Vec::new();
|
||||
|
||||
for line in std::fs::read_to_string(path)?.lines() {
|
||||
@@ -72,6 +74,12 @@ fn read_feed_file(path: &str) -> Result<FeedFile> {
|
||||
name = String::from(&line[8..]);
|
||||
continue;
|
||||
}
|
||||
if line.starts_with("# skip-items-older-than: ") {
|
||||
let skip_items_older_than_string = String::from(&line[25..]);
|
||||
skip_items_older_than_seconds = Some(skip_items_older_than_string.parse::<i64>()
|
||||
.context("Failed to read duration from skip-items-older-than")?);
|
||||
continue;
|
||||
}
|
||||
if line.starts_with("#") {
|
||||
continue;
|
||||
}
|
||||
@@ -82,6 +90,7 @@ fn read_feed_file(path: &str) -> Result<FeedFile> {
|
||||
|
||||
Ok(FeedFile{
|
||||
name: name,
|
||||
skip_items_older_than: skip_items_older_than_seconds.map(|s| chrono::Duration::seconds(s)),
|
||||
urls: urls,
|
||||
})
|
||||
}
|
||||
@@ -108,8 +117,16 @@ fn make_feed_item(entry: Entry) -> Result<FeedItem> {
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
println!("Loading feeds from: {}", &cli.feeds);
|
||||
|
||||
let feed_file = read_feed_file(&cli.feeds)?;
|
||||
|
||||
let skip_items_from_before = feed_file.skip_items_older_than.map(|d| chrono::Utc::now() - d);
|
||||
if let Some(before_time) = skip_items_from_before {
|
||||
println!("Skip items from before: {}", before_time);
|
||||
};
|
||||
|
||||
|
||||
let mut items: Vec<FeedItem> = Vec::new();
|
||||
|
||||
for feed_url in feed_file.urls {
|
||||
@@ -126,6 +143,12 @@ fn main() -> Result<()> {
|
||||
for entry in feed_entries {
|
||||
match make_feed_item(entry) {
|
||||
Ok(item) => {
|
||||
if let Some(before_time) = skip_items_from_before {
|
||||
if before_time > item.time {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
items.push(item);
|
||||
},
|
||||
Err(e) => {
|
||||
|
Reference in New Issue
Block a user