Created
May 31, 2019 19:14
-
-
Save fakenickels/94cf1a6b1e4eb399c26dbf4bc88e504e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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