-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (52 loc) · 1.18 KB
/
index.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
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const express = require('express');
const app = express();
const port = process.env.SERVER_PORT || 3000;
const sourceUrl = process.env.SOURCE_URL || 'https://ligovka.ru';
app.use(express.json())
function asyncWrapper(fn) {
return (req, res, next) => {
return Promise.resolve(fn(req))
.then((result) => res.send(result))
.catch((err) => next(err))
}
}
const getExchangeRates = async () => {
return JSDOM.fromURL(sourceUrl).then(dom => {
let nodes = dom.window.document.querySelectorAll(".money_price")
return [...nodes].map(n => n.textContent)
});
};
const formatExchangeRate = async() => {
let result = await getExchangeRates();
return {
'usd': {
'buy': {
1: result[0],
100: result[6],
1000: result[12],
},
'sell':{
1: result[1],
100: result[7],
1000: result[13],
}
},
'eur': {
'buy' : {
1: result[2],
100: result[8],
1000: result[14],
},
'sell': {
1: result[3],
100: result[9],
1000: result[15],
}
}
}
}
app.get('/', asyncWrapper(formatExchangeRate));
console.log(`[HTTP] Listening on 0.0.0.0:${port}...`);
app.listen(port);