67 lines
2.1 KiB
Rust
67 lines
2.1 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 Fnac {
|
|
price_selector: Selector,
|
|
name_selector: Selector,
|
|
product_selector: Selector,
|
|
}
|
|
|
|
impl Parser for Fnac {
|
|
fn new() -> Result<Self> {
|
|
Ok(Fnac {
|
|
price_selector: Selector::parse(r#".f-priceBox-price.checked"#).unwrap(),
|
|
name_selector: Selector::parse(r#".f-productHeader-Title"#).unwrap(),
|
|
product_selector: Selector::parse(r#".f-productHeader-subTitleLink"#).unwrap(),
|
|
})
|
|
}
|
|
|
|
fn name(&self) -> &'static str {
|
|
"Fnac"
|
|
}
|
|
}
|
|
|
|
impl PriceParser for Fnac {
|
|
fn can_parse(&self, url: &Url) -> bool {
|
|
url.host_str().unwrap_or("") == "www.fnac.com"
|
|
}
|
|
|
|
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_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_start_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_fnac() {
|
|
let fnac_parser = Fnac::new().unwrap();
|
|
assert!(
|
|
fnac_parser.can_parse(&Url::parse("https://www.fnac.com/Apple-iPhone-XS-64-Go-5-8-Argent/a12849718/w-4?CtoPid=488371").unwrap())
|
|
);
|
|
assert!(fnac_parser.can_parse(&Url::parse("http://www.fnac.com/Apple-iPhone-XS-64-Go-5-8-Argent/a12849718/w-4?CtoPid=488371").unwrap()));
|
|
assert!(fnac_parser.can_parse(&Url::parse("https://www.fnace.com").unwrap()) == false);
|
|
}
|