Skip to content

Commit

Permalink
Change from eslint standard to google
Browse files Browse the repository at this point in the history
update deps
add more tests
improve readme
  • Loading branch information
DavideViolante committed Oct 23, 2021
1 parent a2f0fa2 commit a2a494f
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 4,994 deletions.
27 changes: 14 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"env": {
"commonjs": true,
"es2021": true,
"mocha": true,
"node": true
"commonjs": true,
"es2021": true,
"node": true,
"mocha": true
},
"extends": [
"standard"
],
"extends": "google",
"parserOptions": {
"ecmaVersion": 12
"ecmaVersion": 12
},
"rules": {
"quote-props": [
"error",
"consistent-as-needed"
]
"max-len": [
"error", 100
],
"object-curly-spacing": [
"error", "always"
],
"space-infix-ops": "error"
}
}
}
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Investing.com unofficial APIs
[![](https://github.com/davideviolante/investing-com-api/workflows/Node.js%20CI/badge.svg)](https://github.com/DavideViolante/investing-com-api/actions?query=workflow%3A"Node.js+CI") [![Coverage Status](https://coveralls.io/repos/github/DavideViolante/investing-com-api/badge.svg?branch=master)](https://coveralls.io/github/DavideViolante/investing-com-api?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/ce48adbd97ff85557918/maintainability)](https://codeclimate.com/github/DavideViolante/investing-com-api/maintainability) [![Donate](https://img.shields.io/badge/paypal-donate-179BD7.svg)](https://www.paypal.me/dviolante)
# Investing.com Unofficial APIs
[![](https://github.com/davideviolante/investing-com-api/workflows/Node.js%20CI/badge.svg)](https://github.com/DavideViolante/investing-com-api/actions?query=workflow%3A"Node.js+CI") [![Coverage Status](https://coveralls.io/repos/github/DavideViolante/investing-com-api/badge.svg?branch=master)](https://coveralls.io/github/DavideViolante/investing-com-api?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/ce48adbd97ff85557918/maintainability)](https://codeclimate.com/github/DavideViolante/investing-com-api/maintainability) ![npm](https://img.shields.io/npm/dm/investing-com-api) [![Donate](https://img.shields.io/badge/paypal-donate-179BD7.svg)](https://www.paypal.me/dviolante)

[![NPM](https://nodei.co/npm/investing-com-api.png)](https://nodei.co/npm/investing-com-api/)

Unofficial APIs for Investing.com website.

Expand All @@ -8,14 +10,14 @@ Unofficial APIs for Investing.com website.

### Example
```js
const { investing } = require('investing-com-api')
const { investing } = require('investing-com-api');

async function main () {
async function main() {
try {
const response1 = await investing('currencies/eur-usd')
const response2 = await investing('currencies/eur-usd', 3600, 24, '1-day') // With optional params
const response1 = await investing('currencies/eur-usd');
const response2 = await investing('currencies/eur-usd', 3600, 24, '1-day'); // With optional params
} catch (err) {
console.error(err)
console.error(err);
}
}
```
Expand All @@ -33,16 +35,17 @@ async function main () {


### Available inputs
- input (String) required, see [mapping.js](https://github.com/DavideViolante/investing-com-api/blob/master/mapping.js)
- interval (Number in seconds), data interval
- candleCount (Number) Max number of results
- period (String) time window: n-day, n-month or n-year where n is a number
Only input is required, other params are optional.
- **input** _String_: input string, see [mapping.js](https://github.com/DavideViolante/investing-com-api/blob/master/mapping.js) keys.
- **interval** _Number_: interval between results in seconds, only some values are accepted such as 900, 1800, 3600, 18000, etc.
- **candleCount** _Number_: number of total results, seems it must be >10, it depends on interval and period too.
- **period** _String_: n-hour, n-day, n-month or n-year where n is a number.

### Run tests
- `npm test`
`npm test`

### Run lint
- `npm run lint`
`npm run lint`

### Author
- [Davide Violante](https://github.com/DavideViolante/)
Expand Down
17 changes: 11 additions & 6 deletions functions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
function mapResponse (array) {
return array.map(item => ({
/**
* 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
*/
function mapResponse(array) {
return array.map((item) => ({
date: item[0],
value: item[1]
}))
value: item[1],
}));
}

module.exports = {
mapResponse
}
mapResponse,
};
58 changes: 39 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const axios = require('axios')
const { mapping } = require('./mapping')
const { mapResponse } = require('./functions')
const axios = require('axios');
const { mapping } = require('./mapping');
const { mapResponse } = require('./functions');

function callInvesting (pairId, interval, candleCount, period) {
/**
* Call Investing.com APIs
* @param {string} pairId The ID of the resource to get from Investing.com
* @param {number} interval Interval between results in seconds,
* only some values are accepted such as 900, 1800, 3600, 18000, etc
* @param {number} candleCount Number of total results, seems it must be >10,
* it depends on interval and period too
* @param {string} period n-hour, n-day, n-month or n-year string where n is a number
* @return {Object} Object with the following properties: _comment, candles, events, attr, html
*/
function callInvesting(pairId, interval, candleCount, period) {
return axios({
method: 'GET',
url: 'https://www.investing.com/common/modules/js_instrument_chart/api/data.php',
Expand All @@ -13,33 +23,43 @@ function callInvesting (pairId, interval, candleCount, period) {
candle_count: candleCount,
volume_series: 'no',
events: 'no',
period: period
period: period,
},
headers: {
'Referer': 'https://www.investing.com/',
'X-Requested-With': 'XMLHttpRequest'
}
})
'X-Requested-With': 'XMLHttpRequest',
},
});
}

async function investing (input, interval = 3600, candleCount = 24, period = '1-day') {
/**
* Call Investing
* @param {string} input Input string, see mapping.js
* @param {number} interval Interval between results in seconds,
* only some values are accepted such as 900, 1800, 3600, 18000, etc
* @param {number} candleCount Number of total results, seems it must be >10,
* it depends on interval and period too
* @param {string} period n-hour, n-day, n-month or n-year string where n is a number
* @return {Array} An array of object with date and value properties
*/
async function investing(input, interval = 3600, candleCount = 24, period = '1-day') {
try {
if (!input) {
throw Error('Parameter input is required')
throw Error('Parameter input is required');
}
const endpoint = mapping[input]
const endpoint = mapping[input];
if (!endpoint) {
throw Error(`No mapping found for ${input}, check mapping.js`)
throw Error(`No mapping found for ${input}, check mapping.js`);
}
const response = await callInvesting(endpoint.pairId, interval, candleCount, period)
if (!response.data.candles) {
throw Error('No response.data.candles found')
const { data } = await callInvesting(endpoint.pairId, interval, candleCount, period);
if (!data.candles) {
throw Error('No data.candles found, please check your params');
}
const results = mapResponse(response.data.candles)
return results
const results = mapResponse(data.candles);
return results;
} catch (err) {
console.error(err.message)
console.error(err.message);
}
}

exports.investing = investing
exports.investing = investing;

0 comments on commit a2a494f

Please sign in to comment.