Add doc and tests
This commit is contained in:
parent
4bd5e1eb5e
commit
3ace078024
4 changed files with 39 additions and 0 deletions
|
@ -9,23 +9,35 @@ use arraygen::Arraygen;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
/// Trait needed to get price from a specific website
|
||||||
pub trait PriceParser{
|
pub trait PriceParser{
|
||||||
|
/// Create a new parser
|
||||||
fn new() -> Result<Self> where Self :Sized;
|
fn new() -> Result<Self> where Self :Sized;
|
||||||
|
/// Indicate if it can parse this URL
|
||||||
fn can_parse(&self, url : &Url) -> bool;
|
fn can_parse(&self, url : &Url) -> bool;
|
||||||
|
/// Parse the html into a price
|
||||||
fn parse(&self, html : &Html) -> Result<PriceResult>;
|
fn parse(&self, html : &Html) -> Result<PriceResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Arraygen, Debug)]
|
#[derive(Arraygen, Debug)]
|
||||||
#[gen_array(pub fn get_price: & dyn PriceParser)]
|
#[gen_array(pub fn get_price: & dyn PriceParser)]
|
||||||
|
/// Represent the list of all the parser
|
||||||
pub struct List {
|
pub struct List {
|
||||||
#[in_array(get_price)]
|
#[in_array(get_price)]
|
||||||
darty: darty::Darty
|
darty: darty::Darty
|
||||||
}
|
}
|
||||||
|
|
||||||
impl List {
|
impl List {
|
||||||
|
/// Create the list
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
Ok(List {
|
Ok(List {
|
||||||
darty: darty::Darty::new()?
|
darty: darty::Darty::new()?
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parser_list() {
|
||||||
|
let parser_list = List::new().unwrap();
|
||||||
|
assert_eq!(parser_list.get_price().len(), 1);
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@ use url::Url;
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
/// Parser for the darty website
|
||||||
pub struct Darty {
|
pub struct Darty {
|
||||||
price_selector: Selector,
|
price_selector: Selector,
|
||||||
name_selector: Selector,
|
name_selector: Selector,
|
||||||
|
@ -46,4 +47,12 @@ impl PriceParser for Darty {
|
||||||
price
|
price
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parser_darty() {
|
||||||
|
let darty_parser = Darty::new().unwrap();
|
||||||
|
assert!(darty_parser.can_parse(&Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()));
|
||||||
|
assert!(darty_parser.can_parse(&Url::parse("http://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()));
|
||||||
|
assert!(darty_parser.can_parse(&Url::parse("https://www.fnace.com").unwrap()) == false);
|
||||||
}
|
}
|
|
@ -10,12 +10,14 @@ use anyhow::{Result, anyhow};
|
||||||
|
|
||||||
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0";
|
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0";
|
||||||
|
|
||||||
|
/// Struct used to get price from a website
|
||||||
pub struct PriceChecker {
|
pub struct PriceChecker {
|
||||||
client: Client,
|
client: Client,
|
||||||
parser_list: parser::List
|
parser_list: parser::List
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PriceChecker {
|
impl PriceChecker {
|
||||||
|
/// Create a new PriceChecker
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build()?;
|
let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build()?;
|
||||||
Ok(PriceChecker {
|
Ok(PriceChecker {
|
||||||
|
@ -24,6 +26,7 @@ impl PriceChecker {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a price from an URL
|
||||||
pub fn get_price(&self, url : Url) -> Result<PriceResult> {
|
pub fn get_price(&self, url : Url) -> Result<PriceResult> {
|
||||||
let response = self.client.get(url.clone()).send()?;
|
let response = self.client.get(url.clone()).send()?;
|
||||||
let text = response.text()?;
|
let text = response.text()?;
|
||||||
|
@ -31,4 +34,15 @@ impl PriceChecker {
|
||||||
let parser = *self.parser_list.get_price().iter().find(|p| p.can_parse(&url)).ok_or(anyhow!("No parser can parse {}", url))?;
|
let parser = *self.parser_list.get_price().iter().find(|p| p.can_parse(&url)).ok_or(anyhow!("No parser can parse {}", url))?;
|
||||||
Ok(parser.parse(&document)?)
|
Ok(parser.parse(&document)?)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_price_checker() {
|
||||||
|
let price_checker = PriceChecker::new().unwrap();
|
||||||
|
|
||||||
|
// Test darty
|
||||||
|
let price_result = price_checker.get_price(Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()).unwrap();
|
||||||
|
assert!(price_result.name != "");
|
||||||
|
assert!(price_result.price != 0.);
|
||||||
|
assert!(price_result.product != "");
|
||||||
}
|
}
|
|
@ -1,9 +1,13 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Debug)]
|
#[derive(PartialEq, Clone, Debug)]
|
||||||
|
/// Price result
|
||||||
pub struct PriceResult {
|
pub struct PriceResult {
|
||||||
|
/// The name of object
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
/// The product type
|
||||||
pub product: String,
|
pub product: String,
|
||||||
|
/// The price
|
||||||
pub price: f64
|
pub price: f64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue