generated from alexk111/store-of-something
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data-collector-exchange-rates.js
141 lines (116 loc) · 3.83 KB
/
data-collector-exchange-rates.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const https = require("https");
const path = require("path");
const fse = require("fs-extra");
const oxrAppId = process.env.OXR_APP_ID;
if (!oxrAppId) {
throw new Error("No OXR_APP_ID specified")
}
const pathSrc = "./src";
const pathCollected = pathSrc + "/data/_collected";
const pathCollectedUSDRates = pathCollected + "/usd-rates";
let currencies;
async function loadCurrenciesFromFile() {
const pathFile = path.join(pathCollected, "currencies.json");
const fileData = await fse.readFile(pathFile);
currencies = JSON.parse(fileData);
}
function dateToYYYYMM(d) {
let month = '' + (d.getUTCMonth() + 1);
const year = d.getUTCFullYear();
if (month.length < 2) {
month = '0' + month;
}
return [year, month].join('-');
}
function dateToYYYYMMDD(d) {
let month = '' + (d.getUTCMonth() + 1);
let date = '' + d.getUTCDate();
const year = d.getUTCFullYear();
if (month.length < 2) {
month = '0' + month;
}
if (date.length < 2) {
date = '0' + date;
}
return [year, month, date].join('-');
}
async function loadExchangeRatesFromRemoteAPI(strYYYYMMDD) {
const url = `https://openexchangerates.org/api/historical/${strYYYYMMDD}.json?app_id=${oxrAppId}`;
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", async () => {
try {
const gotData = JSON.parse(body);
if (!gotData.rates) {
throw new Error("Got empty rates");
}
resolve(gotData.rates);
} catch (error) {
reject(error.message);
}
});
})
.on("error", (error) => {
reject(error.message);
});
});
}
async function build() {
console.info("Collecting Exchange Rates data...");
await loadCurrenciesFromFile();
if (!currencies) {
throw new Error ("Collect currencies data first!");
}
let dPointer = new Date(Date.UTC(2013,3,1,0,0,0,0));
const dNow = new Date();
const dEndPointer = new Date(dNow);
dEndPointer.setUTCDate(1);
dEndPointer.setUTCHours(0,0,0,0);
if (dNow.getUTCDate()<=5) {
dEndPointer.setUTCMonth(dEndPointer.getUTCMonth()-1);
}
// First update monthly rates
let lastFinishedMonthRates;
while (dPointer.getTime()<=dEndPointer.getTime()) {
const strYYYYMM = dateToYYYYMM(dPointer);
const outFile = path.join(pathCollectedUSDRates, strYYYYMM + ".json");
if (fse.existsSync(outFile)) {
const fileData = await fse.readFile(outFile);
lastFinishedMonthRates = JSON.parse(fileData);
} else {
console.info(strYYYYMM);
lastFinishedMonthRates = {};
// some currencies might not have rates data on some days, so let's get values for a few dates and pick the earliest available
for (const strDD of ["01", "03", "05"]) {
const gotRates = await loadExchangeRatesFromRemoteAPI(`${strYYYYMM}-${strDD}`);
lastFinishedMonthRates = Object.assign(gotRates, lastFinishedMonthRates);
}
await fse.writeFile(outFile, JSON.stringify(lastFinishedMonthRates, null, 2));
}
dPointer.setUTCMonth(dPointer.getUTCMonth()+1);
}
// Then update latest rates
console.info("Latest");
const strYYYYMMDD = dateToYYYYMMDD(dNow);
const latestFile = path.join(pathCollectedUSDRates, "latest.json");
let latestRates = lastFinishedMonthRates;
if (fse.existsSync(latestFile)) {
const fileData = await fse.readFile(latestFile);
const parsedData = JSON.parse(fileData);
if (parsedData.rates) {
latestRates = parsedData.rates;
}
}
const gotRates = await loadExchangeRatesFromRemoteAPI(strYYYYMMDD);
latestRates = Object.assign(latestRates, gotRates);
await fse.writeFile(latestFile, JSON.stringify({
date: strYYYYMMDD,
rates: latestRates
}, null, 2));
}
build();