Skip to content

Commit

Permalink
chore: Use pyitau-browser
Browse files Browse the repository at this point in the history
  • Loading branch information
andreroggeri committed May 15, 2024
1 parent 26f81ca commit 1de263e
Show file tree
Hide file tree
Showing 7 changed files with 1,264 additions and 71 deletions.
18 changes: 10 additions & 8 deletions brbanks2ynab/importers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from typing import List

from pyitau import Itau
from pyitau_browser import Itau
from pynubank import Nubank
from ynab_sdk.api.models.responses.accounts import Account

Expand Down Expand Up @@ -43,24 +43,26 @@ def get_nubank_importers(importer_config, ynab_accounts):
logger.warn('[Nubank] Checking account data is currently disabled')
# account = find_account_by_name(ynab_accounts, importer_config.nubank.checking_account)
# importers.append(NubankCheckingAccountData(nu, account.id))

return importers


def get_itau_importers(importer_config: ImporterConfig, ynab_accounts):
logger.info('[Itaú] Fetching data')
importers = []

account_no = importer_config.itau.account_no[:-1]
account_digit = importer_config.itau.account_no[-1]
itau = Itau(importer_config.itau.branch, account_no, account_digit, importer_config.itau.password)

itau = Itau()
logger.info('[Itaú] Authenticating')
itau.authenticate()
try:
itau.login(importer_config.itau.branch, importer_config.itau.account_no, importer_config.itau.password)
except:
itau.context.tracing.stop(path='trace.zip')
raise
logger.info('[Itaú] Authenticated')
# itau.login(importer_config.itau.branch, importer_config.itau.account_no, importer_config.itau.password)
# credit_card_account_id = find_account_by_name(ynab_accounts, importer_config.itau.credit_card_account_name)
checking_account_id = find_account_by_name(ynab_accounts, importer_config.itau.checking_account_name)
importers.append(ItauCheckingAccount(itau, checking_account_id.id))
# importers.append(ItauCreditCard(itau, credit_card_account_id.id))

return importers
22 changes: 9 additions & 13 deletions brbanks2ynab/importers/itau/itau_checking_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from datetime import datetime
from typing import Iterable

from pyitau import Itau
from pyitau_browser import Itau
from pyitau_browser.pyitau import AccountStatement

from brbanks2ynab.importers.data_importer import DataImporter
from brbanks2ynab.importers.transaction import Transaction
Expand All @@ -14,19 +15,14 @@ def __init__(self, itau: Itau, account_id: str):
self.account_id = account_id

def get_data(self) -> Iterable[Transaction]:
statements = self.itau.get_statements()
transactions = filter(self._is_useful_transaction, statements['lancamentos'])
return map(self._to_transaction, transactions)
statements = self.itau.get_account_statements()
return map(self._to_transaction, statements)

def _to_transaction(self, itau_transaction: dict) -> Transaction:
date = datetime.strptime(itau_transaction['dataLancamento'], '%d/%m/%Y')
amount = float(itau_transaction['valorLancamento'].replace('.', '').replace(',', '.'))
is_inflow = itau_transaction['ePositivo']
description = itau_transaction['descricaoLancamento']
tx_id = str(hashlib.sha1(f'{date.isoformat()}:{amount}:{is_inflow}:{description}'.encode('utf-8')).hexdigest())

if not is_inflow:
amount *= -1
def _to_transaction(self, itau_transaction: AccountStatement) -> Transaction:
date = itau_transaction['date']
amount = itau_transaction['amount']
description = itau_transaction['description']
tx_id = str(hashlib.sha1(f'{date.isoformat()}:{amount}:{description}'.encode('utf-8')).hexdigest())

return {
'transaction_id': tx_id[:35],
Expand Down
Loading

0 comments on commit 1de263e

Please sign in to comment.