-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswedishbanks.py
95 lines (78 loc) · 3.83 KB
/
swedishbanks.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
import re
import time
import urllib
import urllib2
class LoginError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ParseError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class _Bank():
def __init__(self, username, password):
self.username = username
self.password = password
self.accounts = []
self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor)
def __str__(self):
return self
def _urlopen(self, url, data=None, headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.2) Gecko/2008091620 Firefox/3.0.2', 'Connection':'Keep-Alive', 'Content-Type':'application/x-www-form-urlencoded'}):
if hasattr(data, "__iter__"):
data = urllib.urlencode(data)
return self._opener.open(urllib2.Request(url, data, headers))
def _str2num(self, s):
"""Convert string to either int or float."""
s = s.replace(".","").replace(",",".").replace(" ","").replace(" ","")
try:
return int(s)
except ValueError:
return float(s)
class Nordea(_Bank):
def __init__(self, username, password):
_Bank.__init__(self, username, password)
self.update()
def update(self):
"""Update the accounts information."""
params = dict(kundnr=self.username, pinkod=self.password, OBJECT='TT00', prev_link='https://gfs.nb.se/privat/bank/login_kod2.html', CHECKCODE='checkcode')
self._urlopen("https://gfs.nb.se/bin2/gfskod?OBJECT=KK20") #Get cookies
data = self._urlopen("https://gfs.nb.se/bin2/gfskod", params).read()
if not "reDirect" in data:
raise LoginError("Wrong username or password.")
data = self._urlopen("https://gfs.nb.se/bin2/gfskod?OBJECT=KF00T&show_button=No").read()
data = data.replace(chr(10),"").replace(chr(13),"").replace(" ","")
balance = re.findall(r'(?is)nowrap>(.+?)SEK<',data)
account = re.findall(r"(?is)Kontoutdraget';.*?>(.*?)</a></td>",data)
if not (balance and account):
raise ParseError('Unable to parse data.')
self.accounts = []
for x in range(len(account)):
self.accounts.append((account[x].capitalize(), self._str2num(balance[x]), "SEK"))
return self
class Swedbank(_Bank):
def __init__(self, username, password):
_Bank.__init__(self, username, password)
self.update()
def update(self):
"""Update the accounts information."""
data = self._urlopen("https://mobilbank.swedbank.se/banking/swedbank-light/login.html").read()
#Find the Cross-site request forgery token
m = re.search(r'csrf_token"\s*value="(?P<csrftoken>[^"]+)"', data, re.I)
if not m:
raise ParseError('Unable to find CSRF token.')
csrftoken = m.group("csrftoken")
params = dict(xyz=self.username, zyx=self.password, _csrf_token=csrftoken)
data = self._urlopen("https://mobilbank.swedbank.se/banking/swedbank/login.html", params).read()
if "misslyckats" in data:
raise LoginError("Wrong username or password.")
data = self._urlopen("https://mobilbank.swedbank.se/banking/swedbank-light/accounts.html").read().replace('\xa0','')
accounts = re.finditer(r'<span.*?/span>(?P<account>[^<]+) <.*?secondary">(?P<balance>[0-9 .,-]+)</span', data, re.I)
if not accounts:
raise ParseError('Unable to parse data.')
self.accounts = []
for account in accounts:
self.accounts.append((account.group("account").capitalize(), self._str2num(account.group("balance")), "SEK"))
return self