Skip to content

Instantly share code, notes, and snippets.

@dequis dequis/n26_ynab.py
Created Jan 4, 2020

Embed
What would you like to do?
Script I use to send n26 transactions to YNAB. Not documented as it's not really intended to be used by anyone but me but good luck.
import requests
from n26.api import Api as N26Api
from datetime import datetime, timedelta
from pprint import pprint
ENABLE_YNAB = True
YNAB_TOKEN = '...'
BUDGET_ID = '...'
ACCOUNT_ID = '...'
IMPORT_ID_FMT = 'dx-n26:{unix_ts}:{amount}'
DAYS_BACK = timedelta(days=7)
YNAB_BASE = 'https://api.youneedabudget.com/v1'
YNAB_TRANS_URL = f'{YNAB_BASE}/budgets/{BUDGET_ID}/transactions'
def process_n26_transaction_for_ynab(tran):
pending = (tran['type'] == 'AA' or tran['pending'])
unix_ts = int(tran['visibleTS'] / 1000)
amount = int(tran['amount'] * 1000)
import_id = IMPORT_ID_FMT.format(unix_ts=unix_ts, amount=amount)
date = datetime.fromtimestamp(unix_ts).date().isoformat()
return {
'account_id': ACCOUNT_ID,
'date': date,
'payee_name': tran.get('merchantName', tran.get('partnerName', '?')),
'memo': tran.get('referenceText', ''),
'amount': amount,
'cleared': 'uncleared' if pending else 'cleared',
'import_id': import_id,
}
def send_transactions_to_ynab(trans):
response = requests.post(
YNAB_TRANS_URL,
json={'transactions': trans},
headers={'Authorization': 'Bearer ' + YNAB_TOKEN},
)
return response.json()
def main():
now = datetime.utcnow()
interval = [
int(dt.timestamp() * 1000)
for dt in [now - DAYS_BACK, now]
]
trans = N26Api().get_transactions(*interval, limit=1000)
trans = [process_n26_transaction_for_ynab(tran) for tran in trans]
if ENABLE_YNAB:
pprint(send_transactions_to_ynab(trans))
else:
pprint(trans)
print(len(trans))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.