Ajout de classe utilitaires
This commit is contained in:
parent
c13027724f
commit
2a6c0bf432
8 changed files with 1541 additions and 8 deletions
47
src/parser/darty.rs
Normal file
47
src/parser/darty.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use super::Parser;
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
|
||||
#[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 : &str) -> bool {
|
||||
url.contains("darty")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue