diff --git a/Cargo.lock b/Cargo.lock index 0dddb0e..a7e38ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -741,6 +741,7 @@ dependencies = [ "arraygen", "reqwest", "scraper", + "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c036ee8..89ca7f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" [dependencies] reqwest = { version = "0.10", features = ["blocking"]} scraper = "0.12.0" -arraygen = "0.1.11" \ No newline at end of file +arraygen = "0.1.11" +url = "2.1.1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6c0ead1..da37f42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,14 @@ pub mod price_result; pub mod parser; pub mod price_checker; -extern crate reqwest; -extern crate scraper; use price_result::PriceResult; use price_checker::PriceChecker; +use url::Url; fn main() { let price_checker = PriceChecker::new(); - let price_result = price_checker.get_price("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html"); - println!("Product «{}: {}» price {}€", price_result.product, price_result.name, price_result.price); + 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()); + println!("{}", price_result); } diff --git a/src/parser.rs b/src/parser.rs index 44a89fa..76a8232 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,14 +1,16 @@ pub mod darty; extern crate arraygen; +extern crate url; use scraper::Html; use crate::price_result::PriceResult; use arraygen::Arraygen; +use url::Url; pub trait Parser{ fn new() -> Self where Self :Sized; - fn can_parse(&self, url : &str) -> bool; + fn can_parse(&self, url : &Url) -> bool; fn parse(&self, html : &Html) -> PriceResult; } diff --git a/src/parser/darty.rs b/src/parser/darty.rs index 2a8d223..02c647a 100644 --- a/src/parser/darty.rs +++ b/src/parser/darty.rs @@ -1,6 +1,7 @@ use super::Parser; use crate::PriceResult; use scraper::{Selector, Html}; +use url::Url; #[derive(Debug)] pub struct Darty { @@ -18,8 +19,8 @@ impl Parser for Darty { } } - fn can_parse(&self, url : &str) -> bool { - url.contains("darty") + fn can_parse(&self, url : &Url) -> bool { + url.host_str().unwrap() == "www.darty.com" } fn parse(&self, html : &Html) -> PriceResult { diff --git a/src/price_checker.rs b/src/price_checker.rs index d07a7b8..2efb0a9 100644 --- a/src/price_checker.rs +++ b/src/price_checker.rs @@ -1,9 +1,11 @@ extern crate reqwest; +extern crate scraper; use reqwest::blocking::Client; use scraper::Html; use crate::parser; use crate::price_result::PriceResult; +use url::Url; const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"; @@ -21,12 +23,12 @@ impl PriceChecker { } } - pub fn get_price(&self, url : &str) -> PriceResult { - let response = self.client.get(url).send().unwrap(); + pub fn get_price(&self, url : Url) -> PriceResult { + let response = self.client.get(url.clone()).send().unwrap(); let text = response.text().unwrap(); let document = Html::parse_document(&text); for parser in self.parser_list.get().iter() { - if parser.can_parse(url) { + if parser.can_parse(&url) { return parser.parse(&document); } } diff --git a/src/price_result.rs b/src/price_result.rs index 540cf04..a17fa89 100644 --- a/src/price_result.rs +++ b/src/price_result.rs @@ -1,6 +1,14 @@ +use std::fmt; + #[derive(PartialEq, Clone, Debug)] pub struct PriceResult { pub name: String, pub product: String, pub price: f64 +} + +impl fmt::Display for PriceResult { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Product «{}: {}» price {}€", self.product, self.name, self.price) + } } \ No newline at end of file