support repositories to manage backends (closes #747)
This commit is contained in:
parent
ef16a5b726
commit
14a7a1d362
410 changed files with 1079 additions and 297 deletions
23
modules/cragr/__init__.py
Normal file
23
modules/cragr/__init__.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from .backend import CragrBackend
|
||||
|
||||
__all__ = ['CragrBackend']
|
||||
105
modules/cragr/backend.py
Normal file
105
modules/cragr/backend.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 Romain Bignon, Christophe Benz
|
||||
#
|
||||
# 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.tools.backend import BaseBackend, BackendConfig
|
||||
from weboob.tools.ordereddict import OrderedDict
|
||||
from weboob.tools.value import ValueBackendPassword, Value
|
||||
|
||||
from .browser import Cragr
|
||||
|
||||
|
||||
__all__ = ['CragrBackend']
|
||||
|
||||
|
||||
class CragrBackend(BaseBackend, ICapBank):
|
||||
NAME = 'cragr'
|
||||
MAINTAINER = 'Xavier Guerrin'
|
||||
EMAIL = 'xavier@tuxfamily.org'
|
||||
VERSION = '0.a'
|
||||
DESCRIPTION = 'Credit Agricole french bank\'s website'
|
||||
LICENSE = 'AGPLv3+'
|
||||
website_choices = OrderedDict([(k, u'%s (%s)' % (v, k)) for k, v in sorted({
|
||||
'm.ca-alpesprovence.fr': u'Alpes Provence',
|
||||
'm.ca-anjou-maine.fr': u'Anjou Maine',
|
||||
'm.ca-atlantique-vendee.fr': u'Atlantique Vendée',
|
||||
'm.ca-aquitaine.fr': u'Aquitaine',
|
||||
'm.ca-briepicardie.fr': u'Brie Picardie',
|
||||
'm.ca-centrest.fr': u'Centre Est',
|
||||
'm.ca-centrefrance.fr': u'Centre France',
|
||||
'm.ca-centreloire.fr': u'Centre Loire',
|
||||
'm.ca-centreouest.fr': u'Centre Ouest',
|
||||
'm.ca-cb.fr': u'Champagne Bourgogne',
|
||||
'm.ca-charente-perigord.fr': u'Charente Périgord',
|
||||
'm.ca-cmds.fr': u'Charente-Maritime Deux-Sèvres',
|
||||
'm.ca-corse.fr': u'Corse',
|
||||
'm.ca-cotesdarmor.fr': u'Côtes d\'Armor',
|
||||
'm.ca-des-savoie.fr': u'Des Savoie',
|
||||
'm.ca-finistere.fr': u'Finistere',
|
||||
'm.ca-paris.fr': u'Ile-de-France',
|
||||
'm.ca-illeetvilaine.fr': u'Ille-et-Vilaine',
|
||||
'm.ca-languedoc.fr': u'Languedoc',
|
||||
'm.ca-loirehauteloire.fr': u'Loire Haute Loire',
|
||||
'm.ca-lorraine.fr': u'Lorraine',
|
||||
'm.ca-martinique.fr': u'Martinique Guyane',
|
||||
'm.ca-morbihan.fr': u'Morbihan',
|
||||
'm.ca-norddefrance.fr': u'Nord de France',
|
||||
'm.ca-nord-est.fr': u'Nord Est',
|
||||
'm.ca-nmp.fr': u'Nord Midi-Pyrénées',
|
||||
'm.ca-normandie.fr': u'Normandie',
|
||||
'm.ca-normandie-seine.fr': u'Normandie Seine',
|
||||
'm.ca-pca.fr': u'Provence Côte d\'Azur',
|
||||
'm.lefil.com': u'Pyrénées Gascogne',
|
||||
'm.ca-reunion.fr': u'Réunion',
|
||||
'm.ca-sudrhonealpes.fr': u'Sud Rhône Alpes',
|
||||
'm.ca-sudmed.fr': u'Sud Méditerranée',
|
||||
'm.ca-toulouse31.fr': u'Toulouse 31', # m.ca-toulousain.fr redirects here
|
||||
'm.ca-tourainepoitou.fr': u'Tourraine Poitou',
|
||||
}.iteritems())])
|
||||
CONFIG = BackendConfig(Value('website', label='Website to use', choices=website_choices),
|
||||
ValueBackendPassword('login', label='Account ID', masked=False),
|
||||
ValueBackendPassword('password', label='Password'))
|
||||
BROWSER = Cragr
|
||||
|
||||
def create_default_browser(self):
|
||||
return self.create_browser(self.config['website'].get(),
|
||||
self.config['login'].get(),
|
||||
self.config['password'].get())
|
||||
|
||||
def iter_accounts(self):
|
||||
for account in self.browser.get_accounts_list():
|
||||
yield account
|
||||
|
||||
def get_account(self, _id):
|
||||
if not _id.isdigit():
|
||||
raise AccountNotFound()
|
||||
account = self.browser.get_account(_id)
|
||||
if account:
|
||||
return account
|
||||
else:
|
||||
raise AccountNotFound()
|
||||
|
||||
def iter_history(self, account):
|
||||
for history in self.browser.get_history(account):
|
||||
yield history
|
||||
|
||||
|
||||
def transfer(self, account, to, amount, reason=None):
|
||||
return self.browser.do_transfer(account, to, amount, reason)
|
||||
259
modules/cragr/browser.py
Normal file
259
modules/cragr/browser.py
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2009-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
|
||||
from weboob.capabilities.bank import Transfer, TransferError
|
||||
from cragr import pages
|
||||
import mechanize
|
||||
from datetime import datetime
|
||||
import re
|
||||
|
||||
class Cragr(BaseBrowser):
|
||||
PROTOCOL = 'https'
|
||||
ENCODING = 'utf-8'
|
||||
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
|
||||
# a session id that is sometimes added, and should be ignored when matching pages
|
||||
SESSION_REGEXP = '(?:|%s[A-Z0-9]+)' % re.escape(r';jsessionid=')
|
||||
|
||||
is_logging = False
|
||||
|
||||
def __init__(self, website, *args, **kwargs):
|
||||
self.DOMAIN = website
|
||||
self.PAGES = {'https://[^/]+/': pages.LoginPage,
|
||||
'https://[^/]+/.*\.c.*': pages.AccountsList,
|
||||
'https://[^/]+/login/process%s' % self.SESSION_REGEXP: pages.AccountsList,
|
||||
'https://[^/]+/accounting/listAccounts': pages.AccountsList,
|
||||
'https://[^/]+/accounting/listOperations': pages.AccountsList,
|
||||
'https://[^/]+/accounting/showAccountDetail.+': pages.AccountsList,
|
||||
'https://[^/]+/accounting/showMoreAccountOperations.*': pages.AccountsList,
|
||||
}
|
||||
BaseBrowser.__init__(self, *args, **kwargs)
|
||||
|
||||
def viewing_html(self):
|
||||
"""
|
||||
As the fucking HTTP server returns a document in unknown mimetype
|
||||
'application/vnd.wap.xhtml+xml' it is not recognized by mechanize.
|
||||
|
||||
So this is a fucking hack.
|
||||
"""
|
||||
return True
|
||||
|
||||
def is_logged(self):
|
||||
logged = self.page and self.page.is_logged() or self.is_logging
|
||||
self.logger.debug('logged: %s' % (logged and 'yes' or 'no'))
|
||||
return logged
|
||||
|
||||
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)
|
||||
|
||||
# Do we really need to login?
|
||||
if self.is_logged():
|
||||
self.logger.debug('already logged in')
|
||||
return
|
||||
|
||||
self.is_logging = True
|
||||
# Are we on the good page?
|
||||
if not self.is_on_page(pages.LoginPage):
|
||||
self.logger.debug('going to login page')
|
||||
BaseBrowser.home(self)
|
||||
self.logger.debug('attempting to log in')
|
||||
self.page.login(self.username, self.password)
|
||||
self.is_logging = False
|
||||
|
||||
if not self.is_logged():
|
||||
raise BrowserIncorrectPassword()
|
||||
|
||||
def get_accounts_list(self):
|
||||
self.logger.debug('accounts list required')
|
||||
self.home()
|
||||
return self.page.get_list()
|
||||
|
||||
def home(self):
|
||||
"""
|
||||
Ensure we are both logged and on the accounts list.
|
||||
"""
|
||||
self.logger.debug('accounts list page required')
|
||||
if self.is_on_page(pages.AccountsList) and self.page.is_accounts_list():
|
||||
self.logger.debug('already on accounts list')
|
||||
return
|
||||
|
||||
# simply go to http(s)://the.doma.in/
|
||||
BaseBrowser.home(self)
|
||||
|
||||
if self.is_on_page(pages.LoginPage):
|
||||
if not self.is_logged():
|
||||
# So, we are not logged on the login page -- what about logging ourselves?
|
||||
self.login()
|
||||
# we assume we are logged in
|
||||
# for some regions, we may stay on the login page once we're
|
||||
# logged in, without being redirected...
|
||||
if self.is_on_page(pages.LoginPage):
|
||||
# ... so we have to move by ourselves
|
||||
self.move_to_accounts_list()
|
||||
|
||||
def move_to_accounts_list(self):
|
||||
"""
|
||||
For regions where you can stay on http(s)://the.doma.in/ while you are
|
||||
logged in, move to the accounts list
|
||||
"""
|
||||
self.location('%s://%s/accounting/listAccounts' % (self.PROTOCOL, self.DOMAIN))
|
||||
|
||||
def get_account(self, id):
|
||||
assert isinstance(id, basestring)
|
||||
|
||||
l = self.get_accounts_list()
|
||||
for a in l:
|
||||
if a.id == ('%s' % id):
|
||||
return a
|
||||
|
||||
return None
|
||||
|
||||
def get_history(self, account):
|
||||
history_url = account.link_id
|
||||
operations_count = 0
|
||||
|
||||
# 1st, go on the account page
|
||||
self.logger.debug('going on: %s' % history_url)
|
||||
self.location('https://%s%s' % (self.DOMAIN, history_url))
|
||||
|
||||
# Some regions have a "Show more" (well, actually "Voir les 25
|
||||
# suivants") link we have to use to get all the operations.
|
||||
# However, it does not show only the 25 next results, it *adds* them
|
||||
# to the current view. Therefore, we have to parse each new page using
|
||||
# an offset, in order to ignore all already-fetched operations.
|
||||
# This especially occurs on CA Centre.
|
||||
use_expand_url = bool(self.page.expand_history_page_url())
|
||||
while (history_url):
|
||||
# we skip "operations_count" operations on each page if we are in the case described above
|
||||
operations_offset = operations_count if use_expand_url else 0
|
||||
for page_operation in self.page.get_history(operations_count, operations_offset):
|
||||
operations_count += 1
|
||||
yield page_operation
|
||||
history_url = self.page.expand_history_page_url() if use_expand_url else self.page.next_page_url()
|
||||
self.logger.debug('going on: %s' % history_url)
|
||||
self.location('https://%s%s' % (self.DOMAIN, history_url))
|
||||
|
||||
def dict_find_value(self, dictionary, value):
|
||||
"""
|
||||
Returns the first key pointing on the given value, or None if none
|
||||
is found.
|
||||
"""
|
||||
for k, v in dictionary.iteritems():
|
||||
if v == value:
|
||||
return k
|
||||
return None
|
||||
|
||||
def do_transfer(self, account, to, amount, reason=None):
|
||||
"""
|
||||
Transfer the given amount of money from an account to another,
|
||||
tagging the transfer with the given reason.
|
||||
"""
|
||||
# access the transfer page
|
||||
transfer_page_unreachable_message = u'Could not reach the transfer page.'
|
||||
self.home()
|
||||
if not self.page.is_accounts_list():
|
||||
raise TransferError(transfer_page_unreachable_message)
|
||||
|
||||
operations_url = self.page.operations_page_url()
|
||||
|
||||
self.location('https://%s%s' % (self.DOMAIN, operations_url))
|
||||
transfer_url = self.page.transfer_page_url()
|
||||
|
||||
abs_transfer_url = 'https://%s%s' % (self.DOMAIN, transfer_url)
|
||||
self.location(abs_transfer_url)
|
||||
if not self.page.is_transfer_page():
|
||||
raise TransferError(transfer_page_unreachable_message)
|
||||
|
||||
source_accounts = self.page.get_transfer_source_accounts()
|
||||
target_accounts = self.page.get_transfer_target_accounts()
|
||||
|
||||
# check that the given source account can be used
|
||||
if not account in source_accounts.values():
|
||||
raise TransferError('You cannot use account %s as a source account.' % account)
|
||||
|
||||
# check that the given source account can be used
|
||||
if not to in target_accounts.values():
|
||||
raise TransferError('You cannot use account %s as a target account.' % to)
|
||||
|
||||
# separate euros from cents
|
||||
amount_euros = int(amount)
|
||||
amount_cents = int((amount * 100) - (amount_euros * 100))
|
||||
|
||||
# let's circumvent https://github.com/jjlee/mechanize/issues/closed#issue/17
|
||||
# using http://wwwsearch.sourceforge.net/mechanize/faq.html#usage
|
||||
adjusted_response = self.response().get_data().replace('<br/>', '<br />')
|
||||
response = mechanize.make_response(adjusted_response, [('Content-Type', 'text/html')], abs_transfer_url, 200, 'OK')
|
||||
self.set_response(response)
|
||||
|
||||
# fill the form
|
||||
self.select_form(nr=0)
|
||||
self['numCompteEmetteur'] = ['%s' % self.dict_find_value(source_accounts, account)]
|
||||
self['numCompteBeneficiaire'] = ['%s' % self.dict_find_value(target_accounts, to)]
|
||||
self['montantPartieEntiere'] = '%s' % amount_euros
|
||||
self['montantPartieDecimale'] = '%02d' % amount_cents
|
||||
if reason != None:
|
||||
self['libelle'] = reason
|
||||
self.submit()
|
||||
|
||||
# look for known errors
|
||||
content = unicode(self.response().get_data(), 'utf-8')
|
||||
insufficient_amount_message = u'Montant insuffisant.'
|
||||
maximum_allowed_balance_message = u'Solde maximum autorisé dépassé.'
|
||||
|
||||
if content.find(insufficient_amount_message) != -1:
|
||||
raise TransferError('The amount you tried to transfer is too low.')
|
||||
|
||||
if content.find(maximum_allowed_balance_message) != -1:
|
||||
raise TransferError('The maximum allowed balance for the target account has been / would be reached.')
|
||||
|
||||
# look for the known "all right" message
|
||||
ready_for_transfer_message = u'Vous allez effectuer un virement'
|
||||
if not content.find(ready_for_transfer_message):
|
||||
raise TransferError('The expected message "%s" was not found.' % ready_for_transfer_message)
|
||||
|
||||
# submit the last form
|
||||
self.select_form(nr=0)
|
||||
submit_date = datetime.now()
|
||||
self.submit()
|
||||
|
||||
# look for the known "everything went well" message
|
||||
content = unicode(self.response().get_data(), 'utf-8')
|
||||
transfer_ok_message = u'Vous venez d\'effectuer un virement du compte'
|
||||
if not content.find(transfer_ok_message):
|
||||
raise TransferError('The expected message "%s" was not found.' % transfer_ok_message)
|
||||
|
||||
# We now have to return a Transfer object
|
||||
# the final page does not provide any transfer id, so we'll use the submit date
|
||||
transfer = Transfer(submit_date.strftime('%Y%m%d%H%M%S'))
|
||||
transfer.amount = amount
|
||||
transfer.origin = account
|
||||
transfer.recipient = to
|
||||
transfer.date = submit_date
|
||||
return transfer
|
||||
|
||||
#def get_coming_operations(self, account):
|
||||
# if not self.is_on_page(pages.AccountComing) or self.page.account.id != account.id:
|
||||
# self.location('/NS_AVEEC?ch4=%s' % account.link_id)
|
||||
# return self.page.get_operations()
|
||||
BIN
modules/cragr/favicon.png
Normal file
BIN
modules/cragr/favicon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
24
modules/cragr/pages/__init__.py
Normal file
24
modules/cragr/pages/__init__.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from .accounts_list import AccountsList
|
||||
from .login import LoginPage
|
||||
|
||||
__all__ = ['AccountsList', 'LoginPage']
|
||||
283
modules/cragr/pages/accounts_list.py
Normal file
283
modules/cragr/pages/accounts_list.py
Normal file
|
|
@ -0,0 +1,283 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
import re
|
||||
from weboob.capabilities.bank import Account
|
||||
from .base import CragrBasePage
|
||||
from weboob.capabilities.bank import Operation
|
||||
|
||||
def clean_amount(amount):
|
||||
"""
|
||||
Removes weird characters and converts to a float
|
||||
>>> clean_amount(u'1 000,00 $')
|
||||
1000.0
|
||||
"""
|
||||
data = amount.replace(',', '.').replace(' ', '').replace(u'\xa0', '')
|
||||
matches = re.findall('^(-?[0-9]+\.[0-9]{2}).*$', data)
|
||||
return float(matches[0]) if (matches) else 0.0
|
||||
|
||||
class AccountsList(CragrBasePage):
|
||||
|
||||
def get_list(self):
|
||||
"""
|
||||
Returns the list of available bank accounts
|
||||
"""
|
||||
l = []
|
||||
|
||||
for div in self.document.getiterator('div'):
|
||||
if div.attrib.get('class', '') == 'dv' and div.getchildren()[0].tag in ('a', 'br'):
|
||||
account = Account()
|
||||
if div.getchildren()[0].tag == 'a':
|
||||
# This is at least present on CA Nord-Est
|
||||
account.label = ' '.join(div.find('a').text.split()[:-1])
|
||||
account.link_id = div.find('a').get('href', '')
|
||||
account.id = div.find('a').text.split()[-1]
|
||||
s = div.find('div').find('b').find('span').text
|
||||
else:
|
||||
# This is at least present on CA Toulouse
|
||||
account.label = div.find('a').text.strip()
|
||||
account.link_id = div.find('a').get('href', '')
|
||||
account.id = div.findall('br')[1].tail.strip()
|
||||
s = div.find('div').find('b').text
|
||||
account.balance = clean_amount(s)
|
||||
l.append(account)
|
||||
return l
|
||||
|
||||
def is_accounts_list(self):
|
||||
"""
|
||||
Returns True if the current page appears to be the page dedicated to
|
||||
list the accounts.
|
||||
"""
|
||||
# we check for the presence of a "mes comptes titres" link_id
|
||||
link = self.document.xpath('/html/body//a[contains(text(), "comptes titres")]')
|
||||
return bool(link)
|
||||
|
||||
def is_account_page(self):
|
||||
"""
|
||||
Returns True if the current page appears to be a page dedicated to list
|
||||
the history of a specific account.
|
||||
"""
|
||||
# tested on CA Lorraine, Paris, Toulouse
|
||||
title_spans = self.document.xpath('/html/body//div[@class="dv"]/span')
|
||||
for title_span in title_spans:
|
||||
title_text = title_span.text_content().strip().replace("\n", '')
|
||||
if (re.match('.*Compte.*n.*[0-9]+.*au.*', title_text)):
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_transfer_page(self):
|
||||
"""
|
||||
Returns True if the current page appears to be the page dedicated to
|
||||
order transfers between accounts.
|
||||
"""
|
||||
source_account_select_field = self.document.xpath('/html/body//form//select[@name="numCompteEmetteur"]')
|
||||
target_account_select_field = self.document.xpath('/html/body//form//select[@name="numCompteBeneficiaire"]')
|
||||
return bool(source_account_select_field) and bool(target_account_select_field)
|
||||
|
||||
def get_transfer_accounts(self, select_name):
|
||||
"""
|
||||
Returns the accounts proposed for a transfer in a select field.
|
||||
This method assumes the current page is the one dedicated to transfers.
|
||||
select_name is the name of the select field to analyze
|
||||
"""
|
||||
if not self.is_transfer_page():
|
||||
return False
|
||||
source_accounts = {}
|
||||
source_account_options = self.document.xpath('/html/body//form//select[@name="%s"]/option' % select_name)
|
||||
for option in source_account_options:
|
||||
source_account_value = option.get('value', -1)
|
||||
if (source_account_value != -1):
|
||||
matches = re.findall('^[A-Z0-9]+.*([0-9]{11}).*$', self.extract_text(option))
|
||||
if matches:
|
||||
source_accounts[source_account_value] = matches[0]
|
||||
return source_accounts
|
||||
|
||||
def get_transfer_source_accounts(self):
|
||||
return self.get_transfer_accounts('numCompteEmetteur')
|
||||
|
||||
def get_transfer_target_accounts(self):
|
||||
return self.get_transfer_accounts('numCompteBeneficiaire')
|
||||
|
||||
def expand_history_page_url(self):
|
||||
"""
|
||||
When on a page dedicated to list the history of a specific account (see
|
||||
is_account_page), returns the link to expand the history with 25 more results,
|
||||
or False if the link is not present.
|
||||
"""
|
||||
# tested on CA centre france
|
||||
a = self.document.xpath('/html/body//div[@class="navlink"]//a[contains(text(), "Voir les 25 suivants")]')
|
||||
if not a:
|
||||
return False
|
||||
else:
|
||||
return a[0].get('href', '')
|
||||
|
||||
def next_page_url(self):
|
||||
"""
|
||||
When on a page dedicated to list the history of a specific account (see
|
||||
is_account_page), returns the link to the next page, or False if the
|
||||
link is not present.
|
||||
"""
|
||||
# tested on CA Lorraine, Paris, Toulouse
|
||||
a = self.document.xpath('/html/body//div[@class="navlink"]//a[contains(text(), "Suite")]')
|
||||
if not a:
|
||||
return False
|
||||
else:
|
||||
return a[0].get('href', '')
|
||||
|
||||
def operations_page_url(self):
|
||||
"""
|
||||
Returns the link to the "Opérations" page. This function assumes the
|
||||
current page is the accounts list (see is_accounts_list)
|
||||
"""
|
||||
link = self.document.xpath(u'/html/body//a[contains(text(), "Opérations")]')
|
||||
return link[0].get('href')
|
||||
|
||||
def transfer_page_url(self):
|
||||
"""
|
||||
Returns the link to the "Virements" page. This function assumes the
|
||||
current page is the operations list (see operations_page_url)
|
||||
"""
|
||||
link = self.document.xpath('/html/body//a[@accesskey=1]/@href')
|
||||
return link[0]
|
||||
|
||||
def is_right_aligned_div(self, div_elmt):
|
||||
"""
|
||||
Returns True if the given div element is right-aligned
|
||||
"""
|
||||
return(re.match('.*text-align: ?right.*', div_elmt.get('style', '')))
|
||||
|
||||
def extract_text(self, xml_elmt):
|
||||
"""
|
||||
Given an XML element, returns its inner text in a reasonably readable way
|
||||
"""
|
||||
data = u''
|
||||
for text in xml_elmt.itertext():
|
||||
data = data + u'%s ' % text
|
||||
data = re.sub(' +', ' ', data.replace("\n", ' ').strip())
|
||||
return data
|
||||
|
||||
def get_history(self, start_index = 0, start_offset = 0):
|
||||
"""
|
||||
Returns the history of a specific account. Note that this function
|
||||
expects the current page to be the one dedicated to this history.
|
||||
start_index is the id used for the first created operation.
|
||||
start_offset allows ignoring the `n' first Operations on the page.
|
||||
"""
|
||||
# tested on CA Lorraine, Paris, Toulouse
|
||||
# avoir parsing the page as an account-dedicated page if it is not the case
|
||||
if not self.is_account_page():
|
||||
return
|
||||
|
||||
index = start_index
|
||||
operation = False
|
||||
skipped = 0
|
||||
|
||||
body_elmt_list = self.document.xpath('/html/body/*')
|
||||
|
||||
# type of separator used in the page
|
||||
separators = 'hr'
|
||||
# How many <hr> elements do we have under the <body>?
|
||||
sep_expected = len(self.document.xpath('/html/body/hr'))
|
||||
if (not sep_expected):
|
||||
# no <hr>? Then how many class-less <div> used as separators instead?
|
||||
sep_expected = len(self.document.xpath('/html/body/div[not(@class) and not(@style)]'))
|
||||
separators = 'div'
|
||||
|
||||
# the interesting divs are after the <hr> elements
|
||||
interesting_divs = []
|
||||
right_div_count = 0
|
||||
left_div_count = 0
|
||||
sep_found = 0
|
||||
for body_elmt in body_elmt_list:
|
||||
if (separators == 'hr' and body_elmt.tag == 'hr'):
|
||||
sep_found += 1
|
||||
elif (separators == 'div' and body_elmt.tag == 'div' and body_elmt.get('class', 'nope') == 'nope'):
|
||||
sep_found += 1
|
||||
elif (sep_found >= sep_expected and body_elmt.tag == 'div'):
|
||||
# we just want <div> with dv class and a style attribute
|
||||
if (body_elmt.get('class', '') != 'dv'):
|
||||
continue
|
||||
if (body_elmt.get('style', 'nope') == 'nope'):
|
||||
continue
|
||||
interesting_divs.append(body_elmt)
|
||||
if (self.is_right_aligned_div(body_elmt)):
|
||||
right_div_count += 1
|
||||
else:
|
||||
left_div_count += 1
|
||||
|
||||
# new layout that is somewhat easier to parse (found at Toulouse)
|
||||
table_layout = len(self.document.xpath("id('operationsHeader')")) > 0
|
||||
# So, how are data laid out?
|
||||
alternate_layout = (left_div_count == 2 * right_div_count)
|
||||
# we'll have: one left-aligned div for the date, one right-aligned
|
||||
# div for the amount, and one left-aligned div for the label. Each time.
|
||||
|
||||
if table_layout:
|
||||
lines = self.document.xpath('id("operationsContent")//table[@class="tb"]/tr')
|
||||
for line in lines:
|
||||
if skipped < start_offset:
|
||||
skipped += 1
|
||||
continue
|
||||
operation = Operation(index)
|
||||
index += 1
|
||||
operation.date = self.extract_text(line[0])
|
||||
operation.label = self.extract_text(line[1])
|
||||
operation.amount = clean_amount(self.extract_text(line[2]))
|
||||
yield operation
|
||||
elif (not alternate_layout):
|
||||
for body_elmt in interesting_divs:
|
||||
if skipped < start_offset:
|
||||
if self.is_right_aligned_div(body_elmt):
|
||||
skipped += 1
|
||||
continue
|
||||
if (self.is_right_aligned_div(body_elmt)):
|
||||
# this is the second line of an operation entry, displaying the amount
|
||||
operation.amount = clean_amount(self.extract_text(body_elmt))
|
||||
yield operation
|
||||
else:
|
||||
# this is the first line of an operation entry, displaying the date and label
|
||||
data = self.extract_text(body_elmt)
|
||||
matches = re.findall('^([012][0-9]|3[01])/(0[1-9]|1[012]).(.+)$', data)
|
||||
operation = Operation(index)
|
||||
index += 1
|
||||
if (matches):
|
||||
operation.date = u'%s/%s' % (matches[0][0], matches[0][1])
|
||||
operation.label = u'%s' % matches[0][2]
|
||||
else:
|
||||
operation.date = u'01/01'
|
||||
operation.label = u'Unknown'
|
||||
else:
|
||||
for i in range(0, len(interesting_divs)/3):
|
||||
if skipped < start_offset:
|
||||
skipped += 1
|
||||
continue
|
||||
operation = Operation(index)
|
||||
index += 1
|
||||
# amount
|
||||
operation.amount = clean_amount(self.extract_text(interesting_divs[(i*3)+1]))
|
||||
# date
|
||||
data = self.extract_text(interesting_divs[i*3])
|
||||
matches = re.findall('^([012][0-9]|3[01])/(0[1-9]|1[012])', data)
|
||||
operation.date = u'%s/%s' % (matches[0][0], matches[0][1]) if (matches) else u'01/01'
|
||||
#label
|
||||
data = self.extract_text(interesting_divs[(i*3)+2])
|
||||
data = re.sub(' +', ' ', data)
|
||||
operation.label = u'%s' % data
|
||||
yield operation
|
||||
41
modules/cragr/pages/base.py
Normal file
41
modules/cragr/pages/base.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.tools.browser import BasePage
|
||||
from weboob.tools.browser import BrowserUnavailable
|
||||
|
||||
class CragrBasePage(BasePage):
|
||||
def on_loaded(self):
|
||||
# Check for an error
|
||||
for div in self.document.getiterator('div'):
|
||||
if div.attrib.get('class', '') == 'dv' and div.getchildren()[0].tag in ('img') and div.getchildren()[0].attrib.get('alt', '') == 'Attention':
|
||||
# Try to find a detailed error message
|
||||
if div.getchildren()[1].tag == 'span':
|
||||
raise BrowserUnavailable(div.find('span').find('b').text)
|
||||
elif div.getchildren()[1].tag == 'b':
|
||||
# I haven't encountered this variation in the wild,
|
||||
# but I wouldn't be surprised if it existed
|
||||
# given the similar differences between regions.
|
||||
raise BrowserUnavailable(div.find('b').find('span').text)
|
||||
raise BrowserUnavailable()
|
||||
|
||||
def is_logged(self):
|
||||
return not self.document.xpath('/html/body//form//input[@name = "code"]') and \
|
||||
not self.document.xpath('/html/body//form//input[@name = "userPassword"]')
|
||||
51
modules/cragr/pages/login.py
Normal file
51
modules/cragr/pages/login.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.tools.mech import ClientForm
|
||||
ControlNotFoundError = ClientForm.ControlNotFoundError
|
||||
|
||||
from .base import CragrBasePage
|
||||
|
||||
|
||||
__all__ = ['LoginPage']
|
||||
|
||||
|
||||
class LoginPage(CragrBasePage):
|
||||
def login(self, login, password):
|
||||
self.browser.select_form(nr=0)
|
||||
try:
|
||||
self.browser['numero'] = login
|
||||
self.browser['code'] = password
|
||||
except ControlNotFoundError:
|
||||
try:
|
||||
self.browser['userLogin'] = login
|
||||
self.browser['userPassword'] = password
|
||||
except ControlNotFoundError:
|
||||
self.browser.controls.append(ClientForm.TextControl('text', 'numero', {'value': ''}))
|
||||
self.browser.controls.append(ClientForm.TextControl('text', 'code', {'value': ''}))
|
||||
self.browser.controls.append(ClientForm.TextControl('text', 'userLogin', {'value': ''}))
|
||||
self.browser.controls.append(ClientForm.TextControl('text', 'userPassword', {'value': ''}))
|
||||
self.browser.set_all_readonly(False)
|
||||
self.browser['numero'] = login
|
||||
self.browser['code'] = password
|
||||
self.browser['userLogin'] = login
|
||||
self.browser['userPassword'] = password
|
||||
|
||||
self.browser.submit()
|
||||
30
modules/cragr/test.py
Normal file
30
modules/cragr/test.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from weboob.tools.test import BackendTest
|
||||
|
||||
class CrAgrTest(BackendTest):
|
||||
BACKEND = 'cragr'
|
||||
|
||||
def test_cragr(self):
|
||||
l = list(self.backend.iter_accounts())
|
||||
if len(l) > 0:
|
||||
a = l[0]
|
||||
list(self.backend.iter_history(a))
|
||||
Loading…
Add table
Add a link
Reference in a new issue