]> git.rmz.io Git - dotfiles.git/blobdiff - bin/starling.py
bin: add monzo and starling json to qif conversion scripts
[dotfiles.git] / bin / starling.py
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