Last active
April 1, 2019 07:43
-
-
Save celsopalmeiraneto/53fea079f7cf8b951b1d5e843f6e04d8 to your computer and use it in GitHub Desktop.
Santander Brasil - Ler Faturas dos Cartões
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
function readPageAndGenerateResults() { | |
function getBRLToUSD() { | |
const text = document.querySelector('.tabla_datos:nth-of-type(2) table:nth-of-type(2) tr:last-of-type td:last-of-type').textContent; | |
return parseNumber(text); | |
} | |
function getCCLedgers() { | |
const exchangeRate = getBRLToUSD(); | |
const ledger = Array.from(document.querySelector('.tabla_datos:nth-of-type(1) table tbody').rows); | |
let currentCard = null; | |
const parsedLedger = {}; | |
for (row of ledger) { | |
if (row.classList.contains('cabecera2')) { | |
currentCard = row.querySelector('dd:last-of-type').textContent; | |
currentCard = currentCard.trim(); | |
parsedLedger[currentCard] = []; | |
continue; | |
} | |
if (row.cells[0].textContent.trim() === '01/01/1') continue; | |
const dateAsText = row.cells[0].textContent.trim(); | |
const date = new Date( | |
dateAsText.substr(6, 4), | |
dateAsText.substr(3, 2), | |
dateAsText.substr(0, 2), | |
); | |
let valueInUSD = parseNumber(row.cells[2].textContent.trim()); | |
let valueInBRL = parseNumber(row.cells[3].textContent.trim()); | |
if (valueInBRL === 0) { | |
valueInBRL = valueInUSD * exchangeRate; | |
} | |
parsedLedger[currentCard].push({ | |
date, | |
description: row.cells[1].textContent.trim(), | |
valueInUSD, | |
valueInBRL, | |
}); | |
} | |
return parsedLedger; | |
} | |
function parseNumber(text) { | |
text = text.replace(new RegExp('[.]', 'g'), '').replace(',', '.'); | |
return Number(text); | |
} | |
function ledgerToCelsosExcel(ledgers) { | |
const joinedLedgers = Object.values(ledgers).reduce((acc, ledger) => acc.concat(ledger)); | |
const groupedItems = joinedLedgers.reduce((acc, item) => { | |
if (!acc[item.description]) { | |
acc[item.description] = item; | |
} else { | |
const reduced = acc[item.description]; | |
reduced.valueInBRL += item.valueInBRL; | |
reduced.valueInUSD += item.valueInUSD; | |
} | |
return acc; | |
}, {}); | |
return Object.values(groupedItems).reduce((plainText, item) => { | |
plainText += `${item.date.getMonth()+1}/${item.date.getDate()}/${item.date.getFullYear()}\t`; | |
plainText += `${item.description}\t`; | |
plainText += `\t`; | |
plainText += `${item.valueInBRL*-1}\n`; | |
return plainText; | |
}, ''); | |
} | |
return ledgerToCelsosExcel(getCCLedgers()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment