diff --git a/modules/banqueaccord/__init__.py b/modules/banqueaccord/__init__.py new file mode 100644 index 00000000..3b550258 --- /dev/null +++ b/modules/banqueaccord/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Romain Bignon +# +# 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 . + + +from .backend import BanqueAccordBackend + + +__all__ = ['BanqueAccordBackend'] diff --git a/modules/banqueaccord/backend.py b/modules/banqueaccord/backend.py new file mode 100644 index 00000000..02f20b3d --- /dev/null +++ b/modules/banqueaccord/backend.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Romain Bignon +# +# 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 . + + +from weboob.capabilities.bank import ICapBank +from weboob.tools.backend import BaseBackend, BackendConfig +from weboob.tools.value import ValueBackendPassword + +from .browser import BanqueAccordBrowser + + +__all__ = ['BanqueAccordBackend'] + + +class BanqueAccordBackend(BaseBackend, ICapBank): + NAME = 'banqueaccord' + DESCRIPTION = u'Banque Accord' + MAINTAINER = u'Romain Bignon' + EMAIL = 'romain@weboob.org' + LICENSE = 'AGPLv3+' + VERSION = '0.h' + CONFIG = BackendConfig(ValueBackendPassword('login', label='Account ID', regexp='\d+', masked=False), + ValueBackendPassword('password', label='Password', regexp='\d+')) + + BROWSER = BanqueAccordBrowser + + def create_default_browser(self): + return self.create_browser(self.config['login'].get(), + self.config['password'].get()) + + def iter_accounts(self): + with self.browser: + return [self.browser.get_account()] + + def get_account(self, id): + return self.browser.get_account() + + def iter_history(self, account): + with self.browser: + return self.browser.iter_history(account) diff --git a/modules/banqueaccord/browser.py b/modules/banqueaccord/browser.py new file mode 100644 index 00000000..dd7b2bde --- /dev/null +++ b/modules/banqueaccord/browser.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Romain Bignon +# +# 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 . + + +from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword +from weboob.capabilities.bank import Account + +from .pages import LoginPage, IndexPage, AccountsPage, OperationsPage + + +__all__ = ['BanqueAccordBrowser'] + + +class BanqueAccordBrowser(BaseBrowser): + PROTOCOL = 'https' + DOMAIN = 'www.banque-accord.fr' + ENCODING = None + + PAGES = { + 'https://www.banque-accord.fr/site/s/login/login.html': LoginPage, + 'https://www.banque-accord.fr/site/s/detailcompte/detailcompte.html': IndexPage, + 'https://www.banque-accord.fr/site/s/detailcompte/ongletdetailcompte.html': AccountsPage, + 'https://www.banque-accord.fr/site/s/detailcompte/ongletdernieresoperations.html': OperationsPage, + } + + def is_logged(self): + return self.page is not None and not self.is_on_page(LoginPage) + + def home(self): + if self.is_logged(): + self.location('/site/s/detailcompte/detailcompte.html') + else: + self.login() + + def login(self): + """ + Attempt to log in. + Note: this method does nothing if we are already logged in. + """ + assert isinstance(self.username, basestring) + assert isinstance(self.password, basestring) + + if self.is_logged(): + return + + self.location('/site/s/login/login.html', no_login=True) + assert self.is_on_page(LoginPage) + + self.page.login(self.username, self.password) + + if not self.is_logged(): + raise BrowserIncorrectPassword() + + + def get_account(self): + if not self.is_on_page(IndexPage): + self.home() + + account = Account() + account.id = '0' + account.label = self.page.get_card_name() + + self.location('/site/s/detailcompte/ongletdetailcompte.html') + account.balance = self.page.get_balance() + + return account + + def iter_history(self, account): + self.location('/site/s/detailcompte/ongletdernieresoperations.html') + + assert self.is_on_page(OperationsPage) + return self.page.get_history() diff --git a/modules/banqueaccord/pages.py b/modules/banqueaccord/pages.py new file mode 100644 index 00000000..fc94c561 --- /dev/null +++ b/modules/banqueaccord/pages.py @@ -0,0 +1,130 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Romain Bignon +# +# 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 . + + +from decimal import Decimal +import re + +from weboob.tools.browser import BasePage +from weboob.tools.captcha.virtkeyboard import MappedVirtKeyboard, VirtKeyboardError +from weboob.tools.capabilities.bank.transactions import FrenchTransaction + + +__all__ = ['LoginPage', 'IndexPage', 'AccountsPage', 'OperationsPage'] + + +class VirtKeyboard(MappedVirtKeyboard): + symbols={'0':('8664b9cdfa66b4c3a1ec99c35a2bf64b','9eb80c6e99410eaac32905b2c77e65e5'), + '1':('1f36986f9d27dde54ce5b08e8e285476','9d0aa7a0a2bbab4f2c01ef1e820cb3f1'), + '2':('b560b0cce2ca74d3d499d73775152ab7',), + '3':('d16e426e71fc29b1b55d0fbded99a473',), + '4':('19c68066e414e08d17c86fc5c4acc949','c43354a7f7739508f76c538d5b3bce26'), + '5':('4b9abf98e30a1475997ec770cbe5e702',), + '6':('804be4171d61f9cc10e9978c43b1d2a0','a41b091d4a11a318406a5a8bd3ed3837'), + '7':('8adf951f4eea5f446f714214e101d555',), + '8':('568135f3844213c30f2c7880be867d3d',), + '9':('a3750995c511ea1492ac244421109e77','eeb3a8ba804f19380dfe94a91a37595b'), + } + + color=(0,0,0) + + def __init__(self, page): + img = page.document.find("//img[@usemap='#cv']") + img_file = page.browser.openurl(img.attrib['src']) + MappedVirtKeyboard.__init__(self, img_file, page.document, img, self.color, 'href', convert='RGB') + + self.check_symbols(self.symbols, page.browser.responses_dirname) + + def check_color(self, pixel): + for p in pixel: + if p >= 0xd5: + return False + + return True + + def get_symbol_coords(self, (x1, y1, x2, y2)): + # strip borders + return MappedVirtKeyboard.get_symbol_coords(self, (x1+10, y1+10, x2-10, y2-10)) + + def get_symbol_code(self, md5sum_list): + for md5sum in md5sum_list: + try: + code = MappedVirtKeyboard.get_symbol_code(self,md5sum) + except VirtKeyboardError: + continue + else: + return ''.join(re.findall("'(\d+)'", code)[1:]) + raise VirtKeyboardError('Symbol not found') + + def get_string_code(self, string): + code = '' + for c in string: + code += self.get_symbol_code(self.symbols[c]) + return code + +class LoginPage(BasePage): + def login(self, login, password): + vk = VirtKeyboard(self) + + form = self.document.xpath('//form[@id="formulaire-login"]')[0] + self.browser.location(self.browser.buildurl(form.attrib['action'], identifiant=login, code=vk.get_string_code(password)), no_login=True) + +class IndexPage(BasePage): + def get_card_name(self): + return self.parser.tocleanstring(self.document.xpath('//h1')[0]) + +class AccountsPage(BasePage): + def get_balance(self): + balance = Decimal('0.0') + for line in self.document.xpath('//div[@class="detail"]/table//tr'): + try: + left = line.xpath('./td[@class="gauche"]')[0] + right = line.xpath('./td[@class="droite"]')[0] + except IndexError: + #useless line + continue + + if len(left.xpath('./span[@class="precision"]')) == 0 and (left.text is None or not 'total' in left.text.lower()): + continue + + balance -= Decimal(FrenchTransaction.clean_amount(right.text)) + return balance + + +class OperationsPage(BasePage): + def get_history(self): + for tr in self.document.xpath('//div[contains(@class, "mod-listeoperations")]//table/tbody/tr'): + cols = tr.findall('td') + + date = self.parser.tocleanstring(cols[0]) + raw = self.parser.tocleanstring(cols[1]) + + amount = self.parser.tocleanstring(cols[2]) + if len(amount) > 0: + t = FrenchTransaction(0) + t.parse(date, raw) + t.set_amount('-' + amount) + yield t + + debit = self.parser.tocleanstring(cols[3]) + if len(debit) > 0: + t = FrenchTransaction(0) + t.parse(date, raw) + t.set_amount(debit) + yield t diff --git a/modules/banqueaccord/test.py b/modules/banqueaccord/test.py new file mode 100644 index 00000000..fd502aab --- /dev/null +++ b/modules/banqueaccord/test.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Romain Bignon +# +# 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 . + + +from weboob.tools.test import BackendTest + + +class BanqueAccordTest(BackendTest): + BACKEND = 'banqueaccord' + + def test_banqueaccord(self): + raise NotImplementedError()