]> git.rmz.io Git - dotfiles.git/commitdiff
bin: add monzo and starling json to qif conversion scripts
authorSamir Benmendil <me@rmz.io>
Sat, 8 Aug 2020 11:46:23 +0000 (12:46 +0100)
committerSamir Benmendil <me@rmz.io>
Sat, 8 Aug 2020 11:51:04 +0000 (12:51 +0100)
Used to import monzo and starling transactions history from their rest
api into kmymoney.

bin/monzo.py [new file with mode: 0755]
bin/starling.py [new file with mode: 0755]

diff --git a/bin/monzo.py b/bin/monzo.py
new file mode 100755 (executable)
index 0000000..3c79c71
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+from datetime import datetime
+import json
+import sys
+
+outformat = "qif"
+# outformat = "csv"
+
+if __name__ == "__main__":
+    if outformat == "qif":
+        print("!Type:Bank")
+    cnt = 1
+    with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as fp:
+        data = json.load(fp)
+        for t in data.get("transactions"):
+            try:
+                date = datetime.strptime(t["created"], "%Y-%m-%dT%H:%M:%S.%fZ")
+            except ValueError:
+                date = datetime.strptime(t["created"], "%Y-%m-%dT%H:%M:%SZ")
+            amount = t["amount"] / 100
+            if "name" in t.get("counterparty", {}):
+                payee = t["counterparty"]["name"]
+            elif t["merchant"] is None:
+                payee = t["description"]
+            else:
+                payee = t["merchant"]["name"]
+            memo = t["description"]
+            memo += "\n{local_amount} {local_currency}".format(**t)
+            notes = t["notes"]
+            if outformat == "csv":
+                print('{date:%Y-%m-%d},{amount},"{payee}","{description}","{notes}"'.format(date=date,
+                                                                                            payee=payee,
+                                                                                            amount=amount,
+                                                                                            **t))
+            elif outformat == "qif":
+                print("D{:%Y-%m-%d}".format(date))
+                print("T{}".format(amount))
+                print("P{}".format(payee))
+                print("M{}".format(memo))
+                print("#{}".format(t['id']))
+                print("^")
+            cnt += 1
diff --git a/bin/starling.py b/bin/starling.py
new file mode 100755 (executable)
index 0000000..669cf06
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+from datetime import datetime
+import json
+import sys
+
+outformat = "qif"
+# outformat = "csv"
+
+class FeedError(Exception):
+    pass
+
+def read_feed_item(i):
+    try:
+        date = datetime.strptime(i["transactionTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
+        amount = i["amount"]["minorUnits"] / 100
+        if i["direction"] == "OUT":
+            amount = -amount
+        payee = i["counterPartyName"]
+        reference = i.get("reference", payee)
+        if outformat == "csv":
+            print('{date:%Y-%m-%d},{amount},"{payee}","{description}","{notes}"'.format(date=date,
+                payee=payee,
+                amount=amount,
+                description=reference,
+                notes=reference))
+        elif outformat == "qif":
+            print("D{:%Y-%m-%d}".format(date))
+            print("T{}".format(amount))
+            print("P{}".format(payee))
+            print("M{}".format(reference))
+            print("#{}".format(i['feedItemUid']))
+            print("^")
+    except KeyError:
+        raise FeedError(i['feedItemUid'])
+
+
+if __name__ == "__main__":
+    if outformat == "qif":
+        print("!Type:Bank")
+    cnt = 1
+    with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as fp:
+        data = json.load(fp)
+        for i in data.get('feedItems'):
+            read_feed_item(i)
+            cnt += 1