Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created May 31, 2019 19:14
Show Gist options
  • Save fakenickels/94cf1a6b1e4eb399c26dbf4bc88e504e to your computer and use it in GitHub Desktop.
Save fakenickels/94cf1a6b1e4eb399c26dbf4bc88e504e to your computer and use it in GitHub Desktop.
import 'isomorphic-fetch'
import cheerio from 'cheerio'
import moment from 'moment-timezone'
import BigNumber from 'bignumber.js'
export const crawlSELIC = async () => {
const page = await fetch(
'http://idg.receita.fazenda.gov.br/orientacao/tributaria/pagamentos-e-parcelamentos/taxa-de-juros-selic',
)
const $ = cheerio.load(await page.text())
return $('body table.listing:nth-of-type(5) tr')
.toArray()
.filter((_, i) => i !== 0)
.map((item, month) => {
return $(item)
.children('td')
.toArray()
.filter((_, i) => i !== 0)
.map((value, year) => ({
date: moment
.tz(`${2015 + year}-${String(1 + month).padStart(2, '0')}-01`, 'America/Sao_Paulo')
.endOf('month')
.startOf('day')
.toDate(),
selic: new BigNumber(
$(value)
.text()
.replace(/[%,]/g, ''),
)
.dividedBy(100)
.toNumber(),
}))
})
.reduce((acc, item) => acc.concat(item), [])
.sort((a, b) => a.date - b.date)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment