浏览代码

work towards saving past auctions

Johann Woelper 7 年之前
父节点
当前提交
3193a3e9ee
共有 2 个文件被更改,包括 28 次插入8 次删除
  1. 25 6
      src/main.rs
  2. 3 2
      urls.json

+ 25 - 6
src/main.rs

@@ -11,6 +11,7 @@ use std::io::BufWriter;
 use std::io::BufReader;
 use std::fs::File;
 use std::{thread, time};
+use std::collections::HashMap;
 
 /// Fucking egun is a mess. It does not even use css and is built using tables. This is an attempt to parse it.
 
@@ -106,11 +107,13 @@ fn parse_url(url: &str) -> Vec<Auction> {
     let mut auctions = vec![];
 
     if let Ok(mut resp) = reqwest::get(url) {
+
         if !resp.status().is_success() {
+            println!("ERR {:?}", resp.text());
             return auctions;
         }
 
-    let text = resp.text().unwrap_or("".to_string());
+        let text = resp.text().unwrap_or("".to_string());
 
 
 
@@ -219,19 +222,29 @@ fn parse_url(url: &str) -> Vec<Auction> {
 
 
 
-fn daemon(queries: Vec<Query>) {
+fn daemon(queries: Vec<Query>, loaded_auctions: Vec<Auction>) {
     
+    dbg!(&queries);
+    // let _s = loaded_auctions.into_iter().map(|x| (x.url.clone(), x.clone())).collect::<Vec<_>>();
+    let mut auction_map: HashMap<String, Auction> = loaded_auctions.into_iter().map(|x| (x.url.clone(), x.clone())).collect();
     
     println!("Starting daemon with {} active queries", queries.len());
     loop {
-        let mut auctions = vec![];
+        // TODO: see if we can get rid of clone
+        let auction_map = auction_map.clone();
+        // let mut auctions = vec![];
         for mut query in queries.clone() {
             query.run();
-            auctions.extend(query.auctions);
+            // auctions.extend(query.auctions);
+            for auction in query.auctions {
+                // auction_map.insert(auction.url, auction.clone());
+
+            }
         }
-        println!("{} auctions found", auctions.len());
+        println!("{} auctions found", &auction_map.len());
         let writer = BufWriter::new(File::create("db.json").unwrap());
 
+        let mut auctions: Vec<Auction> = auction_map.into_iter().map(|(x,y)| y).collect();
         auctions.sort_by_key(|k| k.remaining);
 
         serde_json::to_writer_pretty(writer, &auctions).unwrap();
@@ -248,13 +261,19 @@ fn main() {
     
 
     let reader = BufReader::new(File::open("urls.json").unwrap());
+    // let reader = BufReader::new(File::open("url_local.json").unwrap());
     let urls: Vec<String> = serde_json::from_reader(reader).unwrap_or(vec![]);
 
 
+    let reader = BufReader::new(File::open("db.json").unwrap());
+    let auctions: Vec<Auction> = serde_json::from_reader(reader).unwrap_or(vec![]);
+
+
     daemon(
         urls.iter()
         .map(|x| Query {url: x.to_string(), ..Default::default()})
-        .collect::<Vec<Query>>()
+        .collect::<Vec<Query>>(),
+        auctions
         );
 
 }

+ 3 - 2
urls.json

@@ -4,7 +4,8 @@
     "http://www.egun.de/market/list_items.php?mode=qry&plusdescr=off&wheremode=and&query=shr+970&quick=1",
     "http://www.egun.de/market/list_items.php?mode=qry&query=smith+wesson+r8&plusdescr=off&wheremode=andewb=1&status=&minprice=&maxprice=&type=0&nick=&country_id=&zip=&int=0&order=ends&asdes=asc",
     "http://www.egun.de/market/list_items.php?mode=qry&query=m305&plusdescr=off&wheremode=and&ewb=1&status=&minprice=&maxprice=&type=0&nick=&country_id=&zip=&int=0&order=ends&asdes=asc",
-    "http://www.egun.de/market/list_items.php?mode=qry&plusdescr=off&wheremode=and&maxprice=2000&query=m14+luxdef&quick=1",
+    "http://www.egun.de/market/list_items.php?mode=qry&plusdescr=off&wheremode=and&maxprice=2200&query=m14+luxdef&quick=1",
     "http://egun.de/market/list_items.php?mode=qry&query=accuracy&plusdescr=off&wheremode=and&ewb=&status=&minprice=300&maxprice=2100&type=0&nick=&country_id=&zip=&int=0&order=ends&asdes=asc",
-    "http://egun.de/market/list_items.php?mode=qry&plusdescr=off&wheremode=and&query=harris+1A2-L&quick=1"
+    "http://egun.de/market/list_items.php?mode=qry&plusdescr=off&wheremode=and&query=harris+1A2-L&quick=1",
+    "http://www.egun.de/market/list_items.php?mode=qry&query=308+enfield&plusdescr=off&wheremode=and&ewb=1&status=&minprice=&maxprice=&type=0&nick=&country_id=&zip=&int=0&order=ends&asdes=asc"
 ]