Created
April 3, 2017 10:35
-
-
Save mtrovo/10b1654ad0036aba7b39fcb038954d86 to your computer and use it in GitHub Desktop.
N26 transactions to YNAB CSV format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from n26 import api | |
from os import path | |
from itertools import takewhile | |
import csv | |
from sys import stdout | |
from datetime import datetime | |
from os import environ | |
DEBUG = environ.get('DEBUG') | |
def main(): | |
last_transaction = get_last_tid() | |
writer = csv.writer(stdout, delimiter=',') | |
# header | |
writer.writerow("Date,Payee,Category,Memo,Outflow,Inflow".split(',')) | |
n26 = api.Api() | |
transactions = n26.get_transactions_limited(40) | |
for t in takewhile(lambda x: x['id'] != last_transaction, | |
transactions): | |
writer.writerow(to_row(t)) | |
if not DEBUG: | |
set_last_tid(transactions[0]['id']) | |
def to_row(tr): | |
ts = tr['confirmed'] / 1000 | |
date = datetime.fromtimestamp(ts).strftime('%d/%m/%Y') | |
payee = tr.get('merchantName', tr.get('partnerName', '')) | |
category = '' | |
memo = tr.get('referenceText', '') | |
outflow = tr['amount'] | |
inflow = 0 | |
if outflow < 0: | |
outflow = -outflow | |
else: | |
inflow = outflow | |
outflow = 0 | |
return [date, payee, category, memo, outflow, inflow] | |
def get_last_tid(): | |
if path.exists('.tid'): | |
with open('.tid') as tid: | |
return tid.read().strip() | |
return None | |
def set_last_tid(cur): | |
with open('.tid', 'w') as tid: | |
tid.write(cur) | |
tid.write('\n') | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.python.org/simple" | |
verify_ssl = true | |
[packages] | |
n26 = "*" | |
pynynab = "*" | |
sqlalchemy = "*" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment