-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCoins.js
87 lines (77 loc) · 2.06 KB
/
getCoins.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
76
77
78
79
80
81
82
83
84
85
86
87
const https = require("https");
const fs = require("fs");
const priceUrl =
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=200&convert=USD";
const shapeShiftUrl = "https://shapeshift.io/getcoins";
const currencyURL = "https://api.fixer.io/latest?base=USD";
const getPrices = () => {
const options = {
host: "pro-api.coinmarketcap.com",
path: "/v1/cryptocurrency/listings/latest",
headers: {
"X-CMC_PRO_API_KEY": process.env.COIN_MARKET_CAP_API_KEY
}
};
return new Promise((resolve, reject) => {
https.get(options, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
if (res.statusCode < 400) {
resolve(JSON.parse(body));
} else {
console.error(res.statusCode, body);
reject();
}
});
res.on("error", () => {
console.error("Error fetching from");
reject();
});
});
});
};
const getData = (url, cb) => {
https.get(url, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
if (res.statusCode < 400) {
cb(JSON.parse(body));
} else {
console.error(res.statusCode, url);
cb();
}
});
res.on("error", () => {
console.error("Error fetching from", url);
cb();
});
});
};
const getPriceUSD = price => price.quote && price.quote.USD;
getPrices().then(({ data }) => {
getData(shapeShiftUrl, shapeShiftData => {
const output = data
.map(price => {
if (shapeShiftData[price.symbol]) {
return Object.assign({}, price, {
...price,
...getPriceUSD(price),
image: shapeShiftData[price.symbol].image
});
}
return null;
})
.filter(price => price);
console.log("Coin data fetched");
fs.writeFileSync("public/prices.json", JSON.stringify(output));
console.log("Coin data saved");
});
});