-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert-number26-json.py
executable file
·82 lines (67 loc) · 3.13 KB
/
convert-number26-json.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from ledger import *
import os, sys, io
from functools import reduce
import json
import datetime
import time
#################### BEGIN CONFIG ##########################
from config import *
#transaction_type_direction_ = {"CT":1,"AA":-1,"PT":-1,"PF":-1,"AV":1, "AE":1, "DD":-1, "DT":-1}
#################### CONFIG END ##########################
def addTagsFromDict(t, dct, prepend=""):
for k,v in dct.items():
if isinstance(v,dict):
addTagsFromDict(t, v, k+"_")
else:
t.addTag(prepend+k,str(v).strip())
newjournal = []
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf8',newline='')
json_array_sorted = sorted(json.load(input_stream), key=lambda x: x["visibleTS"])
for jsontrsc in json_array_sorted:
date = datetime.date.fromtimestamp(jsontrsc["visibleTS"]/1000.0)
currency = jsontrsc["currencyCode"]
# if "newAmount" in jsontrsc and "oldAmount" in jsontrsc:
# amount = jsontrsc["newAmount"] - jsontrsc["oldAmount"] #gives us plus/minus sign which "amount" does not give
# else:
#assert("type" in jsontrsc and jsontrsc["type"] in transaction_type_direction_)
amount = jsontrsc["amount"] # * transaction_type_direction_[(jsontrsc["type"])]
jsontrsc["Amount"] = amount
name = " ".join([ str(jsontrsc[k]).strip() for k in ["bankTransferTypeText","partnerName","merchantName","merchantCity"] if k in jsontrsc ])
new_transaction = None
for (guardfunc, possible_transaction) in n26_transaction_matchors.items():
try:
if not guardfunc(jsontrsc):
continue
except Exception as e:
continue
new_transaction = possible_transaction.copy()
break # first match wins
if new_transaction is None:
if amount > 0.0:
new_transaction = unknown_revenue_default_transaction_.copy()
elif amount < 0.0:
new_transaction = unknown_expense_default_transaction_.copy()
else:
new_transaction = unknown_equity_default_transaction_.copy()
## Convert FutureAmountFractions to real Amounts
for p in new_transaction.postings:
if isinstance(p.amount, FutureAmountFraction):
p.amount.convertToAmount(Amount(amount,currency))
new_transaction.setDate(date).addComment(name)
new_posting = Posting(n26_primary_account_, Amount(amount, currency))
if amount < 0.0 and "newAmount" in jsontrsc:
new_posting.addPostPostingAssertAmount(Amount(jsontrsc["newAmount"], currency))
elif amount > 0.0 and "oldAmount" in jsontrsc:
new_posting.addPostPostingAssertAmount(Amount(jsontrsc["oldAmount"], currency))
new_transaction.prependPosting(new_posting)
addTagsFromDict(new_transaction, jsontrsc)
newjournal.append(new_transaction)
not_balanced = [t for t in newjournal if not t.isBalanced()]
if len(not_balanced) > 0:
print("ERROR: the following transactions are not balanced !!\n", file=sys.stderr)
print("\n\n".join(map(str,(sortTransactionsByDate(not_balanced)))), file=sys.stderr)
sys.exit(1)
assert(len(not_balanced) == 0)
print("\n\n".join(map(str,(newjournal))))