From: Samir Benmendil Date: Sat, 8 Aug 2020 11:46:23 +0000 (+0100) Subject: bin: add monzo and starling json to qif conversion scripts X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/d15e050a98639d3be72af6f3e7523a80ffad0245?ds=inline bin: add monzo and starling json to qif conversion scripts Used to import monzo and starling transactions history from their rest api into kmymoney. --- diff --git a/bin/monzo.py b/bin/monzo.py new file mode 100755 index 0000000..3c79c71 --- /dev/null +++ b/bin/monzo.py @@ -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 index 0000000..669cf06 --- /dev/null +++ b/bin/starling.py @@ -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