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
|
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Johann Broudin
#
# This file is part of weboob.
#
# weboob 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.
#
# weboob 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 weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.bank import ICapBank, AccountNotFound
from weboob.capabilities.bank import Account, Transaction
from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.value import ValueBackendPassword
from weboob.capabilities.base import NotAvailable
from weboob.tools.browser import BrowserIncorrectPassword, BrokenPageError
from re import match, compile, sub
from httplib import HTTPSConnection
from urllib import urlencode
from decimal import Decimal
from lxml import etree
from datetime import date
from StringIO import StringIO
__all__ = ['CmbBackend']
class CmbBackend(BaseBackend, ICapBank):
NAME = 'cmb'
MAINTAINER = 'Johann Broudin'
EMAIL = 'Johann.Broudin@6-8.fr'
VERSION = '0.c'
LICENSE = 'AGPLv3+'
DESCRIPTION = u'Crédit Mutuel de Bretagne French bank website'
CONFIG = BackendConfig(
ValueBackendPassword('login', label='Account ID', masked=False),
ValueBackendPassword('password', label='Password', masked=True))
LABEL_PATTERNS = [
( # card
compile('^CARTE (?P<text>.*)'),
Transaction.TYPE_CARD,
'%(text)s'
),
( # order
compile('^PRLV (?P<text>.*)'),
Transaction.TYPE_ORDER,
'%(text)s'
),
( # withdrawal
compile('^RET DAB (?P<text>.*)'),
Transaction.TYPE_WITHDRAWAL,
'%(text)s'
),
( # loan payment
compile('^ECH (?P<text>.*)'),
Transaction.TYPE_LOAN_PAYMENT,
'%(text)s'
),
( # transfer
compile('^VIR (?P<text>.*)'),
Transaction.TYPE_TRANSFER,
'%(text)s'
),
( # payback
compile('^ANN (?P<text>.*)'),
Transaction.TYPE_PAYBACK,
'%(text)s'
),
( # bank
compile('^F (?P<text>.*)'),
Transaction.TYPE_BANK,
'%(text)s'
)
]
cookie = None
headers = {
'User-Agent':
'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OSX; en-us) ' +
'AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'
}
def login(self):
params = urlencode({
'codeEspace': 'NO',
'codeEFS': '01',
'codeSi': '001',
'noPersonne': self.config['login'].get(),
'motDePasse': self.config['password'].get()
})
conn = HTTPSConnection("www.cmb.fr")
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
conn.request("POST",
"/domiweb/servlet/Identification",
params,
headers)
response = conn.getresponse()
if response.status == 302:
self.cookie = response.getheader('Set-Cookie').split(';')[0]
self.cookie += ';'
return True
else:
raise BrowserIncorrectPassword()
return False
def iter_accounts(self):
if not self.cookie:
self.login()
def do_http():
conn = HTTPSConnection("www.cmb.fr")
headers = self.headers
headers['Cookie'] = self.cookie
conn.request("GET",
'/domiweb/prive/particulier/releve/0-releve.act',
{},
headers)
response = conn.getresponse()
data = response.read()
conn.close()
return data
data = do_http()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(data), parser)
table = tree.xpath('/html/body/table')
if len(table) == 0:
title = tree.xpath('/html/head/title')[0].text
if title == u"Utilisateur non identifié":
self.login()
data = do_http()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(data), parser)
table = tree.xpath('/html/body/table')
if len(table) == 0:
raise BrokenPageError()
else:
raise BrokenPageError()
for tr in table[1].getiterator('tr'):
if tr.get('class') != 'LnTit' and tr.get('class') != 'LnTot':
account = Account()
td = tr.xpath('td')
a = td[0].xpath('a')
account.label = unicode(a[0].text).strip()
href = a[0].get('href')
m = match(r"javascript:releve\((.*),'(.*)','(.*)'\)",
href)
account.id = unicode(m.group(1) + m.group(2) + m.group(3))
account.cmbvaleur = m.group(1)
account.cmbvaleur2 = m.group(2)
account.cmbtype = m.group(3)
balance = td[1].text
balance = balance.replace(',', '.').replace(u"\xa0", '')
account.balance = Decimal(balance)
span = td[3].xpath('a/span')
if len(span):
coming = span[0].text.replace(' ', '').replace(',', '.')
coming = coming.replace(u"\xa0", '')
account.coming = Decimal(coming)
else:
account.coming = NotAvailable
yield account
def get_account(self, _id):
for account in self.iter_accounts():
if account.id == _id:
return account
raise AccountNotFound()
def iter_history(self, account):
if not self.cookie:
self.login()
page = "/domiweb/prive/particulier/releve/"
if account.cmbtype == 'D':
page += "10-releve.act"
else:
page += "2-releve.act"
page +="?noPageReleve=1&indiceCompte="
page += account.cmbvaleur
page += "&typeCompte="
page += account.cmbvaleur2
page += "&deviseOrigineEcran=EUR"
def do_http():
conn = HTTPSConnection("www.cmb.fr")
headers = self.headers
headers['Cookie'] = self.cookie
conn.request("GET", page, {}, headers)
response = conn.getresponse()
data = response.read()
conn.close
return data
data = do_http()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(data), parser)
tables = tree.xpath('/html/body/table')
if len(tables) == 0:
title = tree.xpath('/html/head/title')[0].text
if title == u"Utilisateur non identifié":
self.login()
data = do_http()
parser = etree.HTMLParser()
tree = etree.parse(StringIO(data), parser)
tables = tree.xpath('/html/body/table')
if len(tables) == 0:
raise BrokenPageError()
else:
raise BrokenPageError()
i = 0
for table in tables:
if table.get('id') != "tableMouvements":
continue
for tr in table.getiterator('tr'):
if (tr.get('class') != 'LnTit' and
tr.get('class') != 'LnTot'):
operation = Transaction(i)
td = tr.xpath('td')
div = td[1].xpath('div')
d = div[0].text.split('/')
operation.date = date(*reversed([int(x) for x in d]))
div = td[2].xpath('div')
label = div[0].xpath('a')[0].text.replace('\n', '')
operation.raw = unicode(' '.join(label.split()))
for pattern, _type, _label in self.LABEL_PATTERNS:
mm = pattern.match(operation.raw)
if mm:
operation.type = _type
operation.label = sub('[ ]+', ' ', _label % mm.groupdict()).strip()
break
amount = td[3].text
if amount.count(',') != 1:
amount = td[4].text
amount = amount.replace(',', '.').replace(u'\xa0', '')
operation.amount = Decimal(amount)
else:
amount = amount.replace(',', '.').replace(u'\xa0', '')
operation.amount = - Decimal(amount)
i += 1
yield operation
|