-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprice.js
75 lines (65 loc) · 1.7 KB
/
price.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";
var request = require("request");
var fs = require("fs");
const MARKETCAP = 'https://api.coinmarketcap.com/v1/ticker/';
var data = {};
function getCMCPrice(ticker) {
request(MARKETCAP + ticker, (error, response, body)=>{
try{
var dataCoin = JSON.parse(body);
} catch (e) {
console.log("Api Coinmarket Problem" + e);
return
}
var marketcapInfo = dataCoin[0];
data.priceUSD = marketcapInfo['price_usd'];
fs.writeFile("data/usdprice.txt",data.priceUSD,(err)=>{
if(err) throw err;
//console.log('File with price was updated');
});
});
}
function getBitzPrice(ticker) {
const URL = 'https://api.bit-z.com/api_v1/ticker?coin=' + ticker.toLowerCase() + '_btc';
request(URL, (error, response, body) => {
try {
var dataCoin = JSON.parse(body);
} catch (e) {
console.log("Error: Fail to get Bit-z api:" + e);
return
}
data.priceBTC = dataCoin.data['last'];
getBtcPrice(function(btcUsd) {
var usdPrice = data.priceBTC * btcUsd;
fs.writeFile("data/usdprice.txt", usdPrice, (err)=>{
if (err) {
throw err;
}
console.log('File with price was updated');
});
});
});
}
function getBtcPrice(callback) {
const URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD,JPY,EUR,KRW,EUR,JPY,GBP,BRL,CNY,AUD,CAD";
request(URL, (error, response, body) => {
try {
var json = JSON.parse(body);
} catch (e) {
console.log("Error: Fail to get BTC price from cryptocompare.com:" + e);
return
}
if (json["USD"] && callback) {
callback(json["USD"]);
}
fs.writeFile("data/btcprice.txt", JSON.stringify(json, null, 2), (err)=>{
if (err) {
throw err;
}
});
});
}
module.exports = {
getCMCPrice,
getBitzPrice
};