-
Notifications
You must be signed in to change notification settings - Fork 16
/
fetch-eu-currencies.js
executable file
·115 lines (100 loc) · 3.09 KB
/
fetch-eu-currencies.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
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const convert = require("xml-js");
const request = require("axios");
const { format } = require("date-fns");
const { skewDays, loadYaml } = require("./misc/util");
const UniCache = require("./misc/unicache");
const config = loadYaml('./config.yaml');
const savePath = config.currencyFilePath || "./data/currencies";
const debug = config.DEBUG;
const cacheType = config.cacheType || "file";
const namePrefix = "currencies-";
const url =
config.currencyUrl ||
"https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
console.log(url);
const keepDays = config.keepDays || 7;
const runNodeSchedule = config.runNodeSchedule || true;
// Currency rates are available around 16:00 hours
const scheduleHours = config.scheduleHours;
const scheduleMinutes = config.scheduleEuMinutes;
let schedule;
let runSchedule;
if (runNodeSchedule) {
schedule = require("node-schedule");
runSchedule = new schedule.RecurrenceRule();
runSchedule.hour = scheduleHours;
runSchedule.minute = scheduleMinutes;
}
const DB_PREFIX = namePrefix;
const DB_OPTIONS = {
cacheType: cacheType,
syncOnWrite: true,
//syncInterval: 600,
savePath: savePath,
};
const cacheName = `${DB_PREFIX}latest`;
const currencyDb = new UniCache(cacheName, DB_OPTIONS);
const options = {
headers: {
accept: "application/xml",
"Content-Type": "text/xml",
},
method: "GET",
};
function getEuroRates(cur) {
const obj = {};
for (let i = 0; i < cur.length; i++) {
obj[cur[i]._attributes.currency] = cur[i]._attributes.rate * 1;
}
obj["EUR"] = 1;
return obj;
}
async function getCurrencies() {
retireDays(keepDays);
request(url, options)
.then(function (body) {
const result = convert.xml2js(body.data, { compact: true, spaces: 4 });
const root = result["gesmes:Envelope"].Cube.Cube;
const obj = {
status: "OK",
date: root._attributes.time,
base: "EUR",
rates: getEuroRates(root.Cube),
};
//currencyDb.createObject(`${DB_PREFIX}latest`, obj);
currencyDb.init(obj);
console.log("Currencies stored as", `${DB_PREFIX}latest`);
currencyDb.createObject(`${DB_PREFIX}${obj.date}`, obj);
console.log("Currencies stored as", `${DB_PREFIX}${obj.date}`);
if (debug) {
console.log(JSON.stringify(obj, null, 2));
}
})
.catch(function (err) {
if (err.response) {
console.log("Error:", err.response.status, err.response.statusText);
console.log("Headers:", err.response.headers);
}
});
}
async function retireDays(offset) {
// Count offset days backwards
offset *= -1;
const retireDate = skewDays(offset);
const keys = await currencyDb.dbKeys(`${DB_PREFIX}${retireDate}'*'`);
console.log("Retiring", keys);
keys.forEach(async (key) => {
if (key <= `${DB_PREFIX}${retireDate}`) {
await currencyDb.deleteObject(key);
}
});
}
if (runNodeSchedule) {
console.log("Fetch currency rates scheduling started..");
schedule.scheduleJob(runSchedule, getCurrencies);
}
//currencyDb = new UniCache(null, DB_OPTIONS);
getCurrencies();