use super::{Parser, PriceParser}; use crate::PriceResult; use scraper::{Selector, Html}; use url::Url; use anyhow::{Result, anyhow}; #[derive(Debug)] /// Parser for the darty website pub struct Darty { price_selector: Selector, name_selector: Selector, product_selector: Selector } impl Parser for Darty { fn new() -> Result { Ok(Darty { price_selector: Selector::parse(r#".darty_prix"#).unwrap(), name_selector: Selector::parse(r#".product_name"#).unwrap(), product_selector: Selector::parse(r#".product_family"#).unwrap() }) } fn name() -> &'static str { "Darty" } } impl PriceParser for Darty { fn can_parse(&self, url : &Url) -> bool { url.host_str().unwrap_or("") == "www.darty.com" } fn parse(&self, html : &Html) -> Result { // Get price let price_element = html.select(&self.price_selector).next().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").trim_end_matches('€').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().replace('\n', "-"); // Get product let family_element = html.select(&self.product_selector).next().ok_or(anyhow!("No product element"))?; let family = family_element.text().next().unwrap_or("").trim().replace('\n', "-"); Ok(PriceResult { name: name.to_owned(), product: family.to_owned(), price }) } } #[test] fn test_parser_darty() { let darty_parser = Darty::new().unwrap(); assert!(darty_parser.can_parse(&Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap())); assert!(darty_parser.can_parse(&Url::parse("http://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap())); assert!(darty_parser.can_parse(&Url::parse("https://www.fnace.com").unwrap()) == false); }