Prechádzať zdrojové kódy

add best price check

Johann Woelper 4 rokov pred
rodič
commit
19e49fd335
6 zmenil súbory, kde vykonal 429 pridanie a 333 odobranie
  1. 375 310
      Cargo.lock
  2. 11 9
      Cargo.toml
  3. 15 3
      src/main.rs
  4. 12 6
      src/query.rs
  5. 13 5
      webapp/app.js
  6. 3 0
      webapp/index.html

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 375 - 310
Cargo.lock


+ 11 - 9
Cargo.toml

@@ -1,25 +1,27 @@
 [package]
 name = "guntrader"
-version = "0.1.0"
+version = "0.2.1"
 authors = ["Johann Woelper <woelper@gmail.com>"]
 edition = "2018"
 default-run = "guntrader"
 
 [dependencies]
-reqwest = "0.9.18"
-select = "0.4.2"
-chrono = { version = "0.4", features = ["serde"] }
+reqwest = "0.9.24"
+select = "0.4.3"
+chrono = { version = "0.4.10", features = ["serde"] }
 parse_duration = "1.0.1"
-serde = "1.0"
-serde_derive = "1.0"
-serde_json = "1.0"
-rocket = "0.4.1"
+serde = "1.0.104"
+serde_derive = "1.0.104"
+serde_json = "1.0.44"
+rocket = "0.4.2"
 lazy_static = "*"
 url = "*"
 urldecode = "0.1.1"
+log = "0.4"
+env_logger = "*"
 
 [dependencies.rocket_contrib]
-version = "0.4.1"
+version = "0.4.2"
 default-features = false
 features = ["json", "serve"]
 

+ 15 - 3
src/main.rs

@@ -16,6 +16,8 @@ use std::io::BufReader;
 use std::io::BufWriter;
 use std::sync::Mutex;
 use std::{thread, time};
+use log::*;
+use env_logger;
 
 /// Fucking egun is a mess. It does not even use css and is built using tables. This is an attempt to parse it.
 mod query;
@@ -31,28 +33,36 @@ lazy_static! {
 
 fn daemon() {
     loop {
-        println!(">>> Running update...");
+        info!(">>> Running update...");
 
         let accounts_unlocked = ACCOUNTS.lock().unwrap();
         let accounts_cl = accounts_unlocked.clone();
         drop(accounts_unlocked);
 
+        let mut threads = vec![];
 
         for (account_name, account) in accounts_cl {
-            thread::spawn(move || {
+            let t = thread::spawn(move || {
                 let updated_account = account.updated();
                 let mut accounts = ACCOUNTS.lock().unwrap();
                 accounts.insert(account_name.to_string(), updated_account);
             });
+            threads.push(t);
         }
 
+        info!("Joining threads");
+        for t in threads {
+            t.join().unwrap();
+        }
+        info!("Done.");
+
 
         let accounts_unlocked = ACCOUNTS.lock().unwrap();
         let writer = BufWriter::new(File::create(DBFILE).unwrap());
         serde_json::to_writer_pretty(writer, &*accounts_unlocked).unwrap();
         drop(accounts_unlocked);
 
-        println!(">>> Done.");
+        info!("Cycle over, sleeping 30 secs.");
         let pause = time::Duration::from_secs(30);
         thread::sleep(pause);
 
@@ -61,6 +71,8 @@ fn daemon() {
 
 fn main() {
     
+    std::env::set_var("RUST_LOG", "info");
+    let _ = env_logger::init();
 
     match File::open(DBFILE) {
         Ok(f) => {

+ 12 - 6
src/query.rs

@@ -6,7 +6,7 @@ use select::predicate::Name;
 use std::collections::HashMap;
 // extern crate url;
 use url::form_urlencoded::{byte_serialize};
-
+use log::*;
 
 #[derive(Serialize, Deserialize, Debug, Default, Clone)]
 pub struct Auction {
@@ -140,9 +140,12 @@ fn parse_price(price: &str) -> Option<f32> {
 fn auction_details(auction: &Auction) -> Auction {
     let mut updated_auction = auction.clone();
 
+    if auction.is_price_final {
+        return updated_auction;
+    }
 
     let client = reqwest::Client::builder()
-        .timeout(Some(core::time::Duration::from_secs(8)))
+        // .timeout(Some(core::time::Duration::from_secs(10)))
         .build()
         .unwrap();
 
@@ -181,10 +184,13 @@ fn auction_details(auction: &Auction) -> Auction {
         
         }
 
-        
 
+        Err(e) => {
+            error!("Could not fetch auction details: {:?}", e);
+            // println!("Setting this price to final");
+            // updated_auction.is_price_final = true;
 
-        Err(e) => println!("{:?}", e),
+        },
     }
 
     updated_auction
@@ -201,7 +207,7 @@ fn auctions_from_url(url: &str) -> HashMap<String, Auction> {
     // dbg!(&url);
 
     let client = reqwest::Client::builder()
-        .timeout(Some(core::time::Duration::from_secs(8)))
+        // .timeout(Some(core::time::Duration::from_secs(8)))
         .build()
         .unwrap();
 
@@ -321,7 +327,7 @@ fn auctions_from_url(url: &str) -> HashMap<String, Auction> {
                 }
             }
         }
-        Err(e) => println!("{:?}", e),
+        Err(e) => error!("Retrieve auction list {:?}", e),
     }
 
     // if let Ok(mut resp) = client.get(url).send() {

+ 13 - 5
webapp/app.js

@@ -74,11 +74,19 @@ var app = new Vue({
         },
         ordered_auctions: function () {
             return Object.values(this.queries)
-            .map(q => Object.values(q.auctions) )
-            .flat()
-            .sort(function(a, b) {
-                return a.timestamp - b.timestamp;
-            });
+                .map(q => 
+                    
+                    
+                    Object.values(q.auctions).map(a => {
+                        a.avg_price = q.avg_price;
+                        return a;
+                    })
+                 )
+                .flat()
+                .sort(function(a, b) {
+                    return a.timestamp - b.timestamp;
+                })
+            ;
     
         },
         load_account: function () {

+ 3 - 0
webapp/index.html

@@ -72,6 +72,9 @@
             <div class="currency">€ {{auction.price}}</div>
             <div>{{ts_to_remaining(auction.timestamp)}}</div>
             <a v-bind:href="auction.gcal">Kalendereintrag</a>
+            <div v-if="auction.avg_price > auction.price">
+              Preistipp
+            </div>
           </div>
         </div>
         <hr>