This commit is contained in:
Rémi BERTHO 2020-05-24 18:19:18 +02:00
parent 3ace078024
commit e46167777f
Signed by: dalan
GPG key ID: EE3B917931C07B64
5 changed files with 126 additions and 31 deletions

View file

@ -1,4 +1,5 @@
pub mod darty;
pub mod fnac;
extern crate arraygen;
extern crate url;
@ -24,14 +25,17 @@ pub trait PriceParser{
/// Represent the list of all the parser
pub struct List {
#[in_array(get_price)]
darty: darty::Darty
darty: darty::Darty,
#[in_array(get_price)]
fnac: fnac::Fnac,
}
impl List {
/// Create the list
pub fn new() -> Result<Self> {
Ok(List {
darty: darty::Darty::new()?
darty: darty::Darty::new()?,
fnac: fnac::Fnac::new()?
})
}
}
@ -39,5 +43,5 @@ impl List {
#[test]
fn test_parser_list() {
let parser_list = List::new().unwrap();
assert_eq!(parser_list.get_price().len(), 1);
assert_eq!(parser_list.get_price().len(), 2);
}

58
src/parser/fnac.rs Normal file
View file

@ -0,0 +1,58 @@
use super::PriceParser;
use crate::PriceResult;
use scraper::{Selector, Html};
use url::Url;
use anyhow::{Result, anyhow};
#[derive(Debug)]
/// Parser for the darty website
pub struct Fnac {
price_selector: Selector,
name_selector: Selector,
product_selector: Selector
}
impl PriceParser 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 can_parse(&self, url : &Url) -> bool {
url.host_str().unwrap_or("") == "www.fnac.com"
}
fn parse(&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);
}

View file

@ -45,4 +45,10 @@ fn test_price_checker() {
assert!(price_result.name != "");
assert!(price_result.price != 0.);
assert!(price_result.product != "");
// Test fnac
let price_result = price_checker.get_price(Url::parse("https://www.fnac.com/PC-Gaming-Milenium-MM1-Ekko-R207-A536X-W-S-R48-B-D1-AMD-Ryzen-5-32-Go-RAM-500-Go-SSD-1-To-SATA-Noir/a14620943/w-4").unwrap()).unwrap();
assert!(price_result.name != "");
assert!(price_result.price != 0.);
assert!(price_result.product != "");
}