]> git.rmz.io Git - dotfiles.git/blob - bin/starling.py
vim: open commit window in a new tab
[dotfiles.git] / bin / starling.py
1 #!/usr/bin/env python3
2
3 from datetime import datetime
4 import json
5 import sys
6
7 outformat = "qif"
8 # outformat = "csv"
9
10 class FeedError(Exception):
11 pass
12
13 def read_feed_item(i):
14 try:
15 date = datetime.strptime(i["transactionTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
16 amount = i["amount"]["minorUnits"] / 100
17 if i["direction"] == "OUT":
18 amount = -amount
19 payee = i["counterPartyName"]
20 reference = i.get("reference", payee)
21 if outformat == "csv":
22 print('{date:%Y-%m-%d},{amount},"{payee}","{description}","{notes}"'.format(date=date,
23 payee=payee,
24 amount=amount,
25 description=reference,
26 notes=reference))
27 elif outformat == "qif":
28 print("D{:%Y-%m-%d}".format(date))
29 print("T{}".format(amount))
30 print("P{}".format(payee))
31 print("M{}".format(reference))
32 print("#{}".format(i['feedItemUid']))
33 print("^")
34 except KeyError:
35 raise FeedError(i['feedItemUid'])
36
37
38 if __name__ == "__main__":
39 if outformat == "qif":
40 print("!Type:Bank")
41 cnt = 1
42 with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as fp:
43 data = json.load(fp)
44 for i in data.get('feedItems'):
45 read_feed_item(i)
46 cnt += 1