Beispiel #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')
    def handle(self, *args, **options):

        nu = Nubank()
        uuid, qr_code = nu.get_qr_code()

        qr_code.print_ascii(invert=True)

        nu.authenticate_with_qr_code(NUBANK_CPF, NUBANK_PASSWORD, uuid)
        print(nu.get_account_balance())

        card_statements = nu.get_card_statements()
Beispiel #3
0
            logging.config.dictConfig(config)
        return True
    except FileNotFoundError:
        print('Missing logging.json, logging not configured.')
        return False


YNAB_EMAIL = os.getenv('YNAB_EMAIL')
YNAB_PASSWORD = os.getenv('YNAB_PASSWORD')
YNAB_BUDGET = os.getenv('YNAB_BUDGET')
NUBANK_LOGIN = os.getenv('NUBANK_LOGIN')
NUBANK_PASSWORD = os.getenv('NUBANK_PASSWORD')
STARTING_POINT = datetime.datetime.strptime(os.getenv('STARTING_POINT'), '%Y-%m-%d').date()

if __name__ == '__main__':
    log_config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logging.json')
    setup_logging(log_config_file)
    ynab = YNAB(YNAB_EMAIL, YNAB_PASSWORD, YNAB_BUDGET)
    nu = Nubank(NUBANK_LOGIN, NUBANK_PASSWORD)
    transactions = filter_transactions(nu.get_card_statements(), STARTING_POINT)
    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()
        )

    ynab.sync()
Beispiel #4
0
IN_EVENT = ['TransferInEvent', 'TransferOutReversalEvent']

if __name__ == '__main__':
    with open('cert.p12', 'wb') as f:
        cert_content = base64.b64decode(NUBANK_CERT)
        f.write(cert_content)

    log_config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                   'logging.json')
    setup_logging(log_config_file)
    ynab = YNAB(YNAB_EMAIL, YNAB_PASSWORD, YNAB_BUDGET)
    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')
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)