Add Parser trait
This commit is contained in:
parent
f47b5f3b33
commit
cec9900b45
7 changed files with 51 additions and 16 deletions
|
@ -1,5 +1,7 @@
|
|||
extern crate anyhow;
|
||||
extern crate clap;
|
||||
extern crate url;
|
||||
extern crate arraygen;
|
||||
|
||||
pub mod price_result;
|
||||
pub mod parser;
|
||||
|
|
|
@ -4,19 +4,21 @@ pub mod du_bruit_dans_la_cuisine;
|
|||
pub mod ldlc;
|
||||
pub mod amazon;
|
||||
|
||||
extern crate arraygen;
|
||||
extern crate url;
|
||||
|
||||
use scraper::Html;
|
||||
use crate::price_result::PriceResult;
|
||||
use arraygen::Arraygen;
|
||||
use url::Url;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Trait needed to get price from a specific website
|
||||
pub trait PriceParser{
|
||||
/// Create a new parser
|
||||
pub trait Parser{
|
||||
/// Create the parser
|
||||
fn new() -> Result<Self> where Self : Sized;
|
||||
/// Get the name
|
||||
fn name() -> &'static str where Self : Sized;
|
||||
}
|
||||
|
||||
/// Trait needed to get price from a specific website
|
||||
pub trait PriceParser : Parser{
|
||||
/// Indicate if it can parse this URL
|
||||
fn can_parse(&self, url : &Url) -> bool;
|
||||
/// Parse the html into a price
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::PriceParser;
|
||||
use super::{Parser, PriceParser};
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
use url::Url;
|
||||
|
@ -12,7 +12,7 @@ pub struct Amazon {
|
|||
product_selector: Selector
|
||||
}
|
||||
|
||||
impl PriceParser for Amazon {
|
||||
impl Parser for Amazon {
|
||||
fn new() -> Result<Self> {
|
||||
Ok(Amazon {
|
||||
price_selector: Selector::parse(r"#priceblock_ourprice").unwrap(),
|
||||
|
@ -21,6 +21,13 @@ impl PriceParser for Amazon {
|
|||
})
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"Amazon"
|
||||
}
|
||||
}
|
||||
|
||||
impl PriceParser for Amazon {
|
||||
|
||||
fn can_parse(&self, url : &Url) -> bool {
|
||||
url.host_str().unwrap_or("") == "www.amazon.fr"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::PriceParser;
|
||||
use super::{Parser, PriceParser};
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
use url::Url;
|
||||
|
@ -12,7 +12,7 @@ pub struct Darty {
|
|||
product_selector: Selector
|
||||
}
|
||||
|
||||
impl PriceParser for Darty {
|
||||
impl Parser for Darty {
|
||||
fn new() -> Result<Self> {
|
||||
Ok(Darty {
|
||||
price_selector: Selector::parse(r#".darty_prix"#).unwrap(),
|
||||
|
@ -21,6 +21,12 @@ impl PriceParser for Darty {
|
|||
})
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"Darty"
|
||||
}
|
||||
}
|
||||
|
||||
impl PriceParser for Darty {
|
||||
fn can_parse(&self, url : &Url) -> bool {
|
||||
url.host_str().unwrap_or("") == "www.darty.com"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::PriceParser;
|
||||
use super::{PriceParser, Parser};
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
use url::Url;
|
||||
|
@ -11,7 +11,7 @@ pub struct DuBruitDansLaCuisine {
|
|||
name_selector: Selector,
|
||||
}
|
||||
|
||||
impl PriceParser for DuBruitDansLaCuisine {
|
||||
impl Parser for DuBruitDansLaCuisine {
|
||||
fn new() -> Result<Self> {
|
||||
Ok(DuBruitDansLaCuisine {
|
||||
price_selector: Selector::parse(r#".price"#).unwrap(),
|
||||
|
@ -19,6 +19,12 @@ impl PriceParser for DuBruitDansLaCuisine {
|
|||
})
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"Du bruit dans la Cuisine"
|
||||
}
|
||||
}
|
||||
|
||||
impl PriceParser for DuBruitDansLaCuisine {
|
||||
fn can_parse(&self, url : &Url) -> bool {
|
||||
url.host_str().unwrap_or("") == "www.dubruitdanslacuisine.fr"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::PriceParser;
|
||||
use super::{Parser, PriceParser};
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
use url::Url;
|
||||
|
@ -12,7 +12,7 @@ pub struct Fnac {
|
|||
product_selector: Selector
|
||||
}
|
||||
|
||||
impl PriceParser for Fnac {
|
||||
impl Parser for Fnac{
|
||||
fn new() -> Result<Self> {
|
||||
Ok(Fnac {
|
||||
price_selector: Selector::parse(r#".f-priceBox-price.checked"#).unwrap(),
|
||||
|
@ -21,6 +21,12 @@ impl PriceParser for Fnac {
|
|||
})
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"Fnac"
|
||||
}
|
||||
}
|
||||
|
||||
impl PriceParser for Fnac {
|
||||
fn can_parse(&self, url : &Url) -> bool {
|
||||
url.host_str().unwrap_or("") == "www.fnac.com"
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::PriceParser;
|
||||
use super::{PriceParser, Parser};
|
||||
use crate::PriceResult;
|
||||
use scraper::{Selector, Html};
|
||||
use url::Url;
|
||||
|
@ -11,7 +11,7 @@ pub struct LDLC {
|
|||
name_selector: Selector,
|
||||
}
|
||||
|
||||
impl PriceParser for LDLC {
|
||||
impl Parser for LDLC {
|
||||
fn new() -> Result<Self> {
|
||||
Ok(LDLC {
|
||||
price_selector: Selector::parse(r#".price"#).unwrap(),
|
||||
|
@ -19,6 +19,12 @@ impl PriceParser for LDLC {
|
|||
})
|
||||
}
|
||||
|
||||
fn name() -> &'static str {
|
||||
"LDLC"
|
||||
}
|
||||
}
|
||||
|
||||
impl PriceParser for LDLC {
|
||||
fn can_parse(&self, url : &Url) -> bool {
|
||||
url.host_str().unwrap_or("") == "www.ldlc.com"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue