Skip to content

Commit

Permalink
Add new CLI utility, along with a way to query data from the API
Browse files Browse the repository at this point in the history
Fix #8
  • Loading branch information
nihey committed Apr 9, 2019
1 parent 63b4392 commit ac512d6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
76 changes: 76 additions & 0 deletions bin/cli.js
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()
}
1 change: 1 addition & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"api": "https://bovespa.nihey.page",
"db": {
"database": "bovespa",
"user": null,
Expand Down
15 changes: 15 additions & 0 deletions lib/index.js
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
}
}
6 changes: 6 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const sequelize = require('../db')
const { Quote } = sequelize

module.exports = {
formatters: {
percentage: (number) => {
return (number * 100).toFixed(2) + '%'
}
},

get: {
filled: async (start = moment().startOf('year').format('YYYY-MM-DD')) => {
const end = moment(start).endOf('year')
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "bovespa",
"version": "0.1.0",
"description": "Recover Bovespa's data freely",
"description": "Bovespa data extractor, server, and command line interface",
"main": "./lib/index.js",
"bin": {
"bovespa": "./bin/index.js"
"bovespa": "./bin/cli.js"
},
"scripts": {
"serve": "fastify start bin/server.js -p 7000 -a 0.0.0.0 -l debug -w",
Expand Down Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"adm-zip": "^0.4.13",
"axios": "^0.18.0",
"chalk": "^2.4.2",
"config": "^3.0.1",
"fastify": "^2.1.0",
"fastify-cli": "^1.0.0",
Expand Down

0 comments on commit ac512d6

Please sign in to comment.