2020-07-21 16:43:29 +00:00
|
|
|
use super::{Parser, PriceParser, SearchParser};
|
2020-05-24 16:19:18 +00:00
|
|
|
use crate::PriceResult;
|
2020-07-20 20:14:05 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use scraper::{Html, Selector};
|
2020-05-24 16:19:18 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
/// Parser for the darty website
|
|
|
|
pub struct Fnac {
|
|
|
|
price_selector: Selector,
|
|
|
|
name_selector: Selector,
|
2020-07-20 20:14:05 +00:00
|
|
|
product_selector: Selector,
|
2020-07-21 16:43:29 +00:00
|
|
|
search_selector: Selector,
|
2020-05-24 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:14:05 +00:00
|
|
|
impl Parser for Fnac {
|
2020-05-24 16:19:18 +00:00
|
|
|
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(),
|
2020-07-20 20:14:05 +00:00
|
|
|
product_selector: Selector::parse(r#".f-productHeader-subTitleLink"#).unwrap(),
|
2020-07-21 16:43:29 +00:00
|
|
|
search_selector: Selector::parse(r#".Article-title"#).unwrap(),
|
2020-05-24 16:19:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:29:55 +00:00
|
|
|
fn name(&self) -> &'static str {
|
2020-06-21 09:32:40 +00:00
|
|
|
"Fnac"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PriceParser for Fnac {
|
2020-07-20 20:14:05 +00:00
|
|
|
fn can_parse(&self, url: &Url) -> bool {
|
2020-07-21 16:43:29 +00:00
|
|
|
url.host_str().unwrap_or("").ends_with("fnac.com")
|
2020-05-24 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:14:05 +00:00
|
|
|
fn parse_price(&self, html: &Html) -> Result<PriceResult> {
|
2020-05-24 16:19:18 +00:00
|
|
|
// 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();
|
2020-07-20 20:14:05 +00:00
|
|
|
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()?;
|
2020-05-24 16:19:18 +00:00
|
|
|
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(),
|
2020-07-20 20:14:05 +00:00
|
|
|
price,
|
2020-05-24 16:19:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:29 +00:00
|
|
|
impl SearchParser for Fnac {
|
|
|
|
fn search_url(&self, name: &str) -> Url {
|
|
|
|
Url::parse(&format!(
|
|
|
|
"https://www.fnac.com/SearchResult/ResultList.aspx?Search={}",
|
|
|
|
name.replace(' ', "+")
|
|
|
|
))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
fn search(&self, html: &Html) -> Result<Option<Url>> {
|
|
|
|
if let Some(search_element) = html.select(&self.search_selector).next() {
|
2020-07-21 16:52:52 +00:00
|
|
|
let path_url = search_element
|
|
|
|
.value()
|
|
|
|
.attr("href")
|
|
|
|
.ok_or(anyhow!("No link element"))?
|
|
|
|
.trim_end_matches("#omnsearchpos=1");
|
2020-07-21 16:43:29 +00:00
|
|
|
Ok(Option::Some(Url::parse(path_url)?))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 16:19:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_parser_fnac() {
|
|
|
|
let fnac_parser = Fnac::new().unwrap();
|
2020-07-20 20:14:05 +00:00
|
|
|
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())
|
|
|
|
);
|
2020-05-24 16:19:18 +00:00
|
|
|
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);
|
2020-07-20 20:14:05 +00:00
|
|
|
}
|