Exemplo n.º 1
0
def _download_external_data():
    nu = Nubank(os.environ.get('NUBANK_LOGIN'),
                os.environ.get('NUBANK_PASSWORD'))
    card_transactions = nu.get_card_statements()
    _transactions_to_csv(card_transactions, 'card_transactions')

    account_transactions = nu.get_account_statements()
    _transactions_to_csv(account_transactions, 'account_transactions')
Exemplo n.º 2
0
    nu = Nubank()
    nu.authenticate_with_refresh_token(NUBANK_TOKEN, './cert.p12')

    transactions = filter_transactions(nu.get_card_statements(),
                                       STARTING_POINT)

    print(f'Found {len(transactions)} card transactions')
    for transaction in transactions:
        ynab.add_transaction(payee=transaction['description'],
                             date=parse_transaction_date(transaction),
                             value=-int(transaction['amount']) / 100,
                             id=transaction['id'],
                             subcategory=transaction['category'].capitalize(),
                             account=NUBANK_CARD_ACCOUNT)

    account_transactions = filter_transactions(nu.get_account_statements(),
                                               STARTING_POINT)

    print(f'Found {len(account_transactions)} account transactions')
    for transaction in account_transactions:
        print(transaction)
        payee = transaction['title']
        if not transaction.get('__typename') in IN_EVENT:
            transaction['amount'] = transaction['amount'] * -1
            if 'destinationAccount' in transaction:
                payee = transaction['destinationAccount']['name']
        else:
            if 'originAccount' in transaction:
                payee = transaction['originAccount']['name']
        ynab.add_transaction(payee=payee,
                             date=parse_transaction_date(transaction),
Exemplo n.º 3
0
class NubankBot:
    nu = None
    logged = False
    n_card = None

    def __init__(self):
        self.nu = Nubank()

    def set_nubank_command(self, n: NubankCards):
        self.n_card = n

    def get_qr_code(self):

        uid, qr_code = self.nu.get_qr_code()
        img: PilImage = qr_code.make_image()

        NubankSession.objects.update_or_create(session_id=uid,
                                               defaults={"session_id": uid})

        name = f"./{uuid.uuid4().hex}.png"
        img.save(name)

        qr_code.print_ascii(invert=True)

        return name

    def _login(self, pwd: str):

        last_login_diff = timezone.now() - self.n_card.last_login
        total_hours = last_login_diff.total_seconds() / 3600
        if total_hours < 12:
            raise Exception("You can't login now")

        nu_session = NubankSession.objects.order_by("-created_at").first()

        self.nu.authenticate_with_qr_code(self.n_card.cpf, pwd,
                                          nu_session.session_id)
        self.logged = True
        self.n_card.last_login = timezone.now()
        self.n_card.save()

    def execute(self, pwd: str, card: NubankCards):

        if not self.logged:
            self._login(pwd)

        card_statements = self.nu.get_card_statements()

        for n in card_statements:

            amount_without_iof = 0
            if n.get("amount_without_iof"):
                amount_without_iof = Decimal(n["amount_without_iof"]) / 100

            defaults = {
                "amount": Decimal(n["amount"]) / 100,
                "amount_without_iof": amount_without_iof,
                "description": n.get("description"),
                "category": n.get("category"),
                "source": n.get("source"),
                "title": n.get("title"),
                "account": n.get("account"),
                "details": n.get("details"),
                "nubank_id": n["id"],
                "href": n.get("href"),
                "item_time": parse(n["time"]),
            }

            NubankStatement.objects.get_or_create(nubank_id=n["id"],
                                                  defaults=defaults)

        self._execute_bank_statements(card)

        return

    def _execute_bank_statements(self, card: NubankCards):
        account_statements = self.nu.get_account_statements()

        for a in account_statements:
            if a["__typename"] == "TransferOutReversalEvent":
                continue

            defaults = {
                "nubank_id": a["id"],
                "title": a["title"],
                "detail": a["detail"],
                "amount": Decimal(a["amount"]),
                "post_date": a["postDate"],
                "_type": a["__typename"],
                "cpf": card.cpf,
            }
            NubankBankStatement.objects.get_or_create(nubank_id=a["id"],
                                                      defaults=defaults)
Exemplo n.º 4
0
import json
from pprint import pprint
from pynubank import Nubank

credentials = json.load(open('nubank_credentials.json'))

print(credentials)

nu = Nubank(credentials['cpf'], credentials['password'])

transactions = nu.get_account_statements()

for entry in transactions:
    row = [
        entry['time'], entry['title'], entry['description'], entry['amount']
    ]
    if 'tags' in entry['details']:
        row.append(entry['details']['tags'])
    pprint(row)