-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexport.py
executable file
·123 lines (85 loc) · 3.38 KB
/
export.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
#!/usr/bin/env python
from datetime import timedelta
from lib import db, browser, qif
import sys
def get_last_transaction_date(b, account):
today = browser.get_servers_today_date(b)
# It looks like NAB only lets you get transactions for last 560 days
# (ancient back-end restriction, I suppose?)
MAX_HISTORY_DAYS = 560
last_date = db.get_last_transaction_date(account['bsb'], account['acc_no'])
if not last_date:
print('\tWe don\'t seem to have any transactions for account \'%s\' in database.' %
account['name'])
print('\tThat\'s OK though! Let\'s retrieve transactions for last %s days' %
MAX_HISTORY_DAYS)
last_date = today - timedelta(days=MAX_HISTORY_DAYS)
else:
print('\tAccount %(acc)s has some transactions, so just get the new ones...' %
{'acc': account['acc_no']})
if (today - last_date).days > MAX_HISTORY_DAYS:
print('Looks like the oldest transaction in the DB is older than %s days' %
MAX_HISTORY_DAYS)
print('Retrieving transactions for only last %s days' %
MAX_HISTORY_DAYS)
last_date = today - timedelta(days=MAX_HISTORY_DAYS)
return last_date
def remove_pending_transactions(trans):
res = []
payees_ignore = ['EFTPOS DEBIT PURCH', 'EFTPOS DEBIT PURCHASE-FLEXIPAY']
for t in trans:
if t['payee'] in payees_ignore or\
t['memo'] == 'MISCELLANEOUS DEBIT DEBIT':
print('\tSkipping transaction %s' % t)
else:
res.append(t)
return res
def exclude_existing_in_db_trans(bsb, acc_n, trans):
res = []
for t in trans:
if not db.is_transaction_in_db(bsb, acc_n, t):
res.append(t)
return res
def export(options):
"""
options: an array of cli options
"""
db.init_db()
if not db:
return
b = browser.login()
if not b:
return
response = b.response().read()
accounts = browser.get_accounts(response)
if not accounts:
return
for account in accounts:
account['acc_no'] = account['acc_no'].replace('-', '')
account['bsb'] = account['bsb'].replace('-', '')
print('\nProcessing account \'%s\' (BSB: %s Number: %s)' % (
account['name'], account['bsb'], account['acc_no']))
b = browser.open_account_transactions_page(b, account['params'])
if not b:
return
last_date = get_last_transaction_date(b, account)
trans = browser.get_all_transactions(b, account, last_date)
trans = remove_pending_transactions(trans)
trans = exclude_existing_in_db_trans(account['bsb'],
account['acc_no'],
trans)
if not trans:
print "\n\tNo transactions saved for the account."
continue
db.save_transactions(account['name'],
account['bsb'],
account['acc_no'],
trans)
if "--no-qif" not in options:
qif.save_qif_file(account['name'],
account['bsb'],
account['acc_no'],
trans)
print('\n\tSaved %s transactions' % len(trans))
if __name__ == "__main__":
export(sys.argv)