improvements to fetching market stuff

master
lza_menace 2 years ago
parent 3ce72cb24f
commit 18d5970a1b

1
.gitignore vendored

@ -1,3 +1,4 @@
/target /target
/vendor /vendor
*.json
*.ogg *.ogg

@ -10,27 +10,27 @@ use libtor::Error as libtorError;
pub struct App { pub struct App {
pub player: Player, pub player: Player,
pub market: Market,
pub tor_required: bool, pub tor_required: bool,
pub tor_started: bool, pub tor_started: bool,
pub tor_connected: bool, pub tor_connected: bool,
pub to_data: String, pub to_data: String,
pub show_irc: bool, pub show_irc: bool,
pub irc_message: String, pub irc_message: String,
pub show_market_data: bool, pub show_market_data: bool
pub market: Market
} }
impl Default for App { impl Default for App {
fn default() -> Self { fn default() -> Self {
Self { Self {
player: Player::default(),
market: Market::new(),
tor_started: false, tor_started: false,
tor_required: true, tor_required: true,
tor_connected: false, tor_connected: false,
show_market_data: false, show_market_data: false,
show_irc: false, show_irc: false,
to_data: "".to_owned(), to_data: "".to_owned(),
player: Player::default(),
market: Market::new(),
irc_message: "".to_owned() 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) { if self.market.last_check_time.elapsed().unwrap() > Duration::from_secs(120) {
println!("[+] refreshing WOW market data."); println!("[+] Refreshing WOW market data.");
self.market.last_cg_data = self.market.get_wow_market(self.tor_required); self.market.store_market_data(self.tor_required);
self.market.last_check_time = SystemTime::now(); 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 m = &self.market.last_cg_data;
let md = &m["market_data"]; let md = &m["market_data"];
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {

@ -1,14 +1,13 @@
extern crate json; extern crate json;
extern crate reqwest; extern crate reqwest;
use std::time::{SystemTime, Duration}; use std::{time::{SystemTime, Duration}, io::Write};
use json::JsonValue; use json::{JsonValue, object};
use reqwest::{blocking::ClientBuilder, Proxy};
pub struct Market { pub struct Market {
pub last_check_time: SystemTime, pub last_check_time: SystemTime,
pub last_cg_data: JsonValue, pub last_cg_data: JsonValue,
pub denomination: String pub denomination: String,
} }
impl Market { impl Market {
@ -20,19 +19,27 @@ impl Market {
} }
} }
fn get_client(&self, tor_required: bool) -> ClientBuilder { pub fn read_json_from_file(&self) -> JsonValue {
let mut client_builder = ClientBuilder::new(); let f = std::fs::read_to_string("market.json");
if tor_required { if f.is_ok() {
let proxy = Proxy::all("socks5://127.0.0.1:19050").unwrap(); let j = json::parse(&f.unwrap());
client_builder = client_builder.proxy(proxy); 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 url = "https://api.coingecko.com/api/v3/coins/wownero?community_data=false&developer_data=false";
let client = self.get_client(tor_required); let t: Result<std::thread::JoinHandle<()>, std::io::Error> = std::thread::Builder::new().name("market_data".to_string()).spawn(move || {
let res: String = client 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() .build()
.unwrap() .unwrap()
.get(url) .get(url)
@ -40,12 +47,11 @@ impl Market {
.unwrap() .unwrap()
.text() .text()
.unwrap(); .unwrap();
let j = json::parse(res.as_str()).expect("failed to parse json from cg");
let j = json::parse(res.as_str()); let mut f = std::fs::File::create("market.json").expect("failed to create market.json");
if j.is_ok() { let _ = f.write_all(j.dump().as_bytes()).expect("failed to write to market.json");
return j.unwrap(); ()
} else { });
return json::object!{}; return t.unwrap()
}
} }
} }