-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_ofx.py
129 lines (95 loc) · 4.06 KB
/
export_ofx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/env python3
# -*- coding: utf-8 -*-
from qonto_client import QontoClient, QontoOfx, QontoOfxTransaction
import os, argparse, requests, zipfile, json
from datetime import datetime, timezone
import shutil
ID=os.getenv("ID")
IBAN=os.getenv("IBAN")
KEY=os.getenv("KEY")
parser = argparse.ArgumentParser(description="Qonto OFX exporting script")
parser.add_argument('--attachments', action='store_true', help='export attachments')
parser.add_argument('--dir', default=None, help='directory to save ofx file to')
parser.add_argument('--out', default=None, help='directory to save ofx file to')
parser.add_argument('--pretty', action='store_true', help='pretty format ofx file')
parser.add_argument('--json', action='store_true', help='include a json version for reference')
parser.add_argument('--start-date', default=None, help='fetch transactions on or after UTC date expressed as YYYY-MM-DD')
parser.add_argument('--end-date', default=None, help='fetch transactions on or before UTC date expressed as YYYY-MM-DD')
parser.add_argument('--last-month', action='store_true', help='fetch transactions from last completed month')
parser.add_argument('--zip', action='store_true', help='Zip export into a single file')
args = parser.parse_args()
Q = QontoClient(
api_id = ID,
api_key= KEY,
iban = IBAN
)
QO = QontoOfx(iban=IBAN, curdef=Q.currency(), balance=Q.balance(), balancedt=Q.balancedt())
if args.dir == None:
N = datetime.now()
args.dir = "{}-{}-{}_{}-{}-{}_{}_qonto".format(N.year, N.month, N.day, N.hour, N.minute, N.second, ID)
if args.dir != None:
if not os.path.isdir(args.dir):
os.makedirs(args.dir)
attachment_num = 0
filters = {}
if args.last_month:
now = datetime.now()
Y_1 = now.year
M_1 = now.month-1
if M_1 == 0:
M_1 = 12
Y_1 = now.year - 1
args.start_date = "{}-{}-01".format(Y_1, M_1)
args.end_date = "{}-{}-01".format(now.year, now.month)
if args.start_date != None:
Y, M, D = args.start_date.split("-")
filters["settled_at_from"] = datetime(int(Y),int(M),int(D), hour=0, minute=0, second=0, microsecond=0, tzinfo=timezone.utc)
if args.end_date != None:
Y, M, D = args.end_date.split("-")
filters["settled_at_to"] = datetime(int(Y),int(M),int(D), hour=0, minute=0, second=0, microsecond=0, tzinfo=timezone.utc)
if args.json:
J = []
for t in Q.transactions(filters=filters):
QO.add_transaction(QontoOfxTransaction(t))
if args.json:
J.append(t)
if args.attachments:
for url, filename, attachment_id in Q.attachment_urls(t["id"]):
attachment_num+=1
with requests.get(url, stream=True) as fh:
fh.raise_for_status()
fn = "attachment-{}-id-{}-{}".format(attachment_num, attachment_id[0:8], filename)
if args.dir != None:
fn = "{}/{}".format(args.dir, fn)
with open(fn, 'wb') as f:
for chunk in fh.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
ofx = QO.export(pretty=args.pretty)
if args.out == None and args.dir == None :
print(ofx)
else:
outfn = args.out
if args.dir != None:
if args.dir[-1] == "/":
args.dir = args.dir[:-1]
if args.out == None:
outfn = "{}/{}".format(args.dir, os.path.basename(args.dir)+".ofx")
else:
outfn = "{}/{}".format(args.dir, args.out)
with open(outfn, "w") as fh:
fh.write(ofx)
if args.json:
with open(outfn+".json", "w") as fh:
json.dump(J, fh, indent=4)
if args.zip:
zipf = zipfile.ZipFile('{}.zip'.format(args.dir), 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(args.dir):
for file in files:
zipf.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(args.dir, '..')))
zipf.close()
shutil.rmtree(args.dir)