Add fnac
parent
3ace078024
commit
e46167777f
@ -0,0 +1,27 @@
|
||||
{
|
||||
// Utilisez IntelliSense pour en savoir plus sur les attributs possibles.
|
||||
// Pointez pour afficher la description des attributs existants.
|
||||
// Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(gdb) Lancer",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "target/debug/deps/price_checker-9ac713c382a0d250",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Activer l'impression en mode Pretty pour gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue