|
@@ -1,6 +1,8 @@
|
|
use chrono::{DateTime, Duration, Local, Utc};
|
|
use chrono::{DateTime, Duration, Local, Utc};
|
|
use parse_duration::parse;
|
|
use parse_duration::parse;
|
|
use reqwest;
|
|
use reqwest;
|
|
|
|
+use reqwest::blocking::Client;
|
|
|
|
+
|
|
use select::document::Document;
|
|
use select::document::Document;
|
|
use select::predicate::Name;
|
|
use select::predicate::Name;
|
|
use std::collections::HashMap;
|
|
use std::collections::HashMap;
|
|
@@ -8,6 +10,8 @@ use std::collections::HashMap;
|
|
use url::form_urlencoded::{byte_serialize};
|
|
use url::form_urlencoded::{byte_serialize};
|
|
use log::*;
|
|
use log::*;
|
|
|
|
|
|
|
|
+const MAXCLIENTS: usize = 8;
|
|
|
|
+
|
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
|
pub struct Auction {
|
|
pub struct Auction {
|
|
pub price: f32,
|
|
pub price: f32,
|
|
@@ -22,7 +26,11 @@ pub struct Auction {
|
|
|
|
|
|
|
|
|
|
impl Auction {
|
|
impl Auction {
|
|
- fn update(&mut self) {
|
|
|
|
|
|
+ fn update(&mut self, client: &Client) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
// println!("now: {:?} auction: {:?}", Utc::now().with_timezone(&Local).timestamp(), self.timestamp);
|
|
// println!("now: {:?} auction: {:?}", Utc::now().with_timezone(&Local).timestamp(), self.timestamp);
|
|
let was_not_final = self.is_price_final;
|
|
let was_not_final = self.is_price_final;
|
|
self.is_price_final = Utc::now().with_timezone(&Local).timestamp() > self.timestamp;
|
|
self.is_price_final = Utc::now().with_timezone(&Local).timestamp() > self.timestamp;
|
|
@@ -32,7 +40,7 @@ impl Auction {
|
|
dbg!(&self.price);
|
|
dbg!(&self.price);
|
|
}
|
|
}
|
|
|
|
|
|
- *self = auction_details(&self);
|
|
|
|
|
|
+ *self = auction_details(&self, client);
|
|
|
|
|
|
// if was_not_final != self.is_price_final {
|
|
// if was_not_final != self.is_price_final {
|
|
// self.price = auction_details(&self.url);
|
|
// self.price = auction_details(&self.url);
|
|
@@ -56,13 +64,16 @@ impl Query {
|
|
fn get_avg_price(&mut self) -> f32 {
|
|
fn get_avg_price(&mut self) -> f32 {
|
|
self.auctions.iter().map(|(_u, a)| a.price).sum::<f32>() / self.auctions.len() as f32
|
|
self.auctions.iter().map(|(_u, a)| a.price).sum::<f32>() / self.auctions.len() as f32
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
- fn detect_expired(&self) -> HashMap<String, Auction> {
|
|
|
|
|
|
+ fn detect_expired(&self, client: &Client) -> HashMap<String, Auction> {
|
|
self.auctions
|
|
self.auctions
|
|
.iter()
|
|
.iter()
|
|
.map(|(u, a)| {
|
|
.map(|(u, a)| {
|
|
let mut a1 = a.clone();
|
|
let mut a1 = a.clone();
|
|
- a1.update();
|
|
|
|
|
|
+ a1.update(client);
|
|
(u.clone(), a1)
|
|
(u.clone(), a1)
|
|
})
|
|
})
|
|
.collect()
|
|
.collect()
|
|
@@ -77,13 +88,13 @@ pub struct Account {
|
|
}
|
|
}
|
|
|
|
|
|
impl Account {
|
|
impl Account {
|
|
- pub fn run_queries(&self) -> HashMap<String, Query> {
|
|
|
|
|
|
+ pub fn run_queries(&self, client: &Client) -> HashMap<String, Query> {
|
|
self.queries
|
|
self.queries
|
|
.clone()
|
|
.clone()
|
|
.into_iter()
|
|
.into_iter()
|
|
.map(|(url, mut query)| {
|
|
.map(|(url, mut query)| {
|
|
(url.clone(), {
|
|
(url.clone(), {
|
|
- query.auctions.extend(auctions_from_url(&url));
|
|
|
|
|
|
+ query.auctions.extend(auctions_from_url(&url, client));
|
|
query.avg_price = query.get_avg_price();
|
|
query.avg_price = query.get_avg_price();
|
|
if cfg!(debug_assertions) {
|
|
if cfg!(debug_assertions) {
|
|
// dbg!(&query);
|
|
// dbg!(&query);
|
|
@@ -92,15 +103,15 @@ impl Account {
|
|
})
|
|
})
|
|
})
|
|
})
|
|
.map(|(url, mut query)| {
|
|
.map(|(url, mut query)| {
|
|
- query.auctions = query.detect_expired();
|
|
|
|
|
|
+ query.auctions = query.detect_expired(client);
|
|
(url, query)
|
|
(url, query)
|
|
})
|
|
})
|
|
.collect()
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn updated(&self) -> Account {
|
|
|
|
|
|
+ pub fn updated(&self, client: &Client) -> Account {
|
|
Account {
|
|
Account {
|
|
- queries: self.run_queries(), // .iter()
|
|
|
|
|
|
+ queries: self.run_queries(client), // .iter()
|
|
// .map(|(k,v)| (k.clone(), Query{auctions: v.clone(), .. Default::default()}))
|
|
// .map(|(k,v)| (k.clone(), Query{auctions: v.clone(), .. Default::default()}))
|
|
// .collect()
|
|
// .collect()
|
|
}
|
|
}
|
|
@@ -137,17 +148,18 @@ fn parse_price(price: &str) -> Option<f32> {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-fn auction_details(auction: &Auction) -> Auction {
|
|
|
|
|
|
+fn auction_details(auction: &Auction, client: &Client) -> Auction {
|
|
let mut updated_auction = auction.clone();
|
|
let mut updated_auction = auction.clone();
|
|
|
|
|
|
if auction.is_price_final {
|
|
if auction.is_price_final {
|
|
return updated_auction;
|
|
return updated_auction;
|
|
}
|
|
}
|
|
|
|
|
|
- let client = reqwest::blocking::Client::builder()
|
|
|
|
- // .timeout(Some(core::time::Duration::from_secs(10)))
|
|
|
|
- .build()
|
|
|
|
- .unwrap();
|
|
|
|
|
|
+ // let client = reqwest::blocking::Client::builder()
|
|
|
|
+ // .max_idle_per_host(8)
|
|
|
|
+ // // .timeout(Some(core::time::Duration::from_secs(10)))
|
|
|
|
+ // .build()
|
|
|
|
+ // .unwrap();
|
|
|
|
|
|
|
|
|
|
match client.get(&auction.url).send() {
|
|
match client.get(&auction.url).send() {
|
|
@@ -202,14 +214,11 @@ fn abs_image_url(rel_url: &str) -> String {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-fn auctions_from_url(url: &str) -> HashMap<String, Auction> {
|
|
|
|
|
|
+fn auctions_from_url(url: &str, client: &Client) -> HashMap<String, Auction> {
|
|
let mut auctions = HashMap::new();
|
|
let mut auctions = HashMap::new();
|
|
// dbg!(&url);
|
|
// dbg!(&url);
|
|
|
|
|
|
- let client = reqwest::blocking::Client::builder()
|
|
|
|
- // .timeout(Some(core::time::Duration::from_secs(8)))
|
|
|
|
- .build()
|
|
|
|
- .unwrap();
|
|
|
|
|
|
+
|
|
|
|
|
|
match client.get(url).send() {
|
|
match client.get(url).send() {
|
|
Ok(mut resp) => {
|
|
Ok(mut resp) => {
|