-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new CLI utility, along with a way to query data from the API
Fix #8
- Loading branch information
Showing
5 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const config = require('config') | ||
const meow = require('meow') | ||
const moment = require('moment') | ||
const bovespa = require('../lib') | ||
const { formatters } = require('../lib/util') | ||
const chalk = require('chalk') | ||
const { bold, green, red } = chalk | ||
|
||
const cli = meow(` | ||
Usage: | ||
$ bovespa <codes ...> [Options] | ||
Extracts data from bovespa and displays it on the terminal | ||
Options | ||
--date, -d <YYYY-MM-DD> [Default: Today] Which date to extract the data from | ||
--api, -a <API URL> [Default 'https://bovespa.nihey.page'] | ||
Examples: | ||
$ bovespa ABEV3 | ||
$ bovespa ABEV3 --date 2019-04-01 | ||
$ bovespa ABEV3 PETR4 BIDI4 --date 2019-04-01 | ||
`, { | ||
flags: { | ||
date: { | ||
type: 'string', | ||
alias: 'd', | ||
default: moment().format('YYYY-MM-DD') | ||
}, | ||
api: { | ||
type: 'string', | ||
alias: 'a', | ||
default: config.api | ||
} | ||
} | ||
}) | ||
|
||
async function main () { | ||
const codes = cli.input | ||
const date = cli.flags.date | ||
const api = cli.flags.api | ||
|
||
for (const code of codes) { | ||
const getQuote = bovespa(api) | ||
let data | ||
try { | ||
data = await getQuote(code, date) | ||
} catch (e) { | ||
if (e.response.status === 404) { | ||
console.log(chalk`CODE: ${red(bold(code))} @ ${red(bold(date))}`) | ||
console.log(red(bold('NOT FOUND'))) | ||
console.log('') | ||
continue | ||
} | ||
throw e | ||
} | ||
|
||
let color = green | ||
if (data.preult < data.preabe) { | ||
color = red | ||
} | ||
|
||
console.log(chalk`CODE: ${color(bold(data.codneg))} @ ${bold(date)}`) | ||
console.log(chalk`Variation: ${color(formatters.percentage((data.preult - data.preabe) / data.preult))}`) | ||
console.log(chalk`Opening: ${color(data.preabe)}`) | ||
console.log(chalk`Closing: ${color(data.preult)}`) | ||
console.log(chalk`Average: ${color(data.premed)}`) | ||
console.log(chalk`Max: ${color(data.premax)}`) | ||
console.log(chalk`Min: ${color(data.premin)}`) | ||
console.log('') | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
main() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"api": "https://bovespa.nihey.page", | ||
"db": { | ||
"database": "bovespa", | ||
"user": null, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const axios = require('axios') | ||
const moment = require('moment') | ||
const config = require('config') | ||
|
||
module.exports = function (api = config.api) { | ||
return 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); | ||
['preabe', 'premin', 'premax', 'premed', 'preult', 'preofc', 'preofv', 'preexe'].forEach(attr => { | ||
data[attr] = parseFloat(data[attr]) | ||
}) | ||
return data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters