From 35691397a391b543bad12c93c2d6ef587620682b Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Fri, 15 Dec 2023 15:30:01 +0100 Subject: [PATCH] Remove no longer watched books from the feed --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e917419..59033bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,7 +98,7 @@ checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" [[package]] name = "bib-watcher" -version = "0.2.0" +version = "0.4.0" dependencies = [ "chrono", "config", diff --git a/Cargo.toml b/Cargo.toml index 3b9b55c..643a37a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bib-watcher" -version = "0.3.0" +version = "0.4.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index ecb6ad8..89a83a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use config::Config; use reqwest::header; use rss::{Channel, ChannelBuilder, GuidBuilder, ItemBuilder}; use scraper::{Html, Selector}; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::{error::Error, fs::File, io::BufReader}; use chrono::prelude::*; @@ -42,6 +42,23 @@ fn main() -> Result<(), Box> { for (title, url) in searches { search(url, title)? } + let urls = books.values().collect(); + remove_old_books(&urls)?; + + Ok(()) +} + +fn remove_old_books(urls: &HashSet<&String>) -> Result<(), Box> { + 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(()) }