-
Notifications
You must be signed in to change notification settings - Fork 2
/
wise_json.py
executable file
·129 lines (109 loc) · 4.24 KB
/
wise_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Imports balance statements output by Wise API in json format.
"""
import decimal
import json
from datetime import timedelta
import beangulp
import dateutil.parser
from beancount.core import flags
from beancount.core.amount import Amount
from beancount.core.data import Balance, Posting, Transaction, new_metadata
from uabean.importers.mixins import IdentifyMixin
class Importer(IdentifyMixin, beangulp.Importer):
FLAG = flags.FLAG_OKAY
def __init__(self, account, fees_account="Expenses:Fees:Wise", **kwargs):
self._account = account
self.fees_account = fees_account
if "business" in account.lower():
type_pattern = "business"
elif "personal" in account.lower():
type_pattern = "personal"
else:
type_pattern = ".*?"
fname_pattern = "wise-" + type_pattern + r"-.*-\w{3}\.json"
kwargs["matchers"] = [("filename", fname_pattern)]
super().__init__(**kwargs)
def file_account(self, filename):
# example: wise-business-2022-01-01_2022-10-01-USD.json
parts = filename.split("-")
account_type = parts[1].capitalize()
currency = parts[-1].split(".", 1)[0]
return self._account.format(type=account_type, currency=currency)
def account(self, _):
return "wise"
@staticmethod
def date_from_str(s):
return dateutil.parser.parse(s)
def data_from_file(self, filename):
return json.load(open(filename, encoding="utf-8"), parse_float=decimal.Decimal)
def date(self, filename):
data = self.data_from_file(filename)
return data["query"]["intervalEnd"]
def extract(self, filename, existing_entries=None):
data = self.data_from_file(filename)
entries = []
account = self.file_account(filename)
for i, t in enumerate(data["transactions"]):
meta = new_metadata(filename, -(i + 1))
entries.append(self.entry_from_transaction(meta, account, t))
if data["transactions"]:
entries.append(
Balance(
new_metadata(filename, -(i + 1)),
(
self.date_from_str(data["query"]["intervalEnd"])
+ timedelta(days=1)
).date(),
account,
amount_from_obj(data["endOfStatementBalance"]),
None,
None,
)
)
return entries
def entry_from_transaction(self, meta, account, t):
dt = self.date_from_str(t["date"])
meta["time"] = dt.time().strftime("%H:%M:%S")
meta["src_id"] = t["referenceNumber"]
if t["exchangeDetails"] is not None:
meta["converted"] = (
f'{t["exchangeDetails"]["toAmount"]["value"]} {t["exchangeDetails"]["toAmount"]["currency"]} ({t["exchangeDetails"]["rate"]})'
)
postings = [
Posting(account, amount_from_obj(t["amount"]), None, None, None, None)
]
if not t["totalFees"]["zero"]:
postings.append(
Posting(
self.fees_account,
amount_from_obj(t["totalFees"]),
None,
None,
None,
None,
)
)
match t["details"]["type"]:
case "TRANSFER":
payee = t["details"]["recipient"]["name"]
case "CARD":
payee = t["details"]["merchant"]["name"]
meta["src_category"] = t["details"]["category"]
case "DEPOSIT":
payee = t["details"]["senderName"]
case "UNKNOWN":
payee = None
case "MONEY_ADDED":
payee = "self"
case _:
raise ValueError(f"unknown transaction type: {t['details']['type']}")
return Transaction(
meta, dt.date(), self.FLAG, payee, None, set(), set(), postings
)
def amount_from_obj(obj):
return Amount(obj["value"], obj["currency"])
def get_test_importer():
return Importer(account="Assets:Wise:personal:{currency}")
if __name__ == "__main__":
from beangulp.testing import main
main(get_test_importer())