Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use fetch instead of puppeteer #141

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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
19 changes: 0 additions & 19 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,6 @@ const mapResponse = (array = []) => array.map((item) => ({
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,
};
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
40 changes: 23 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,28 @@ 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 query = new URLSearchParams({
period,
interval,
pointscount,
});
const url = `https://api.investing.com/api/financialdata/${pairId}/historical/chart?${query}`;

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 +68,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
Loading