-
Notifications
You must be signed in to change notification settings - Fork 1
/
money-converter.js
55 lines (48 loc) · 2.12 KB
/
money-converter.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
const axios = require('axios');
const htmlParser = require('node-html-parser');
const https = require('https');
const moneyConverterUrl = 'https://themoneyconverter.com/'; // moneda_origen / moneda_destino
const currencyConversionCache = new Map();
function retrieveCurrencyConversion(origCurr, targetCurr) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const agent = new https.Agent({
rejectUnauthorized: false
});
return axios({
method: 'get',
url: moneyConverterUrl + `/${origCurr}/${targetCurr}`,
headers: {
'accept-language': 'en-US,en;q=0.9,es-US;q=0.8,es;q=0.7',
dnt: 1,
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'cross-site',
'upgrade-insecure-requests': 1,
Host: 'themoneyconverter.com',
Connection: 'keep-alive',
Pragma: 'no-cache',
'Cache-Control': 'no-cache',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'Sec-Fetch-User': '?1',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
httpsAgent: agent
}
}).then(
response => {
const html = htmlParser.parse(response.data);
const values = html.querySelectorAll('#currency-converter div.cc-result output');
const result = values[0].childNodes[0].rawText;
const tst = html.querySelectorAll('#currency-converter div.cc-controls time');
let tstResult = tst[0].childNodes[0].rawText;
tstResult = tstResult.replace(/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}:\d{2}:\d{2})/g, '$3-$1-$2T$4.000Z');
return [...result.split(/\s/).filter(r => r !== '='), ...[tstResult]];
}
).catch(err => console.error(err));
}
module.exports = {
currencyConversionCache,
retrieveCurrencyConversion
};
// retrieveCurrencyConversion('USD', 'PEN').then(
// result => console.log(result)
// );