Merge pull request #22 from whitequark/master

The author of BTC-e was arrested and this broke our exchange rate function
This commit is contained in:
Tiffany Bennett 2017-07-26 21:38:57 -07:00 committed by GitHub
commit 1514a37196

View file

@ -9,33 +9,27 @@ use std::io::Read;
use std::rc::Rc;
use json;
static URL: &'static str = "https://btc-e.com/api/3/ticker/btc_usd-ltc_usd-nmc_usd-nvc_usd-ppc_usd-eth_usd";
static URL: &'static str = "https://blockchain.info/stats?format=json";
pub fn parse(mut f: File) -> Result<Defs, String> {
let mut buf = String::new();
try!(f.read_to_string(&mut buf).map_err(|x| format!("{}", x)));
let parsed = try!(json::parse(&*buf).map_err(|x| format!("{}", x)));
let mut out = vec![];
for (k, v) in parsed.entries() {
if k.ends_with("_usd") {
let name = k[0..k.len()-"_usd".len()].to_uppercase();
let avg = v["avg"].as_number();
if let Some(avg) = avg {
let (sign, mantissa, exp) = avg.as_parts();
let integer = format!("{}{}", if sign { "" } else { "-" }, mantissa);
if let Ok(num) = ::Number::from_parts(&*integer, None, Some(&*format!("{}", exp))) {
out.push(DefEntry {
name: name,
def: Rc::new(Def::Unit(
Expr::Mul(vec![
Expr::Const(num),
Expr::Unit("USD".to_owned())
]))),
doc: Some(format!("Sourced from BTC-E exchange.")),
category: Some("currencies".to_owned()),
});
}
}
if let Some(price) = parsed["market_price_usd"].as_number() {
let (sign, mantissa, exp) = price.as_parts();
let integer = format!("{}{}", if sign { "" } else { "-" }, mantissa);
if let Ok(price) = ::Number::from_parts(&*integer, None, Some(&*format!("{}", exp))) {
out.push(DefEntry {
name: "BTC".to_owned(),
def: Rc::new(Def::Unit(
Expr::Mul(vec![
Expr::Const(price),
Expr::Unit("USD".to_owned())
]))),
doc: Some(format!("Sourced from blockchain.info.")),
category: Some("currencies".to_owned()),
});
}
}
Ok(Defs {