Utilisation de URL

This commit is contained in:
Rémi BERTHO 2020-05-23 16:19:04 +02:00
commit e48ac85011
Signed by: dalan
GPG key ID: EE3B917931C07B64
7 changed files with 25 additions and 11 deletions

1
Cargo.lock generated
View file

@ -741,6 +741,7 @@ dependencies = [
"arraygen", "arraygen",
"reqwest", "reqwest",
"scraper", "scraper",
"url",
] ]
[[package]] [[package]]

View file

@ -9,4 +9,5 @@ edition = "2018"
[dependencies] [dependencies]
reqwest = { version = "0.10", features = ["blocking"]} reqwest = { version = "0.10", features = ["blocking"]}
scraper = "0.12.0" scraper = "0.12.0"
arraygen = "0.1.11" arraygen = "0.1.11"
url = "2.1.1"

View file

@ -2,15 +2,14 @@ pub mod price_result;
pub mod parser; pub mod parser;
pub mod price_checker; pub mod price_checker;
extern crate reqwest;
extern crate scraper;
use price_result::PriceResult; use price_result::PriceResult;
use price_checker::PriceChecker; use price_checker::PriceChecker;
use url::Url;
fn main() { fn main() {
let price_checker = PriceChecker::new(); 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"); 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!("Product «{}: {}» price {}", price_result.product, price_result.name, price_result.price); println!("{}", price_result);
} }

View file

@ -1,14 +1,16 @@
pub mod darty; pub mod darty;
extern crate arraygen; extern crate arraygen;
extern crate url;
use scraper::Html; use scraper::Html;
use crate::price_result::PriceResult; use crate::price_result::PriceResult;
use arraygen::Arraygen; use arraygen::Arraygen;
use url::Url;
pub trait Parser{ pub trait Parser{
fn new() -> Self where Self :Sized; 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; fn parse(&self, html : &Html) -> PriceResult;
} }

View file

@ -1,6 +1,7 @@
use super::Parser; use super::Parser;
use crate::PriceResult; use crate::PriceResult;
use scraper::{Selector, Html}; use scraper::{Selector, Html};
use url::Url;
#[derive(Debug)] #[derive(Debug)]
pub struct Darty { pub struct Darty {
@ -18,8 +19,8 @@ impl Parser for Darty {
} }
} }
fn can_parse(&self, url : &str) -> bool { fn can_parse(&self, url : &Url) -> bool {
url.contains("darty") url.host_str().unwrap() == "www.darty.com"
} }
fn parse(&self, html : &Html) -> PriceResult { fn parse(&self, html : &Html) -> PriceResult {

View file

@ -1,9 +1,11 @@
extern crate reqwest; extern crate reqwest;
extern crate scraper;
use reqwest::blocking::Client; use reqwest::blocking::Client;
use scraper::Html; use scraper::Html;
use crate::parser; use crate::parser;
use crate::price_result::PriceResult; 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"; 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 { pub fn get_price(&self, url : Url) -> PriceResult {
let response = self.client.get(url).send().unwrap(); let response = self.client.get(url.clone()).send().unwrap();
let text = response.text().unwrap(); let text = response.text().unwrap();
let document = Html::parse_document(&text); let document = Html::parse_document(&text);
for parser in self.parser_list.get().iter() { for parser in self.parser_list.get().iter() {
if parser.can_parse(url) { if parser.can_parse(&url) {
return parser.parse(&document); return parser.parse(&document);
} }
} }

View file

@ -1,6 +1,14 @@
use std::fmt;
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
pub struct PriceResult { pub struct PriceResult {
pub name: String, pub name: String,
pub product: String, pub product: String,
pub price: f64 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)
}
} }