forked from DavideViolante/investing-com-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
37 lines (35 loc) · 1.17 KB
/
functions.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
/**
* Map the Investing array response
* @param {Array} array Array of data returned from Investing website
* @return {Array} An array of objects with date and value properties
*/
const mapResponse = (array = []) => array.map((item) => ({
date: item[0],
price_open: item[1],
price_high: item[2],
price_low: item[3],
price_close: item[4],
value: item[4],
volume: item[5],
}));
/**
* Get JSON response from Investing APIs
* @param {*} page puppeteer page
* @return {Object} JSON response from Investing, with data property containing an array of arrays
*/
async function getJsonContent(page) {
// If there is this element, the page cannot be loaded due to CloudFlare protection
// Element: <body class="no-js">
// eslint-disable-next-line no-undef
const bodyClass = await page.evaluate(() => document.querySelector('body').getAttribute('class'));
if (bodyClass === 'no-js') {
throw new Error(`Error: couldn't bypass CloudFlare protection`);
}
// eslint-disable-next-line no-undef
const content = await page.evaluate(() => document.querySelector('body').textContent);
return JSON.parse(content);
}
module.exports = {
mapResponse,
getJsonContent,
};