This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
forked from lecagot/dkbweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dkbweb.py
executable file
·292 lines (237 loc) · 9.97 KB
/
dkbweb.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# DKB transaction CSV exporter
# Copyright (C) 2015 Tom Fischer (https://plus.google.com/+TomFischer0810)
#
# Inspired by Christian Hoffmann <[email protected]>,
# see https://github.com/hoffie/dkb-visa,
# but extended to fetch also bank account transactions. You still need to
# have dkb.py module in the same directory as dkbweb.py.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import dkb
import re
import csv
import sys
import logging
from bs4 import BeautifulSoup
from mechanize import Browser
DEBUG = False
logger = logging.getLogger(__name__)
class DkbScraper(dkb.DkbScraper):
LOGOUTURL = "https://banking.dkb.de/dkb/-?$part=DkbTransactionBanking.infobar.logout-button&$event=logout"
def bank_account_transactions_overview(self):
"""
Navigates the internal browser state to the bank account
transaction overview menu
"""
logger.info("Navigating to 'Kontoumsätze'...")
try:
return self.br.follow_link(url_regex='banking/finanzstatus/kontoumsaetze')
except Exception:
raise RuntimeError('Unable to find link Umsätze -- '
'Maybe the login went wrong?')
def _get_transaction_selection_form_ba(self):
"""
Internal.
Returns the transaction selection form object (mechanize)
"""
for form in self.br.forms():
try:
form.find_control(name="slAllAccounts")
return form
except Exception:
continue
raise RuntimeError("Unable to find transaction selection form")
def _select_all_transactions_from_ba(self, form, from_date, to_date):
"""
Internal.
Checks the radio box "Zeitraum: vom" and populates the
"from" and "to" with the given values.
@param mechanize.HTMLForm form
@param str from_date dd.mm.YYYY
@param str to_date dd.mm.YYYY
"""
try:
radio_ctrl = form.find_control("searchPeriodRadio")
except Exception:
raise RuntimeError("Unable to find search period radio box")
all_transactions_item = None
for item in radio_ctrl.items:
if item.id.endswith(":1"):
all_transactions_item = item
break
if not all_transactions_item:
raise RuntimeError(
"Unable to find 'Zeitraum: vom' radio box")
form[radio_ctrl.name] = ["1"] # select from/to date, not "all"
try:
from_item = form.find_control(name="transactionDate")
except Exception:
raise RuntimeError("Unable to find 'vom' (from) date field")
from_item.value = from_date
try:
to_item = form.find_control(name="toTransactionDate")
except Exception:
raise RuntimeError("Unable to find 'bis' (to) date field")
to_item.value = to_date
def _select_bank_account(self, form, baid):
"""
Internal.
Selects the correct bank account from the dropdown menu in the
transaction selection form.
@param mechanize.HTMLForm form
@param str baid: full bank account number
"""
try:
ba_list = form.find_control("slAllAccounts", type="select")
except Exception:
raise RuntimeError("Unable to find bank account selection form")
for item in ba_list.get_items():
# find right bank account...
for label in item.get_labels():
bapattern = r'\b[A-Z]{2}\d{16}%s\b' % re.escape(baid)
if re.search(bapattern, label.text, re.I):
form[ba_list.name] = [item.name]
return
raise RuntimeError("Unable to find the right bank account")
def select_transactions_ba(self, baid, from_date, to_date):
"""
Changes the current view to show all transactions between
from_date and to_date for the bank account identified by the
given full bank account number.
@param str baid: full bank account number
@param str from_date dd.mm.YYYY
@param str to_date dd.mm.YYYY
"""
br = self.br
logger.info("Selecting transactions in time frame %s - %s...",
from_date, to_date)
br.form = form = self._get_transaction_selection_form_ba()
self._select_bank_account(form, baid)
self._select_all_transactions_from_ba(form, from_date, to_date)
br.submit()
def logout(self):
"""
Performs the logout process so that the session is closed on the DKB server.
"""
self.br.open(self.LOGOUTURL)
if __name__ == '__main__':
"""
Added a switch for bank account instead of credit card. That makes
`--cardid` optional but one of them must be given.
"""
from getpass import getpass
from argparse import ArgumentParser
from datetime import date
logging.basicConfig(level=logging.DEBUG)
cli = ArgumentParser()
cli.add_argument("--userid",
help="Your user id (same as used for login)")
cli.add_argument("--pin",
help="Your user PIN (same as used for login). Use with care!")
cli.add_argument("--baid",
help="Full bank account number")
cli.add_argument("--cardid",
help="Last 4 digits of your card number")
cli.add_argument("--output", "-o",
help="Output path (QIF)")
cli.add_argument("--qif-account",
help="Default QIF account name (e.g. Aktiva:VISA)")
cli.add_argument("--from-date",
help="Export transactions as of... (DD.MM.YYYY)")
cli.add_argument("--to-date",
help="Export transactions until... (DD.MM.YYYY)",
default=date.today().strftime('%d.%m.%Y'))
cli.add_argument("--raw", action="store_true",
help="Store the raw CSV file instead of QIF")
args = cli.parse_args()
if not args.userid:
cli.error("Please specify a valid user id")
if (not args.baid and not args.cardid) or (args.baid and args.cardid): # at least one must be given but both are not allowed
cli.error("Please specify a valid bank account number _or_ card id")
def is_valid_date(date):
return date and bool(re.match('^\d{1,2}\.\d{1,2}\.\d{2,5}\Z', date))
from_date = args.from_date
while not is_valid_date(from_date):
from_date = raw_input("Start time: ")
if not is_valid_date(args.to_date):
cli.error("Please specify a valid end time")
if not args.output:
cli.error("Please specify a valid output path")
if args.pin:
pin = args.pin
else:
pin = ""
import os
if os.isatty(0):
while not pin.strip():
pin = getpass('PIN: ')
else:
pin = sys.stdin.read().strip()
fetcher = DkbScraper()
if DEBUG:
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
#fetcher.br.set_debug_http(True)
fetcher.br.set_debug_responses(True)
#fetcher.br.set_debug_redirects(True)
fetcher.login(args.userid, pin)
if args.baid:
fetcher.bank_account_transactions_overview()
fetcher.select_transactions_ba(args.baid, from_date, args.to_date)
elif args.cardid:
fetcher.credit_card_transactions_overview()
fetcher.select_transactions(args.cardid, from_date, args.to_date)
csv_text = fetcher.get_transaction_csv()
if args.raw:
if args.output == '-':
f = sys.stdout
else:
f = open(args.output, 'w')
f.write(csv_text)
else:
dkb2qif = dkb.DkbConverter(csv_text, cc_name=args.qif_account)
# Length of the pre-amble (non-CSV headers), including the
# CSV head line
dkb2qif.SKIP_LINES = 7
# Column Definitions: Which values can be found in which columns?
# see: <https://bazaar.launchpad.net/~mdoyen/homebank/5.1.x/view/head:/src/hb-import-qif.c>
# and dkb.py
dkb2qif.COL_DATE = 0 # Buchungstag
dkb2qif.COL_PAYEE = 3 # Begünstigter # included as optional
dkb2qif.COL_VALUTA_DATE = 1 # Wertstellung
dkb2qif.COL_DESC = 4 # COL_DESC = Memo1 Verwendungszweck
dkb2qif.COL_VALUE = 7 # Betrag
dkb2qif.COL_TEXT = 2 # continue, if this field (Buchungstext) has the value "Abschluss" (Girokonto)
#dkb2qif.COL_INFO = 4 # COL_INFO = Memo2 Verwendungszweck (?) # included as optional
# Number of fields for the line to be recognized as a valid
# transaction line:
dkb2qif.REQUIRED_FIELDS = 5
dkb2qif.export_to(args.output)
fetcher.logout()
"""
Testing:
1. Everything you could do with dkb.py, you still can do exactly the same way:
./dkbweb.py --userid USER --cardid 1234 --from-date 01.01.2015 --output cc.csv --raw
2. With dkbweb.py you can also query your bank account transactions:
./dkbweb.py --userid USER --baid 1234567890 --from-date 01.01.2015 --output ba.csv --raw
3. With dkbweb.py you can also directly submit your PIN to the command line:
./dkbweb.py --userid USER --pin 5x6y7z --cardid 1234 --output cc.qif
4. Also, dkbweb.py logs out the user before quitting the application.
Use with care! This is very insecure because your PIN is submitted in plain text.
But this can be very handy for automation (batch scripting).
Use only if you know what you're doing!
"""