Skip to content

Instantly share code, notes, and snippets.

@ceifa
Last active March 4, 2024 13:43
Show Gist options
  • Save ceifa/2583ba5a4ff01d8ac48010c4d6167494 to your computer and use it in GitHub Desktop.
Save ceifa/2583ba5a4ff01d8ac48010c4d6167494 to your computer and use it in GitHub Desktop.
import axios from 'axios'
import auth from './auth.json' assert { type: "json" }
import * as uuid from 'uuid'
import qr from 'qrcode-terminal'
import fs from 'node:fs/promises'
import { setTimeout } from 'node:timers/promises'
const randomuuid = uuid.v4()
console.log('QR Code ID:', randomuuid)
qr.generate(randomuuid, { small: true })
const { data: apis } = await axios.get('https://prod-s0-webapp-proxy.nubank.com.br/api/discovery')
const { data: app_apis } = await axios.get('https://prod-s0-webapp-proxy.nubank.com.br/api/app/discovery')
const { data: { access_token } } = await axios.post(apis.login, auth)
console.log('Log-in with the QR code in the Nubank app')
console.log('When done press any key to continue...');
await new Promise(resolve => process.stdin.once('data', resolve));
const { data: { access_token: lift_access_token, _links: lift_links } } = await axios.post(app_apis.lift, {
qr_code_id: randomuuid,
type: 'login-webapp'
}, {
headers: {
Authorization: `Bearer ${access_token}`
}
})
const { data: { events } } = await axios.get(lift_links.events.href, {
headers: {
Authorization: `Bearer ${lift_access_token}`
}
})
await fs.writeFile('events.json', JSON.stringify(events, null, 4))
// Filtrar transações do mês passado
const filteredTransactions = events
.filter(ev => ev.category === 'transaction' || ev.category === 'insurance_payment' || ev.category === 'nutag_parking_transaction')
.filter(transaction => {
const { time, details } = transaction;
const transactionDate = new Date(time);
transactionDate.setHours(transactionDate.getHours() - 3); // Ajustar para o fuso horário de Brasília
// Verificar se a transação é uma parcela e, se sim, se uma das parcelas foi paga no mês passado
if (details?.charges?.count > 1) {
const monthsSinceTransaction = ((new Date()).getFullYear() - transactionDate.getFullYear()) * 12 + (new Date()).getMonth() - transactionDate.getMonth();
return monthsSinceTransaction > 0 && monthsSinceTransaction <= details.charges.count;
} else {
const currentDate = new Date();
const startOfLastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 0);
const endOfLastMonth = new Date(startOfLastMonth.getFullYear(), startOfLastMonth.getMonth() + 2, 0);
return transactionDate >= startOfLastMonth && transactionDate <= endOfLastMonth;
}
});
await fs.writeFile('last-month-transactions.json', JSON.stringify(filteredTransactions, null, 4))
console.log('Transações do mês passado:', filteredTransactions.length);
const totalAmount = filteredTransactions.reduce((acc, transaction) => {
if (transaction.details?.charges?.amount) {
return acc + transaction.details.charges.amount;
} else {
return acc + transaction.amount;
}
}, 0);
console.log('Total gasto no mês passado:', totalAmount / 100);
let totalPerCard = {};
let currentProgress = 0;
for (const transactionOverview of filteredTransactions) {
currentProgress++;
if (transactionOverview.category === 'insurance_payment' || transactionOverview.category === 'nutag_parking_transaction') {
continue;
}
const { data: { transaction } } = await axios.get(transactionOverview._links.self.href, {
headers: {
Authorization: `Bearer ${lift_access_token}`
}
})
const card = transaction.card_last_four_digits;
if (!totalPerCard[card]) {
totalPerCard[card] = 0;
}
if (transactionOverview.details?.charges) {
totalPerCard[card] += transactionOverview.details.charges.amount;
} else {
totalPerCard[card] += transactionOverview.amount;
}
const progress = Math.floor(Number(currentProgress) / Number(filteredTransactions.length) * 100);
const progressBar = '='.repeat(Math.floor(progress / 2));
const progressBarRemainder = '-'.repeat(Math.floor((100 - progress) / 2));
process.stdout.write(`[${progressBar}${progressBarRemainder}] ${progress}% (${currentProgress}/${filteredTransactions.length} transações)\r`);
await setTimeout(5000);
}
console.log('\nTotal gasto no mês passado por cartão:')
for (const card in totalPerCard) {
console.log(`${card}:`, totalPerCard[card] / 100);
}
process.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment