63 lines
1.8 KiB
Rust
63 lines
1.8 KiB
Rust
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 DuBruitDansLaCuisine {
|
|
price_selector: Selector,
|
|
name_selector: Selector,
|
|
}
|
|
|
|
impl Parser for DuBruitDansLaCuisine {
|
|
fn new() -> Result<Self> {
|
|
Ok(DuBruitDansLaCuisine {
|
|
price_selector: Selector::parse(r#".price"#).unwrap(),
|
|
name_selector: Selector::parse(r#".product.item"#).unwrap(),
|
|
})
|
|
}
|
|
|
|
fn name(&self) -> &'static str {
|
|
"Du bruit dans la Cuisine"
|
|
}
|
|
}
|
|
|
|
impl PriceParser for DuBruitDansLaCuisine {
|
|
fn can_parse(&self, url: &Url) -> bool {
|
|
url.host_str().unwrap_or("") == "www.dubruitdanslacuisine.fr"
|
|
}
|
|
|
|
fn parse_price(&self, html: &Html) -> Result<PriceResult> {
|
|
// 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: f64 = price_text_it
|
|
.next()
|
|
.unwrap_or("0.")
|
|
.trim_end_matches("€")
|
|
.trim()
|
|
.replace(',', ".")
|
|
.parse()?;
|
|
|
|
// Get name
|
|
let name_element = html.select(&self.name_selector).next().ok_or(anyhow!("No name element"))?;
|
|
let name = name_element.text().nth(1).unwrap_or("").trim();
|
|
|
|
Ok(PriceResult {
|
|
name: name.to_owned(),
|
|
product: "Cuisine".to_owned(),
|
|
price,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_parser_du_bruit_dans_la_cuisine() {
|
|
let parser = DuBruitDansLaCuisine::new().unwrap();
|
|
assert!(parser.can_parse(&Url::parse("https://www.dubruitdanslacuisine.fr/tapis-a-patisserie-40-62-14377-p").unwrap()));
|
|
assert!(parser.can_parse(&Url::parse("https://www.dubruitdanslacuisine.fr/tapis-a-patisserie-40-62-14377-p").unwrap()));
|
|
assert!(parser.can_parse(&Url::parse("https://www.dubrutdanslacuisine.fr/").unwrap()) == false);
|
|
}
|