Handle ebooks

This commit is contained in:
2023-11-09 19:50:06 +01:00
parent 97d97f9f22
commit c110ed0aed
4 changed files with 52 additions and 15 deletions

View File

@ -2,8 +2,8 @@ use config::Config;
use reqwest::header;
use rss::{Channel, ChannelBuilder, ItemBuilder};
use scraper::{Html, Selector};
use std::{error::Error, fs::File, io::BufReader};
use std::collections::HashMap;
use std::{error::Error, fs::File, io::BufReader};
use chrono::prelude::*;
@ -17,7 +17,7 @@ fn get_last_status(title: &str) -> Result<String, Box<dyn Error>> {
let c = ChannelBuilder::default()
.title("Bib Watcher".to_string())
.description("Watches availability status of library books".to_string())
.link("https://phlaym.net".to_string())
.link("https://feeds.phlaym.net/".to_string() + FILE_NAME)
.build();
c.pretty_write_to(File::create(FILE_NAME)?, b' ', 4)?;
Ok(c)
@ -38,9 +38,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.add_source(config::File::with_name("config.json"))
.build()?;
let books = settings.try_deserialize::<HashMap<String, String>>()?;
let searches = books
.iter()
.collect::<HashMap<&String,&String>>();
let searches = books.iter().collect::<HashMap<&String, &String>>();
for (title, url) in searches {
search(url, title)?
}
@ -68,7 +66,12 @@ fn search(url: &str, title: &str) -> Result<(), Box<dyn Error>> {
.build()?;
let request = client.get(url).build()?;
let response = client.execute(request)?;
let status = parse(&response.text()?)?;
let response_text = response.text()?;
let mut status = parse(&response_text.clone())?;
if status == "online" {
let id = parse_id(&response_text)?;
status = check_ebook_status(id)?;
}
let last_status = get_last_status(title)?;
if status != last_status {
let file = File::open(FILE_NAME);
@ -77,7 +80,7 @@ fn search(url: &str, title: &str) -> Result<(), Box<dyn Error>> {
Err(_) => Ok(ChannelBuilder::default()
.title("Bib Watcher".to_string())
.description("Watches availability status of library books".to_string())
.link("https://phlaym.net".to_string())
.link("https://feeds.phlaym.net/".to_string() + FILE_NAME)
.build()),
}?;
let mut items = channel.clone().into_items();
@ -95,6 +98,23 @@ fn search(url: &str, title: &str) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn check_ebook_status(ebook_id: u32) -> Result<String, Box<dyn Error>> {
let id_param = ebook_id.to_string();
let client = reqwest::blocking::Client::new(); // ::builder().build()?;
let request = client
.post("https://webopac.winbiap.de/konstanz/service/ncipservice.ashx")
.form(&HashMap::from([
("action", "lookup"),
("itemId", &id_param),
]))
.build()?;
let response = client.execute(request)?;
let v: serde_json::Value = serde_json::from_reader(response)?;
let v2: &serde_json::Value = &v["MediaItem"]["Available"];
let a: bool = serde_json::value::from_value(v2.clone())?;
Ok((if a { "verfügbar" } else { "entliehen" }).into())
}
fn parse(html: &str) -> Result<String, Box<dyn Error>> {
let document = Html::parse_document(html);
let selector = Selector::parse("#detail-left-wrapper .mediaStatus span")?;
@ -104,3 +124,16 @@ fn parse(html: &str) -> Result<String, Box<dyn Error>> {
.map(|r| r.text().collect::<String>())
.unwrap_or_else(|| "Unknown".into()))
}
fn parse_id(html: &str) -> Result<u32, Box<dyn Error>> {
let document = Html::parse_document(html);
let selector = Selector::parse("#hyperlinkDiviBib")?;
document
.select(&selector)
.next()
.and_then(|r| r.value().attr("data-id"))
.unwrap_or("Unknown")
.parse::<u32>()
.map_err(From::from)
}