-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.html
66 lines (65 loc) · 2.78 KB
/
container.html
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
<script>
"use strict";
const getJSONfxData = async (dateStart = '2018-01-01', dateEnd = '2021-02-24', symbols = ['USD', 'MXN']) => {
(function (console) {
console.save = function (data, filename) {
if (!data) {
console.error('Console.save: No data');
return;
}
if (!filename)
filename = 'console.json';
if (typeof data === "object") {
data = JSON.stringify(data, undefined, 4);
}
var blob = new Blob([data], { type: 'text/json' }), e = document.createEvent('MouseEvents'), a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
};
console.log("file saved");
})(console);
const fetchData = async () => {
let targetURL = `https://api.exchangeratesapi.io/history?start_at=${dateStart}&end_at=${dateEnd}&symbols=USD,MXN`;
console.log(targetURL);
const data = await fetch(targetURL).then(response => response.json());
return data;
};
const data = await fetchData();
const base = data["base"];
const tableName = 'rates';
// console.log(Object.entries(data[tableName]));
const dateData = Object.entries(data[tableName]);
const dataSetData = dateData.map(dateEntryArray => {
let tableRow = { date: dateEntryArray[0], base };
let fxDataRow = dateEntryArray[1];
for (let key in fxDataRow) {
tableRow[key] = fxDataRow[key];
}
return tableRow;
});
const dataSet = { [tableName]: dataSetData };
let filename = 'Fx_rates.json';
console.log(dataSet);
console.save(dataSet, filename);
};
function generateJSONfile() {
let dateStart = document.getElementById('start')?.value || '2018-01-01';
let dateEnd = document.getElementById('end')?.value || new Date().toISOString().split('T')[0];
let symbols = ['USD', 'MXN'];
getJSONfxData(dateStart, dateEnd, symbols);
}
(() => (window.generateJSONfile = generateJSONfile))();
// window.generateJSONfile();
</script>
<div style="margin:20px; min-width: 80%; min-height: 80%;">
<label for="button">Get FX Values for selected dates</label><br>
<input type="date" onchange="console.log(typeof this.value)" id="start"/>
<input type="date" onchange="console.log(typeof this.value)" id="end"/>
<br>
<br>
<br>
<input style="margin-left: 20px; color:white;background-color:rgb(50, 53, 233)" type="button" id="getData" onclick="generateJSONfile()" id="start" value="Submit"/>
</div>