price_checker/src/price_checker.rs

33 lines
862 B
Rust
Raw Normal View History

2020-05-11 19:21:57 +00:00
extern crate reqwest;
2020-05-23 14:19:04 +00:00
extern crate scraper;
2020-05-11 19:21:57 +00:00
use reqwest::blocking::Client;
use scraper::Html;
use crate::parser;
use crate::price_result::PriceResult;
2020-05-23 14:19:04 +00:00
use url::Url;
2020-05-11 19:21:57 +00:00
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0";
pub struct PriceChecker {
client: Client,
parser_list: parser::List
}
impl PriceChecker {
pub fn new() -> Self {
let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build().unwrap();
PriceChecker {
client,
parser_list: parser::List::new()
}
}
2020-05-23 14:19:04 +00:00
pub fn get_price(&self, url : Url) -> PriceResult {
let response = self.client.get(url.clone()).send().unwrap();
2020-05-11 19:21:57 +00:00
let text = response.text().unwrap();
let document = Html::parse_document(&text);
2020-05-23 14:45:55 +00:00
let parser = *self.parser_list.get().iter().find(|p| p.can_parse(&url)).unwrap();
parser.parse(&document)
2020-05-11 19:21:57 +00:00
}
}