-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-DolarArgentina.js
More file actions
183 lines (164 loc) · 5.62 KB
/
Copy pathMMM-DolarArgentina.js
File metadata and controls
183 lines (164 loc) · 5.62 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* MMM-DolarArgentina
* MIT License
*
* By Fabrizz <3
*/
Module.register("MMM-DolarArgentina", {
/* Magic Mirror configuration */
/** Requiered Magic Mirror version */
requiresVersion: "2.12.0",
defaults: {
/** Module name */
name: "MMM-DolarArgentina",
/** API endpoint */
url: "https://api.bluelytics.com.ar/v2/latest",
/** Time between API calls, in seconds (12 hours default) */
updateInterval: 43200,
/** Fade In/Out speed */
animationSpeed: 2500,
/** Use themes bradcasted from other modules */
useDynamicTheming: null,
/** Currency types */
types: ["oficial", "blue"],
},
/* Fetches the provided endpoint */
getDolarExchange: function () {
let t = this;
fetch(this.config.url, {
method: "GET",
referrerPolicy: "no-referrer",
})
.then((data) =>
data.json().then((payload) => t.setDolarExchange(payload)),
)
.catch((e) =>
console.error(
`[${this.name}] An error ocurred while accesing the api.`,
e,
),
);
},
/* Filters the received data and request a redraw */
setDolarExchange: function (data) {
if (!data) return;
// https://www.dolarsi.com/api/api.php?type=valoresprincipales
// La API de dolarsi dejó de actualizar valores en 2022.
// Original by: https://github.com/consus2903
// Dolarsi handler:
if (this.config.url.includes("dolarsi.com")) {
this.dolarTypes = data
.filter((apiDolarType) =>
this.config.types.some((type) =>
apiDolarType.casa.nombre.toLowerCase().includes(type),
),
)
.map((apiDolarType) => ({
purchase:
apiDolarType.casa.compra !== "No Cotiza"
? this.getNumbers(apiDolarType.casa.compra)
: ["--", "--"],
sale: this.getNumbers(apiDolarType.casa.venta),
name: apiDolarType.casa.nombre.replace("Dolar ", "").toUpperCase(),
}));
}
// https://api.bluelytics.com.ar/v2/latest
// Bluelytics handler:
if (this.config.url.includes("bluelytics.com.ar")) {
let dolarArray = [];
this.config.types.forEach((name) => {
let type = data[name];
let currency = {
name: name.replace("_", " ").toUpperCase(),
purchase: type.value_buy
? this.getNumbers(type.value_buy)
: ["--", "--"],
sale: type.value_sell
? this.getNumbers(type.value_sell)
: ["--", "--"],
};
dolarArray.push(currency);
});
this.dolarTypes = dolarArray;
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
/* Dom Generation */
getDom: function () {
let wrapper = document.createElement("div");
wrapper.classList.add("ca-wa");
if (!this.loaded) {
wrapper.innerHTML = "Loading U$D exchange rates...";
wrapper.className = "dimmed light small";
return wrapper;
}
this.dolarTypes.forEach((dolarType) => {
// Container for dolar type
let container = document.createElement("span");
container.classList.add("ca-cnt");
if (this.rtl) container.classList.add("rtl");
// Dolar type name
let designation = document.createElement("span");
designation.classList.add("ca-cnt-dg");
designation.innerHTML = dolarType.name;
container.appendChild(designation);
// Exchange rates
let exchangeContainer = document.createElement("span");
exchangeContainer.classList.add("ca-cnt-ex");
let buy = document.createElement("span");
buy.classList.add("ca-cnt-ex-txt");
buy.innerHTML = `${dolarType.purchase[0]}<span>${dolarType.purchase[1]}</span>`;
exchangeContainer.appendChild(buy);
let sell = document.createElement("span");
sell.innerHTML = `${dolarType.sale[0]}<span>${dolarType.sale[1]}</span>`;
sell.classList.add("ca-cnt-ex-txt");
exchangeContainer.appendChild(sell);
container.appendChild(exchangeContainer);
wrapper.appendChild(container);
});
return wrapper;
},
/* Startup / Schedule */
start: function () {
this.logBadge();
this.rtl = false;
this.loaded = false;
this.enabled = true;
this.getDolarExchange();
if (this.config.useDynamicTheming)
console.warn(
`[${this.name}] "useThemeFrom" is not a feature enabled yet.`,
);
if (this.data.position.includes("rigth")) this.rtl = true;
let t = this;
if (this.config.updateInterval !== 0) {
setInterval(function () {
t.getDolarExchange();
}, this.config.updateInterval * 1000);
}
},
getStyles: function () {
return ["MMM-DolarArgentina.css"];
},
/* Utils */
getNumbers: function (str) {
const strNumber =
typeof str === "string"
? (Math.round(Number(str.replace(",", ".")) * 100) / 100)
.toString()
.replace(".", ",")
: (Math.round(str * 100) / 100).toString().replace(".", ",");
let [part1, part2 = "0"] = strNumber.split(",");
part2 = part2.padEnd(2, "0");
return [part1, part2];
},
logBadge: function () {
console.log(
`\n %c by Fabrizz %c ${this.name} %c \n`,
"background-color: #555;color: #fff;padding: 3px 2px 3px 3px;border-radius: 3px 0 0 3px;font-family: DejaVu Sans,Verdana,Geneva,sans-serif;text-shadow: 0 1px 0 rgba(1, 1, 1, 0.3)",
"background-color: #bc81e0;background-image: linear-gradient(90deg, #854D0E, #EAB308);color: #fff;padding: 3px 3px 3px 2px;border-radius: 0 3px 3px 0;font-family: DejaVu Sans,Verdana,Geneva,sans-serif;text-shadow: 0 1px 0 rgba(1, 1, 1, 0.3)",
"background-color: transparent",
);
},
});