From 18d5970a1b4ecc68440ac2865208973856e81176 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Wed, 26 Oct 2022 12:06:24 -0700 Subject: [PATCH] improvements to fetching market stuff --- .gitignore | 1 + src/app.rs | 19 +++++++++++------ src/stats.rs | 60 +++++++++++++++++++++++++++++----------------------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 9ad6cd5..6e2af26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /vendor +*.json *.ogg diff --git a/src/app.rs b/src/app.rs index 9918f5e..64eb860 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,27 +10,27 @@ use libtor::Error as libtorError; pub struct App { pub player: Player, + pub market: Market, pub tor_required: bool, pub tor_started: bool, pub tor_connected: bool, pub to_data: String, pub show_irc: bool, pub irc_message: String, - pub show_market_data: bool, - pub market: Market + pub show_market_data: bool } impl Default for App { fn default() -> Self { Self { + player: Player::default(), + market: Market::new(), tor_started: false, tor_required: true, tor_connected: false, show_market_data: false, show_irc: false, to_data: "".to_owned(), - player: Player::default(), - market: Market::new(), irc_message: "".to_owned() } } @@ -101,10 +101,17 @@ impl eframe::App for App { } ); if self.market.last_check_time.elapsed().unwrap() > Duration::from_secs(120) { - println!("[+] refreshing WOW market data."); - self.market.last_cg_data = self.market.get_wow_market(self.tor_required); + println!("[+] Refreshing WOW market data."); + self.market.store_market_data(self.tor_required); self.market.last_check_time = SystemTime::now(); } + if self.market.last_check_time.elapsed().unwrap() < Duration::from_secs(30) && self.market.read_json_from_file().len() == 0 { + ui.horizontal(|ui| { + ui.spinner(); + ui.label("Fetching market data..."); + }); + } + self.market.last_cg_data = self.market.read_json_from_file(); let m = &self.market.last_cg_data; let md = &m["market_data"]; ui.horizontal_wrapped(|ui| { diff --git a/src/stats.rs b/src/stats.rs index 38e1565..53e3ad5 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -1,14 +1,13 @@ extern crate json; extern crate reqwest; -use std::time::{SystemTime, Duration}; -use json::JsonValue; -use reqwest::{blocking::ClientBuilder, Proxy}; +use std::{time::{SystemTime, Duration}, io::Write}; +use json::{JsonValue, object}; pub struct Market { pub last_check_time: SystemTime, pub last_cg_data: JsonValue, - pub denomination: String + pub denomination: String, } impl Market { @@ -20,32 +19,39 @@ impl Market { } } - fn get_client(&self, tor_required: bool) -> ClientBuilder { - let mut client_builder = ClientBuilder::new(); - if tor_required { - let proxy = Proxy::all("socks5://127.0.0.1:19050").unwrap(); - client_builder = client_builder.proxy(proxy); + pub fn read_json_from_file(&self) -> JsonValue { + let f = std::fs::read_to_string("market.json"); + if f.is_ok() { + let j = json::parse(&f.unwrap()); + if j.is_ok() { + return j.unwrap(); + } } - return client_builder; + return object!{} } - pub fn get_wow_market(&self, tor_required: bool) -> JsonValue { + pub fn store_market_data(&self, tor_required: bool) -> std::thread::JoinHandle<()> { let url = "https://api.coingecko.com/api/v3/coins/wownero?community_data=false&developer_data=false"; - let client = self.get_client(tor_required); - let res: String = client - .build() - .unwrap() - .get(url) - .send() - .unwrap() - .text() - .unwrap(); - - let j = json::parse(res.as_str()); - if j.is_ok() { - return j.unwrap(); - } else { - return json::object!{}; - } + let t: Result, std::io::Error> = std::thread::Builder::new().name("market_data".to_string()).spawn(move || { + let mut client_builder = reqwest::blocking::Client::builder() + .user_agent("WOC GUI + BoomBox"); + if tor_required { + let proxy = reqwest::Proxy::all("socks5://127.0.0.1:19050").unwrap(); + client_builder = client_builder.proxy(proxy); + } + let res = client_builder + .build() + .unwrap() + .get(url) + .send() + .unwrap() + .text() + .unwrap(); + let j = json::parse(res.as_str()).expect("failed to parse json from cg"); + let mut f = std::fs::File::create("market.json").expect("failed to create market.json"); + let _ = f.write_all(j.dump().as_bytes()).expect("failed to write to market.json"); + () + }); + return t.unwrap() } }