48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use super::Parser;
|
|
use crate::PriceResult;
|
|
use scraper::{Selector, Html};
|
|
use url::Url;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Darty {
|
|
price_selector: Selector,
|
|
name_selector: Selector,
|
|
product_selector: Selector
|
|
}
|
|
|
|
impl Parser for Darty {
|
|
fn new() -> Self {
|
|
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 can_parse(&self, url : &Url) -> bool {
|
|
url.host_str().unwrap() == "www.darty.com"
|
|
}
|
|
|
|
fn parse(&self, html : &Html) -> PriceResult {
|
|
// Get price
|
|
let price_element = html.select(&self.price_selector).next().unwrap();
|
|
let mut price_text_it = price_element.text();
|
|
let price_ent : u32 = price_text_it.next().unwrap_or("0").trim_end_matches(',').parse().unwrap();
|
|
let price_dec : u32 = price_text_it.next().unwrap_or("0").trim_end_matches('€').parse().unwrap();
|
|
let price = price_ent as f64 + (price_dec as f64) / 100.;
|
|
|
|
// Get name
|
|
let name_element = html.select(&self.name_selector).next().unwrap();
|
|
let name = name_element.text().next().unwrap().trim().replace('\n', "-");
|
|
|
|
// Get product
|
|
let family_element = html.select(&self.product_selector).next().unwrap();
|
|
let family = family_element.text().next().unwrap().trim().replace('\n', "-");
|
|
|
|
PriceResult {
|
|
name: name.to_owned(),
|
|
product: family.to_owned(),
|
|
price
|
|
}
|
|
}
|
|
} |