Skip to content

Commit

Permalink
use fetch instead of puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
liardoecp committed Oct 18, 2024
1 parent b90f520 commit 73b4182
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 207 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Only input is required, other params are optional.
- **period** _String_: Period of time, window size. Default P1M (1 month). Valid values: P1D, P1W, P1M, P3M, P6M, P1Y, P5Y, MAX.
- **interval** _Number_: Interval between results. Default P1D (1 day). Valid values: PT1M, PT5M, PT15M, PT30M, PT1H, PT5H, P1D, P1W, P1M.
- **pointscount** _Number_: number of total results. Valid values seems to be 60, 70 or 120.
- **pptrLaunchOptions** _Any_: Puppeteer launch options, see [official website](https://pptr.dev/api/puppeteer.launchoptions).

### Run tests
`npm test`
Expand Down
21 changes: 1 addition & 20 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ function mapResponse(array = []) {
}));
}

/**
* 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,
mapResponse
};
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ declare module "investing-com-api" {
period?: 'P1D' | 'P1W' | 'P1M' | 'P3M' | 'P6M' | 'P1Y' | 'P5Y' | 'MAX',
interval?: 'PT1M' | 'PT5M' | 'PT15M' | 'PT30M' | 'PT1H' | 'PT5H' | 'P1D' | 'P1W' | 'P1M',
pointscount?: 60 | 70 | 120,
pptrLaunchOptions?: any,
): Promise<{
date: number,
value: number,
Expand Down
35 changes: 18 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const puppeteer = require('puppeteer');
const { mapping } = require('./mapping');
const { getJsonContent, mapResponse } = require('./functions');
const { mapResponse } = require('./functions');

const validPeriod = ['P1D', 'P1W', 'P1M', 'P3M', 'P6M', 'P1Y', 'P5Y', 'MAX'];
const validInterval = ['PT1M', 'PT5M', 'PT15M', 'PT30M', 'PT1H', 'PT5H', 'P1D', 'P1W', 'P1M'];
Expand Down Expand Up @@ -36,19 +35,23 @@ function checkParams(input, period, interval, pointscount) {
* @param {string} interval Interval between results.
* Valid values: PT1M, PT5M, PT15M, PT30M, PT1H, PT5H, P1D, P1W, P1M
* @param {number} pointscount Number of results returned. Valid values: 60, 70, 120
* @param {*} pptrLaunchOptions Puppeteer launch options, see https://pptr.dev/api/puppeteer.launchoptions
* @return {Promise<Array>} An array of arrays with date (timestamp) and values (number) properties
*/
async function callInvesting(pairId, period, interval, pointscount, pptrLaunchOptions) {
const browser = await puppeteer.launch(pptrLaunchOptions);
const page = await browser.newPage();
// eslint-disable-next-line max-len
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36');
// eslint-disable-next-line max-len
await page.goto(`https://api.investing.com/api/financialdata/${pairId}/historical/chart?period=${period}&interval=${interval}&pointscount=${pointscount}`);
const jsonContent = await getJsonContent(page);
await browser.close();
return jsonContent.data;
async function callInvesting(pairId, period, interval, pointscount) {
const url = `https://api.investing.com/api/financialdata/${pairId}/historical/chart?period=${period}&interval=${interval}&pointscount=${pointscount}`

const response = await fetch(url, {
headers: new Headers({
"upgrade-insecure-requests": "1"
})
});

if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}

const json = await response.json();
return json.data
}

/**
Expand All @@ -60,21 +63,19 @@ async function callInvesting(pairId, period, interval, pointscount, pptrLaunchOp
* Valid values: PT1M, PT5M, PT15M, PT30M, PT1H, PT5H, P1D, P1W, P1M
* @param {number} [pointscount] Number of results returned, but depends on period and interval too.
* Valid values: 60, 70, 120
* @param {*} [pptrLaunchOptions] Puppeteer launch options, see https://pptr.dev/api/puppeteer.launchoptions
* @return {Promise<Array>} An array of objects with date (timestamp), value (number) and other (number) properties
*/
async function investing(input, period = 'P1M', interval = 'P1D', pointscount = 120, pptrLaunchOptions) {
async function investing(input, period = 'P1M', interval = 'P1D', pointscount = 120) {
try {
checkParams(input, period, interval, pointscount);
const pairId = mapping[input]?.pairId || input;
const resInvesting = await callInvesting(pairId, period, interval, pointscount, pptrLaunchOptions);
const resInvesting = await callInvesting(pairId, period, interval, pointscount);
const results = mapResponse(resInvesting);
if (!results.length) {
throw Error('Wrong input or pairId');
}
return results;
} catch (err) {
console.error(err.message);
if (err.response?.data?.['@errors']?.[0]) {
console.error(err.response.data['@errors'][0]);
}
Expand Down
Loading

0 comments on commit 73b4182

Please sign in to comment.