This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sync.py
53 lines (43 loc) · 1.67 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import datetime
import json
import logging.config
import os
import base64
from pynubank import Nubank
from nubank_sync_ynab.util import filter_transactions, parse_transaction_date
from nubank_sync_ynab.ynab import YNAB
def setup_logging(filename):
try:
with open(filename) as f:
config = json.loads(f.read())
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_TOKEN = os.getenv('NUBANK_TOKEN')
NUBANK_CERT = os.getenv('NUBANK_CERT')
STARTING_POINT = datetime.datetime.strptime(os.getenv('STARTING_POINT'), '%Y-%m-%d').date()
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)} 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()
)
ynab.sync()