34 lines
No EOL
938 B
Rust
34 lines
No EOL
938 B
Rust
extern crate reqwest;
|
|
extern crate scraper;
|
|
|
|
use reqwest::blocking::Client;
|
|
use scraper::Html;
|
|
use crate::parser;
|
|
use crate::price_result::PriceResult;
|
|
use url::Url;
|
|
use anyhow::{Result, anyhow};
|
|
|
|
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() -> Result<Self> {
|
|
let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build()?;
|
|
Ok(PriceChecker {
|
|
client,
|
|
parser_list: parser::List::new()?
|
|
})
|
|
}
|
|
|
|
pub fn get_price(&self, url : Url) -> Result<PriceResult> {
|
|
let response = self.client.get(url.clone()).send()?;
|
|
let text = response.text()?;
|
|
let document = Html::parse_document(&text);
|
|
let parser = *self.parser_list.get_price().iter().find(|p| p.can_parse(&url)).ok_or(anyhow!("No parser can parse {}", url))?;
|
|
Ok(parser.parse(&document)?)
|
|
}
|
|
} |