Utilisation de URL

master
Rémi BERTHO 3 years ago
commit e48ac85011
Signed by: dalan
GPG Key ID: EE3B917931C07B64
  1. 1
      Cargo.lock
  2. 3
      Cargo.toml
  3. 7
      src/main.rs
  4. 4
      src/parser.rs
  5. 5
      src/parser/darty.rs
  6. 8
      src/price_checker.rs
  7. 8
      src/price_result.rs

1
Cargo.lock generated

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

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

@ -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);
}

@ -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;
}

@ -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 {

@ -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);
}
}

@ -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)
}
}
Loading…
Cancel
Save