]> git.rmz.io Git - dotfiles.git/blob - bin/starling.py
vim: do not set pastetoggle in nvim
[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 if i.get("status", "") == "DECLINED":
16 return
17 date = datetime.strptime(i["transactionTime"], "%Y-%m-%dT%H:%M:%S.%f%z")
18 amount = i["amount"]["minorUnits"] / 100
19 if i["direction"] == "OUT":
20 amount = -amount
21 payee = i["counterPartyName"]
22 if "counterPartySubEntityName" in i:
23 payee += " ({})".format(i["counterPartySubEntityName"])
24 reference = i.get("reference", payee)
25 if outformat == "csv":
26 print('{date:%Y-%m-%d},{amount},"{payee}","{description}","{notes}"'.format(date=date,
27 payee=payee,
28 amount=amount,
29 description=reference,
30 notes=reference))
31 elif outformat == "qif":
32 print("D{:%Y-%m-%d}".format(date.astimezone(None)))
33 print("T{}".format(amount))
34 print("P{}".format(payee))
35 print("M{}".format(reference))
36 print("#{}".format(i['feedItemUid']))
37 print("^")
38 except KeyError:
39 raise FeedError(i['feedItemUid'])
40
41
42 if __name__ == "__main__":
43 if outformat == "qif":
44 print("!Account")
45 print("NCurrent Accounts:Starling")
46 print("^")
47 print("!Type:Bank")
48 cnt = 1
49 with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as fp:
50 data = json.load(fp)
51 for i in data.get('feedItems'):
52 read_feed_item(i)
53 cnt += 1