forked from andsor/VB-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbscraper.py
286 lines (209 loc) · 7.86 KB
/
vbscraper.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
#!/usr/bin/env python
# encoding: utf-8
# SPDX-License-Identifier: CC0-1.0
# This file is in the Public Domain as specified by
# http://creativecommons.org/publicdomain/zero/1.0/
# grabbed from https://github.com/andsor/VB-Scraper (Andreas Sorge)
# and slightly modified
import requests
import lxml.html
import bs4
import collections
import datetime
import tempfile
import shutil
import os
import os.path
html_parser = "html.parser" # let BeautifulSoup choose html parser
PostboxDocument = collections.namedtuple(
'PostboxDocument',
[
'msg_date',
'msg_type',
'is_new',
'url',
'subject',
'postbox_page',
]
)
class VBSession(object):
"""
Volksbank Online Banking Session
"""
def __init__(self, base_url, bank_id, verbose=True):
# Initialize HTTP session
self.s = requests.Session()
self.base_url = base_url
self.bank_id = bank_id
self.login_url = '/ptlweb/WebPortal?bankid={}'.format(self.bank_id)
self.verbose = verbose
def login(self, username, password):
"""
Login to Volksbank Online Banking
"""
if self.verbose:
print('Login to Volksbank Online Banking')
# Get Volksbank Banking login page
r = self.s.get(self.base_url + self.login_url)
login_page = lxml.html.fromstring(r.text)
# Get Volksbank Banking login form
login_form = login_page.forms[0]
# Fill in username and password
login_data = dict(login_form.fields)
login_data['pruefenPIN_V01_VO.strVrKennungOderAlias'] = username
login_data['pruefenPIN_V01_VO.txtKkdPwTrp'] = password
login_data['event___login'] = 'Login'
# Post login
r = self.s.post(
self.base_url + login_form.action,
data=login_data
)
# Parse returned page
soup = bs4.BeautifulSoup(r.text, html_parser)
if not soup.find(text='Finanzstatus'):
raise RuntimeError('Login to Volksbank Online Banking failed.')
self.logout_url = soup.find('a', id='ummelden')['href']
self.postbox_url = soup.find('a', text='Postkorb')['href']
if self.verbose:
print('Logged in to Volksbank Online Banking')
return True
def logout(self):
"""
Logout from Volksbank Online Banking
"""
if self.verbose:
print('Log out from Volksbank Online Banking')
r = self.s.get(self.base_url + self.logout_url)
ret = r.status_code == 200
self.s.close()
return ret
def postbox_items(self):
"""
Iterate through the postbox items
"""
if self.verbose:
print("Access postbox")
# Get Postbox page
r = self.s.get(self.base_url + self.postbox_url)
# Parse Postbox page
soup = bs4.BeautifulSoup(r.text, html_parser)
# Get number of pages
li = soup.find('li', attrs={'class': 'gad-paginationActivePageNumber'})
if not li.text.strip() == '1':
raise RuntimeError('Postbox does not start with page 1')
# loop through postbox pages
ret = []
while True:
# Get current page number
li = soup.find(
'li', attrs={'class': 'gad-paginationActivePageNumber'}
)
page_number = li.text.strip()
if self.verbose:
print("Postbox page {}".format(page_number))
# loop through documents on the current page
for tr in soup.table.tbody.findChildren('tr'):
is_new = 'gad-tableEntryHighlighted' in tr['class']
msg_date = datetime.datetime.strptime(
tr.find(
'td', attrs={'class': 'gad-dateColumn'}
).text.strip(),
"%d.%m.%Y %H:%M"
)
a = tr.find(
'td', attrs={'class': 'gad-textColumn'}
).a
url = a['href']
subject = ' '.join([
str2 for str2 in [
str.strip() for str in a.text.split('\n')
] if str2
])
msg_type = tr.find(
'td', attrs={'class': 'gad-textColumn'}
).a.span.text
ret.append(PostboxDocument(
msg_date=msg_date,
msg_type=msg_type,
url=url,
is_new=is_new,
subject=subject,
postbox_page=page_number,
))
# terminate loop if this was the last page of the postbox
a_next_page = soup.find('a', title='nächste Seite')
if a_next_page.get('disabled'):
break
# get next page
r = self.s.get(self.base_url + a_next_page['href'])
# Parse Postbox page
soup = bs4.BeautifulSoup(r.text, html_parser)
return ret
def download_document(self, document, destinations):
"""
Download a document
"""
if self.verbose:
print("Access postbox")
# Get Postbox page
r = self.s.get(self.base_url + self.postbox_url)
# Parse Postbox page
soup = bs4.BeautifulSoup(r.text, html_parser)
# Get current page number
li = soup.find(
'li', attrs={'class': 'gad-paginationActivePageNumber'}
)
page_number = li.text.strip()
if not page_number:
raise RuntimeError('Postbox current page number not found')
if not document.postbox_page == page_number:
# get postbox page
if self.verbose:
print("Access postbox page {}".format(document.postbox_page))
while not li.text.strip() == document.postbox_page:
if li.find_next_sibling():
li = li.find_next_sibling()
else: # page not in current scope, move on to last found page and look again
self.postbox_url = li.a['href']
self.download_document(document, destinations)
return
page_url = li.a['href']
r = self.s.get(self.base_url + page_url)
assert r.status_code == 200
# get message page
if self.verbose:
print(u"Download document {}".format(document.subject))
r = self.s.get(self.base_url + document.url)
# parse message page
soup = bs4.BeautifulSoup(r.text, html_parser)
# subject = msg_soup.find(
# 'label', attrs={'for': 'messageSenderSubject'}
# ).parent.find_next_sibling().span.text
attachment_a = soup.find(
'a', title='Anhang öffnen'
)
if not attachment_a: # message in postbox has no attachment to download
return
attachment_url = attachment_a['href']
filename = attachment_a.text
# download document
r = self.s.get(self.base_url + attachment_url, stream=True)
if not r.status_code == 200:
raise RuntimeError('Download failed.')
r.raw.decode_content = True
# copy http data to temporary file
with tempfile.NamedTemporaryFile(delete=False) as fp:
shutil.copyfileobj(r.raw, fp)
tmp_filename = fp.name
# copy file to destinations
for dest in destinations:
dest_filename = os.path.join(dest, filename)
if self.verbose:
print("Copy to {}".format(dest_filename))
if os.path.exists(dest_filename):
print('"{}" already exists'.format(dest_filename))
continue
shutil.copyfile(tmp_filename, dest_filename)
# delete temporary file
os.unlink(tmp_filename)
return True