use scraper::{Html, Selector}; use crate::context::Context; use crate::probe::{Entry, Probe}; fn from_html(document: &str) -> Result { let html = Html::parse_document(document); let mut messages = Vec::new(); let history = Vec::new(); let comments = Vec::new(); let selector = Selector::parse(".panel-heading > h1:nth-child(3)").unwrap(); if let Some(element) = html.select(&selector).next() { let message = element.inner_html(); let message = htmlescape::decode_html(&message).unwrap(); messages.push(message); } if messages.is_empty() { Err(()) } else { Ok(Entry { messages, history, comments, }) } } pub struct KonsumentInfo; impl Probe for KonsumentInfo { fn uri(&self, number: &str) -> String { format!("http://konsumentinfo.se/telefonnummer/sverige/{}", number) } fn search(&mut self, ctx: &mut Context, number: &str) -> Result { let body = if let Some(cache) = ctx.cache_get("konsument_info", &number) { String::from_utf8(cache.data).unwrap() } else { let body = reqwest::get(&self.uri(number)).unwrap().text().unwrap(); ctx.cache_set("konsument_info", &number, body.as_bytes()) .expect("wut?! why not?!"); body }; from_html(&body) } } #[cfg(test)] mod tests { use insta::assert_yaml_snapshot_matches; use super::*; #[test] fn test_0104754350() { let document = include_str!("../../fixtures/konsumentinfo/0104754350.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } #[test] fn test_0313908905() { let document = include_str!("../../fixtures/konsumentinfo/0313908905.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } #[test] fn test_0702269893() { let document = include_str!("../../fixtures/konsumentinfo/0702269893.html"); assert_yaml_snapshot_matches!(from_html(&document), @r###"Ok: messages: - Hydroscand AB history: [] comments: []"###); } #[test] fn test_0726443387() { let document = include_str!("../../fixtures/konsumentinfo/0726443387.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } #[test] fn test_0751793426() { let document = include_str!("../../fixtures/konsumentinfo/0751793426.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } #[test] fn test_0751793483() { let document = include_str!("../../fixtures/konsumentinfo/0751793483.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } #[test] fn test_0751793499() { let document = include_str!("../../fixtures/konsumentinfo/0751793499.html"); assert_yaml_snapshot_matches!(from_html(&document), @"Err: ~"); } }