Remove no longer watched books from the feed

This commit is contained in:
max.nuding 2023-12-15 15:30:01 +01:00
parent 1b37d6a134
commit 35691397a3
Failed to extract signature
3 changed files with 20 additions and 3 deletions

2
Cargo.lock generated
View File

@ -98,7 +98,7 @@ checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9"
[[package]] [[package]]
name = "bib-watcher" name = "bib-watcher"
version = "0.2.0" version = "0.4.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"config", "config",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bib-watcher" name = "bib-watcher"
version = "0.3.0" version = "0.4.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -2,7 +2,7 @@ use config::Config;
use reqwest::header; use reqwest::header;
use rss::{Channel, ChannelBuilder, GuidBuilder, ItemBuilder}; use rss::{Channel, ChannelBuilder, GuidBuilder, ItemBuilder};
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use std::{error::Error, fs::File, io::BufReader}; use std::{error::Error, fs::File, io::BufReader};
use chrono::prelude::*; use chrono::prelude::*;
@ -42,6 +42,23 @@ fn main() -> Result<(), Box<dyn Error>> {
for (title, url) in searches { for (title, url) in searches {
search(url, title)? search(url, title)?
} }
let urls = books.values().collect();
remove_old_books(&urls)?;
Ok(())
}
fn remove_old_books(urls: &HashSet<&String>) -> Result<(), Box<dyn Error>> {
let file = File::open(FILE_NAME)?;
let mut channel = Channel::read_from(BufReader::new(file))?;
let items: Vec<_> = channel.items
.iter()
.filter(|i|i.link.as_ref().is_some_and(|l|urls.contains(&l)))
.map(|i|i.clone())
.collect();
channel.set_items(items);
channel.pretty_write_to(File::create(FILE_NAME)?, b' ', 4)?;
Ok(()) Ok(())
} }