use super::{Parser, PriceParser}; use crate::PriceResult; use anyhow::{anyhow, Result}; use scraper::{Html, Selector}; use url::Url; #[derive(Debug)] /// Parser for the darty website pub struct LDLC { price_selector: Selector, name_selector: Selector, } impl Parser for LDLC { fn new() -> Result { Ok(LDLC { price_selector: Selector::parse(r#".price"#).unwrap(), name_selector: Selector::parse(r#".title-1"#).unwrap(), }) } fn name(&self) -> &'static str { "LDLC" } } impl PriceParser for LDLC { fn can_parse(&self, url: &Url) -> bool { url.host_str().unwrap_or("") == "www.ldlc.com" } fn parse_price(&self, html: &Html) -> Result { // Get price let price_element = html.select(&self.price_selector).nth(4).ok_or(anyhow!("No price element"))?; let mut price_text_it = price_element.text(); let price_ent: u32 = price_text_it.next().unwrap_or("0").trim_end_matches('€').parse()?; let price_dec: u32 = price_text_it.next().unwrap_or("0").parse()?; let price = price_ent as f64 + (price_dec as f64) / 100.; // Get name let name_element = html.select(&self.name_selector).next().ok_or(anyhow!("No name element"))?; let name = name_element.text().next().unwrap_or("").trim(); Ok(PriceResult { name: name.to_owned(), product: "High-tech".to_owned(), price, }) } } #[test] fn test_parser_du_bruit_dans_la_cuisine() { let parser = LDLC::new().unwrap(); assert!(parser.can_parse(&Url::parse("https://www.ldlc.com/fiche/PB00335410.html").unwrap())); assert!(parser.can_parse(&Url::parse("http://www.ldlc.com/fiche/PB00335410.html").unwrap())); assert!(parser.can_parse(&Url::parse("https://www.ldlv.com").unwrap()) == false); }