Johann Woelper 7 年 前
コミット
a13c5995b1
1 ファイル変更28 行追加16 行削除
  1. 28 16
      src/main.rs

+ 28 - 16
src/main.rs

@@ -9,6 +9,7 @@ use serde_json;
 use std::io::BufWriter;
 use std::io::BufReader;
 use std::fs::File;
+use std::{thread, time};
 
 /// Fucking egun is a mess. It does not even use css and is built using tables. This is an attempt to parse it.
 
@@ -26,7 +27,7 @@ struct Auction {
     is_price_final: bool
 }
 
-#[derive(Serialize, Deserialize, Debug)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
 struct Query {
     url: String,
     auctions: Vec<Auction>,
@@ -217,28 +218,39 @@ fn parse_url(url: &str) -> Vec<Auction> {
 
 
 
+fn daemon(queries: Vec<Query>) {
+    
+    
+    println!("Starting daemon");
+    loop {
+        let mut auctions = vec![];
+        for mut query in queries.clone() {
+            query.run();
+            auctions.extend(query.auctions);
+        }
+        println!("{} auctions found", auctions.len());
+        let writer = BufWriter::new(File::create("db.json").unwrap());
+        serde_json::to_writer_pretty(writer, &auctions).unwrap();
+        let pause = time::Duration::from_secs(300);
+        thread::sleep(pause);
+    }
+
+
+}
+
 
 fn main() {
 
     
-    let mut auctions = vec![];
 
     let reader = BufReader::new(File::open("urls.json").unwrap());
-    let urls: Vec<String> = serde_json::from_reader(reader).unwrap();
+    let urls: Vec<String> = serde_json::from_reader(reader).unwrap_or(vec![]);
 
 
-    for url in urls {
-
-        let mut query = Query {
-            url: url.to_string(),
-            ..Default::default()
-        };
-        query.run();
-
-        auctions.extend(query.auctions);
-    }
+    daemon(
+        urls.iter()
+        .map(|x| Query {url: x.to_string(), ..Default::default()})
+        .collect::<Vec<Query>>()
+        );
 
-    // write out the file
-    let writer = BufWriter::new(File::create("db.json").unwrap());
-    serde_json::to_writer_pretty(writer, &auctions).unwrap();
 }