Skip to content

Commit

Permalink
Add intraday data on SDK and CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
nihey committed Aug 10, 2019
1 parent dfba18b commit 12c2c93
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ $ bovespa --help

Options
--date, -d <YYYY-MM-DD> [Default: Today] Which date to extract the data from
--api, -a <API URL> [Default 'https://bovespa.nihey.page']
--api, -a <API URL> [Default 'https://bovespa.nihey.org']

Examples:
$ bovespa ABEV3
Expand Down
18 changes: 15 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const cli = meow(`
Options
--date, -d <YYYY-MM-DD or int> [Default: Today] Which date to extract the data from or days relative to today
--api, -a <API URL> [Default 'https://bovespa.nihey.page']
--api, -a <API URL> [Default 'https://bovespa.nihey.org']
Examples:
$ bovespa ABEV3
Expand All @@ -34,7 +34,7 @@ const cli = meow(`
api: {
type: 'string',
alias: 'a',
default: process.env.BOVESPA_API || 'https://bovespa.nihey.page'
default: process.env.BOVESPA_API || 'https://bovespa.nihey.org'
}
}
})
Expand All @@ -57,7 +57,19 @@ async function main () {
date = moment().add(intDate, 'days').format('YYYY-MM-DD')
}

data = await getQuote(code, date)
if (moment(date).isSame(moment(), 'day')) {
data = await getQuote.intraday(code)
data = {
codneg: data.code,
preult: data.price,
preabe: data.priceopen,
premed: null,
premax: data.high,
premin: data.low
}
} else {
data = await getQuote(code, date)
}
} catch (e) {
if (e.response.status === 404) {
console.log(chalk`CODE: ${red(bold(code))} @ ${red(bold(date))}`)
Expand Down
1 change: 1 addition & 0 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = async (fastify, options) => {
'datadelay',
'volumeavg',
'volume',
'closeyest',
'tradetime'
]
attrs.forEach(attr => {
Expand Down
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const axios = require('axios')
const moment = require('moment')

module.exports = function (api = process.env.BOVESPA_API || 'https://bovespa.nihey.page') {
return async function (code, date) {
module.exports = function (api = process.env.BOVESPA_API || 'https://bovespa.nihey.org') {
const func = async function (code, date) {
code = (code || '').toUpperCase()
date = date || moment().format('YYYY-MM-DD')
const data = axios.get(`${api}/api/quote/${code}/${date}`).then(r => r.data);
Expand All @@ -11,4 +11,12 @@ module.exports = function (api = process.env.BOVESPA_API || 'https://bovespa.nih
})
return data
}

func.intraday = async function (code) {
code = (code || '').toUpperCase()
const data = axios.get(`${api}/api/intraday/${code}`).then(r => r.data)
return data
}

return func
}

0 comments on commit 12c2c93

Please sign in to comment.