-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexport.py
executable file
·268 lines (199 loc) · 8.09 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
from __future__ import print_function
import os
from getpass import getpass
import re
from datetime import datetime
import argparse
import codecs
import time
import random
from collections import namedtuple
from functools import reduce
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import db
from dateutil import format_tran_date_for_file, format_tran_date_for_qif,\
parse_tran_date
random.seed()
BASE_URL = 'https://28degrees-online.latitudefinancial.com.au/';
WAIT_DELAY = 3
LOGIN_DELAY = 3
Transaction = namedtuple('Transaction',
['date', 'payer', 'amount', 'memo', 'payee'])
export_path = './export'
def messages(before, after_ok, after_fail):
def external_decorator(f):
def wrapped(*args, **kwargs):
print(before)
r = f(*args, **kwargs)
print(after_ok if r else after_fail)
return r
return wrapped
return external_decorator
def get_credentials():
print('Enter your username:')
lines = []
lines.append(input())
lines.append(getpass())
return lines
def get_next_btn(browser):
return browser.find_element(By.NAME, 'nextButton')
def login(creds, captcha):
driver = webdriver.Chrome()
driver.get(BASE_URL)
if captcha:
print('Press enter to continue after Captcha:')
input()
else:
time.sleep(LOGIN_DELAY)
try:
user = driver.find_element(By.NAME, 'USER')
except NoSuchElementException as exception:
exit("Could not find the login screen. Use --captcha option if you need to manually complete a captcha")
user.send_keys(creds[0])
user = driver.find_element(By.NAME, 'PASSWORD')
user.send_keys(creds[1])
btn = driver.find_element(By.NAME, 'SUBMIT')
btn.click()
time.sleep(WAIT_DELAY)
tranLink = driver.find_element(By.XPATH, u'//a[text()="View Transactions"]')
tranLink.click()
nextBtn = get_next_btn(driver)
return driver
def fetch_transactions(driver):
trans = []
rows = driver.find_elements(By.CSS_SELECTOR, 'div[name="transactionsHistory"] tr[name="DataContainer"]')
for row in rows:
dateText = row.find_element(By.CSS_SELECTOR, 'div[name="Transaction_TransactionDate"]').text
date = parse_tran_date(dateText)
payer = row.find_element(By.CSS_SELECTOR, 'div[name="Transaction_CardName"]').text
desc_payee = row.find_element(By.CSS_SELECTOR, 'div[name="Transaction_TransactionDescription"]').text
amount = row.find_element(By.CSS_SELECTOR, 'div[name="Transaction_Amount"]').text
if len(desc_payee) >= 23:
payee = desc_payee[:23]
memo = desc_payee[23:]
else:
payee = desc_payee
memo = ''
# Clean up the data
amount = amount.replace('$', '')
payee = re.sub('\s+', ' ', payee)
memo = re.sub('\s+', ' ', memo)
trans.append(Transaction(date=date,
payer=payer,
amount=amount,
memo=memo,
payee=payee))
return trans
"""See http://en.wikipedia.org/wiki/Quicken_Interchange_Format for more info."""
@messages('Writing QIF file...', 'OK', '')
def write_qif(trans, file_name):
print(file_name)
with codecs.open(file_name, 'w', encoding='utf-8') as f:
# Write header
print('!Account', file=f)
print('NQIF Account', file=f)
print('TCCard', file=f)
print('^', file=f)
print('!Type:CCard', file=f)
for t in trans:
print('C', file=f) # status - uncleared
print('D' + format_tran_date_for_qif(t.date), file=f) # date
print('T' + t.amount, file=f) # amount
print('M' + t.payer, file=f)
print('P' + t.payee + t.memo, file=f)
print('^', file=f) # end of record
@messages('Writing CSV file...', 'OK', '')
def write_csv(trans, file_name):
print(file_name)
with codecs.open(file_name, 'w', encoding='utf-8') as f:
print('Date,Amount,Payer,Payee', file=f)
for t in trans:
print('"%s","%s","%s","%s"' % (format_tran_date_for_qif(t.date), t.amount, t.payer, t.payee), file=f)
def get_file_name(export_path, s_d, e_d, extension):
i = 0
while True:
f_n = os.path.join(export_path, '%s-%s%s.%s' %
(format_tran_date_for_file(s_d),
format_tran_date_for_file(e_d),
'' if i == 0 else '-%s' % i,
extension))
if not os.path.exists(f_n):
return f_n
i += 1
def export(csv, slow, captcha):
print('Use "export.py --help" to see all command line options')
WAIT_DELAY = 3
LOGIN_DELAY = 3
if slow:
WAIT_DELAY = 25
if not os.path.exists(export_path):
os.makedirs(export_path)
t_db = db.init_db()
if not t_db:
print('Error initialising database')
return
creds = get_credentials()
driver = login(creds, captcha)
trans = []
i = 1
while True:
page_trans = fetch_transactions(driver)
trans += page_trans
page_count = len(page_trans)
if page_count == 0:
break;
print('Got %s transactions, from %s to %s' % (page_count,
format_tran_date_for_qif(page_trans[0].date),
format_tran_date_for_qif(page_trans[-1].date)))
print('Opening next page...')
try:
nextButton = get_next_btn(driver)
if not nextButton.is_displayed():
break
nextButton.click()
time.sleep(WAIT_DELAY);
except (NoSuchElementException,) as err:
break
new_trans = db.get_only_new_transactions(trans)
print('Total of %s new transactions obtained' % len(new_trans))
if len(new_trans) != 0:
print('Saving transactions...')
db.save_transactions(new_trans)
s_d = reduce(lambda t1, t2: t1 if t1.date < t2.date else t2, new_trans).date
e_d = reduce(lambda t1, t2: t1 if t1.date > t2.date else t2, new_trans).date
if csv:
file_name = get_file_name(export_path, s_d, e_d, 'csv')
write_csv(new_trans, file_name)
else:
file_name = get_file_name(export_path, s_d, e_d, 'qif')
write_qif(new_trans, file_name)
"""
if statements:
if len(statLink) == 0:
print('Unable to find link to statements page')
return
br.open(statLink[0].attrib['href'])
text = br.response().read()
q = PyQuery(text)
for row in q('a[class="s_downloads"]'):
statement_date = datetime.strptime(row.text, '%d %b %Y').strftime('%Y-%m-%d')
statement_name = '28 Degrees Statement ' + statement_date + '.pdf'
statement_path = os.path.join(export_path, statement_name)
if not os.path.exists(statement_path):
print('Retrieving statement ' + row.text + ' and saving to ' + statement_path)
br.retrieve(row.attrib['href'], statement_path)
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser("""I load transactions from 28degrees-online.latitudefinancial.com.au.
If no arguments specified, I will produce a nice QIF file for you
To get CSV, specify run me with --csv parameter""")
parser.add_argument('--csv', action='store_true', help='Write CSV instead of QIF')
parser.add_argument('--slow', action='store_true', help='Increase wait delay between actions. Use on slow internet connections or when 28degrees is acting up.')
parser.add_argument('--captcha', action='store_true', help='Wait until enter pressed, before login, to allow manual completion of captcha.')
#parser.add_argument('--statements', action='store_true', default=False)
args = parser.parse_args()
export(**vars(args))