]> git.rmz.io Git - dotfiles.git/blob - bin/starling.py
7f4615071c77e142d72a8638584d6651b8b393b0
[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 reference = i.get("reference", payee)
23 if outformat == "csv":
24 print('{date:%Y-%m-%d},{amount},"{payee}","{description}","{notes}"'.format(date=date,
25 payee=payee,
26 amount=amount,
27 description=reference,
28 notes=reference))
29 elif outformat == "qif":
30 print("D{:%Y-%m-%d}".format(date.astimezone(None)))
31 print("T{}".format(amount))
32 print("P{}".format(payee))
33 print("M{}".format(reference))
34 print("#{}".format(i['feedItemUid']))
35 print("^")
36 except KeyError:
37 raise FeedError(i['feedItemUid'])
38
39
40 if __name__ == "__main__":
41 if outformat == "qif":
42 print("!Account")
43 print("NCurrent Accounts:Starling")
44 print("^")
45 print("!Type:Bank")
46 cnt = 1
47 with open(sys.argv[1], 'r') if len(sys.argv) > 1 else sys.stdin as fp:
48 data = json.load(fp)
49 for i in data.get('feedItems'):
50 read_feed_item(i)
51 cnt += 1