Remove no longer watched books from the feed
This commit is contained in:
parent
1b37d6a134
commit
35691397a3
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -98,7 +98,7 @@ checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9"
|
||||
|
||||
[[package]]
|
||||
name = "bib-watcher"
|
||||
version = "0.2.0"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"config",
|
||||
|
@ -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
|
||||
|
19
src/main.rs
19
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<dyn Error>> {
|
||||
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<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(())
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user