You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.0 KiB
58 lines
2.0 KiB
extern crate json;
|
|
extern crate reqwest;
|
|
|
|
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,
|
|
}
|
|
|
|
impl Market {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
last_check_time: SystemTime::now() - Duration::from_secs(600),
|
|
last_cg_data: json::object!{},
|
|
denomination: "sats".to_owned()
|
|
}
|
|
}
|
|
|
|
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 object!{}
|
|
}
|
|
|
|
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 t: Result<std::thread::JoinHandle<()>, 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()
|
|
}
|
|
}
|