-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch.py
executable file
·234 lines (199 loc) · 7.18 KB
/
fetch.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
#!/usr/bin/env python
"""Downloads bank account data from banking websites.
Supported banks:
* Deutsche Kreditbank http://www.dkb.de/
* PostFinance http://www.postfinance.ch/
* Interactive Brokers http://www.interactivebrokers.com/
With inspiration from Jens Herrmann's web_bank.py (http://qoli.de).
For more information see http://github.com/thowi/pybank.
"""
import datetime
import logging
import getopt
import re
import sys
import download.dkb
import download.ib
import download.postfinance
import download.revolut
import qif
BANK_BY_NAME = {
'dkb': download.dkb.DeutscheKreditBank,
'interactivebrokers': download.ib.InteractiveBrokers,
'postfinance': download.postfinance.PostFinance,
'revolut': download.revolut.Revolut,
}
DATE_FORMAT = '%Y-%m-%d'
INVALID_FILENAME_CHARACTERS_PATTERN = re.compile(r'[^a-zA-Z0-9-_.]')
LOG_FORMAT = '%(message)s'
LOG_FORMAT_DEBUG = '%(levelname)s %(name)s: %(message)s'
logger = logging.getLogger(__name__)
class Usage(Exception):
"""Usage: bank.py
[-h|--help]
[-b bank|--bank=bank]
[-u username|--username=username]
[-p password|--password=password]
[-a account|--account=account] Can be repeated. Default: All accounts.
[-f YYYY-MM-DD|--from=YYYY-MM-DD] From (inclusive). Default: First day of last month.
[-t YYYY-MM-DD|--till=YYYY-MM-DD] Until (exclusive). Default: First day of this month..
[-o outfile|--outfile=outfile] Default: STDOUT.
Variables will be replaced: %(bank)s %(account)s %(from)s %(till)s
[-d|--debug]
"""
def __init__(self, msg=''):
self.msg = msg
def __str__(self):
banks = 'Available banks: %s.' % ', '.join(sorted(BANK_BY_NAME.keys()))
return '\n'.join((self.__doc__, self.msg, banks))
def _parse_args(argv):
bank_name = None
username = None
password = None
accounts = []
statements = []
from_date = None
till_date = None
output_filename = None
debug = False
options = 'hb:u:a:p:s:f:t:o:d'
options_long = [
'help', 'bank=', 'username=', 'password=', 'account=',
'statements=', 'from=', 'till=', 'outfile=', 'debug']
try:
opts, unused_args = getopt.getopt(argv[1:], options, options_long)
except getopt.error as msg:
raise Usage(msg)
for opt, arg in opts:
if opt in ('-h', '--help'):
print(Usage())
return 0
if opt in ('-b', '--bank'):
bank_name = arg
if opt in ('-u', '--username'):
username = arg
if opt in ('-p', '--password'):
password = arg
if opt in ('-a', '--account'):
accounts.append(arg)
if opt in ('-s', '--statements'):
statements.append(arg)
if opt in ('-f', '--from'):
from_date = arg
if opt in ('-t', '--till'):
till_date = arg
if opt in ('-o', '--outfile'):
output_filename = arg
if opt in ('-d', '--debug'):
debug = True
if not bank_name:
raise Usage('Must specify a bank.')
if bank_name not in BANK_BY_NAME:
raise Usage('Unknown bank: %s.', bank_name)
if from_date:
try:
from_date = datetime.datetime.strptime(from_date, DATE_FORMAT)
except ValueError:
raise Usage('Invalid from date: %s.', from_date)
else:
# Beginning of last month.
now = datetime.datetime.now()
if now.month == 1:
last_month = now.replace(year=now.year-1, month=12, day=1)
else:
last_month = now.replace(month=now.month-1, day=1)
from_date = datetime.datetime(last_month.year, last_month.month, 1)
if till_date:
try:
till_date = datetime.datetime.strptime(till_date, DATE_FORMAT)
except ValueError:
raise Usage('Invalid until date: %s.', till_date)
else:
# Beginning of this month.
now = datetime.datetime.now()
till_date = datetime.datetime(now.year, now.month, 1)
return (
bank_name, username, password, accounts, statements,
from_date, till_date, output_filename, debug)
def _fetch_accounts(
bank_name, username, password, account_names, statements,
from_date, till_date, output_filename, debug):
bank_class = BANK_BY_NAME[bank_name]
bank = bank_class(debug)
bank.login(username=username, password=password, statements=statements)
available_accounts = bank.get_accounts()
if not available_accounts:
logger.warning('No accounts found.')
return
logger.info(
'Available accounts: %s.',
', '.join(str(a) for a in available_accounts))
if not account_names:
# Download all accounts by default.
accounts = available_accounts
else:
accounts_by_name = {}
for account in available_accounts:
accounts_by_name[account.name] = account
accounts = []
for account_name in account_names:
try:
accounts.append(accounts_by_name[account_name])
except KeyError:
logger.error('Account not found: %s.', account_name)
for account in accounts:
logger.info('Fetching account: %s.', account.name)
account.transactions = bank.get_transactions(
account, from_date, till_date)
output = _open_file(
output_filename, bank_name, account.name, from_date, till_date)
try:
print(qif.serialize_account(account), file=output)
except qif.SerializationError as e:
logger.error('Serialization error: %s.', e)
logout = input('Logout? [yN] ')
if logout == 'y':
bank.logout()
def _open_file(output_filename, bank_name, account_name, from_date, till_date):
if not output_filename:
return sys.stdout
filename_vars = {
'bank': bank_name, 'account': account_name,
'from': from_date.strftime(DATE_FORMAT),
'till': till_date.strftime(DATE_FORMAT),
}
formatted_filename = output_filename % filename_vars
escaped_filename = INVALID_FILENAME_CHARACTERS_PATTERN.sub(
'_', formatted_filename)
try:
logger.info('Writing to file: %s.', escaped_filename)
return open(escaped_filename, 'w')
except IOError as err:
print(err.msg, file=sys.stderr)
def main(argv=None):
if argv is None:
argv = sys.argv
try:
(bank_name, username, password, accounts, statements,
from_date, till_date, output_filename, debug) = _parse_args(argv)
except Usage as err:
print(err, file=sys.stderr)
return 2
if debug:
logging.basicConfig(format=LOG_FORMAT_DEBUG, level=logging.DEBUG)
else:
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
try:
_fetch_accounts(
bank_name, username, password, accounts, statements,
from_date, till_date, output_filename, debug)
except (KeyboardInterrupt, SystemExit):
raise
except Exception as e:
logger.error('Error while fetching transactions: %s' % e)
if debug:
import pdb; pdb.post_mortem()
return 2
return 0
if __name__ == '__main__':
sys.exit(main())