<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2013      <NAME>
#
# 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 datetime
from dateutil.relativedelta import relativedelta

from weboob.deprecated.browser import Browser, BrowserIncorrectPassword

from .pages import LoginPage, AccountPage, UselessPage, HomePage, ProHistoryPage, PartHistoryPage, HistoryDetailsPage, ErrorPage


__all__ = ['Paypal']


class Paypal(Browser):
    DOMAIN = 'www.paypal.com'
    PROTOCOL = 'https'
    CERTHASH = [
            'b8f6c76050ed3035aab08474b1da0ff783f20d114b1740e8db275fe433ff69af',
            '96753399cf183334cef00a72719ea8e13cfe68d1e953006348f41f884180de15',
            '9d3039499e97f7a081244ee01c5d25d82441e5b1f43bd45d6b78dccf1f36652e',
            '72470503a807108c5ff1bc684b786cc3b25b2ec3ba108ac63407193da7bc12a0',
            ]
    ENCODING = 'UTF-8'
    PAGES = {
        '/cgi-bin/webscr\?cmd=_login-run$':             LoginPage,
        '/cgi-bin/webscr\?cmd=_login-submit.+$':        LoginPage,  # wrong login
        '/cgi-bin/webscr\?cmd=_login-processing.+$':    UselessPage,
        '/cgi-bin/webscr\?cmd=_account.*$':             UselessPage,
        '/cgi-bin/webscr\?cmd=_login-done.+$':          UselessPage,
        '/cgi-bin/webscr\?cmd=_home&country_lang.x=true$': HomePage,
        '/auth/validatecaptcha$':                       ErrorPage,
        'https://\w+.paypal.com/cgi-bin/webscr\?cmd=_history-details-from-hub&id=[A-Z0-9]+$': HistoryDetailsPage,
        'https://\w+.paypal.com/webapps/business/\?nav=0.0': HomePage,
        'https://\w+.paypal.com/webapps/business/\?country_lang.x=true': HomePage,
        'https://\w+.paypal.com/myaccount/\?nav=0.0': HomePage,
        'https://\w+.paypal.com/businessexp/money': AccountPage,
        'https://\w+.paypal.com/businessexp/summary': ProHistoryPage,
        'https://\w+.paypal.com/webapps/business/activity\?.*': ProHistoryPage,
        'https://\w+.paypal.com/myaccount/activity/.*': (PartHistoryPage, 'json'),
        'https://\w+.paypal.com/myaccount/': HomePage,
    }

    DEFAULT_TIMEOUT = 180

    BEGINNING = datetime.date(1998, 6, 1)  # The day PayPal was founded
    account_type = None

    def find_account_type(self):
        if self.is_on_page(HomePage):
            # XXX Unable to get more than 2 years of history on pro accounts.
            self.BEGINNING = datetime.date.today() - relativedelta(months=24)
            self.account_type = "pro"
            return
        self.location(self._response.info().getheader('refresh').split("bin/")[1])
        if self.is_on_page(AccountPage):
            self.location('/myaccount')
            self.account_type = "perso"
        else:
            if not self.page or self.page.document.xpath('.//a[contains(@class, "try-now-bttn")]'):
                raise BrowserIncorrectPassword("Please update your account to the new PayPal website to continue to use our services")
            else:
                self.location('/webapps/business/?nav=0.0')
            if self.is_on_page(ProHistoryPage):
                self.account_type = "pro"
            else:
                self.account_type = "perso"

    def home(self):
        self.location('https://' + self.DOMAIN + '/en/cgi-bin/webscr?cmd=_login-run')

    def is_logged(self):
        # TODO Does not handle disconnect mid-session
        return not self.is_on_page(LoginPage)

    def login(self):
        assert isinstance(self.username, basestring)
        assert isinstance(self.password, basestring)

        if not self.is_on_page(LoginPage):
            self.location('https://' + self.DOMAIN + '/en/cgi-bin/webscr?cmd=_login-run')

        self.page.login(self.username, self.password)
        self.page.validate_useless_captacha()

        if self.is_on_page(LoginPage) or self.is_on_page(ErrorPage):
            raise BrowserIncorrectPassword()

        self.find_account_type()

    def get_accounts(self):
        if not self.is_on_page(AccountPage):
            self.location('/businessexp/money')

        return self.page.get_accounts()

    def get_account(self, _id):
        if not self.is_on_page(AccountPage):
            self.location('/businessexp/money')

        return self.page.get_account(_id)

    def get_download_history(self, account, step_min=None, step_max=None):
        if step_min is None and step_max is None:
            step_min = 10
            step_max = 120

        def fetch_fn(start, end):
            if self.download_history(start, end):
                return self.page.iter_transactions(account)
            return iter([])

        assert step_max <= 365*2  # PayPal limitations as of 2014-06-16
        for i in self.smart_fetch(beginning=self.BEGINNING,
                                  end=datetime.date.today(),
                                  step_min=step_min,
                                  step_max=step_max,
                                  fetch_fn=fetch_fn):
            yield i

    def smart_fetch(self, beginning, end, step_min, step_max, fetch_fn):
        """
        Fetches transactions in small chunks to avoid request timeouts.
        Time period of each requested chunk is adjusted dynamically.
        """<commit_msg>handling timeout when fetching old history<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2013      <NAME>
#
# 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 datetime
from dateutil.relativedelta import relativedelta

from weboob.exceptions import BrowserHTTPError
from weboob.deprecated.browser import Browser, BrowserIncorrectPassword

from .pages import LoginPage, AccountPage, UselessPage, HomePage, ProHistoryPage, PartHistoryPage, HistoryDetailsPage, ErrorPage


__all__ = ['Paypal']


class Paypal(Browser):
    DOMAIN = 'www.paypal.com'
    PROTOCOL = 'https'
    CERTHASH = [
            'b8f6c76050ed3035aab08474b1da0ff783f20d114b1740e8db275fe433ff69af',
            '96753399cf183334cef00a72719ea8e13cfe68d1e953006348f41f884180de15',
            '9d3039499e97f7a081244ee01c5d25d82441e5b1f43bd45d6b78dccf1f36652e',
            '72470503a807108c5ff1bc684b786cc3b25b2ec3ba108ac63407193da7bc12a0',
            ]
    ENCODING = 'UTF-8'
    PAGES = {
        '/cgi-bin/webscr\?cmd=_login-run$':             LoginPage,
        '/cgi-bin/webscr\?cmd=_login-submit.+$':        LoginPage,  # wrong login
        '/cgi-bin/webscr\?cmd=_login-processing.+$':    UselessPage,
        '/cgi-bin/webscr\?cmd=_account.*$':             UselessPage,
        '/cgi-bin/webscr\?cmd=_login-done.+$':          UselessPage,
        '/cgi-bin/webscr\?cmd=_home&country_lang.x=true$': HomePage,
        '/auth/validatecaptcha$':                       ErrorPage,
        'https://\w+.paypal.com/cgi-bin/webscr\?cmd=_history-details-from-hub&id=[A-Z0-9]+$': HistoryDetailsPage,
        'https://\w+.paypal.com/webapps/business/\?nav=0.0': HomePage,
        'https://\w+.paypal.com/webapps/business/\?country_lang.x=true': HomePage,
        'https://\w+.paypal.com/myaccount/\?nav=0.0': HomePage,
        'https://\w+.paypal.com/businessexp/money': AccountPage,
        'https://\w+.paypal.com/businessexp/summary': ProHistoryPage,
        'https://\w+.paypal.com/webapps/business/activity\?.*': ProHistoryPage,
        'https://\w+.paypal.com/myaccount/activity/.*': (PartHistoryPage, 'json'),
        'https://\w+.paypal.com/myaccount/': HomePage,
    }

    DEFAULT_TIMEOUT = 180

    BEGINNING = datetime.date(1998, 6, 1)  # The day PayPal was founded
    account_type = None

    def find_account_type(self):
        if self.is_on_page(HomePage):
            # XXX Unable to get more than 2 years of history on pro accounts.
            self.BEGINNING = datetime.date.today() - relativedelta(months=24)
            self.account_type = "pro"
            return
        self.location(self._response.info().getheader('refresh').split("bin/")[1])
        if self.is_on_page(AccountPage):
            self.location('/myaccount')
            self.account_type = "perso"
        else:
            if not self.page or self.page.document.xpath('.//a[contains(@class, "try-now-bttn")]'):
                raise BrowserIncorrectPassword("Please update your account to the new PayPal website to continue to use our services")
            else:
                self.location('/webapps/business/?nav=0.0')
            if self.is_on_page(ProHistoryPage):
                self.account_type = "pro"
            else:
                self.account_type = "perso"

    def home(self):
        self.location('https://' + self.DOMAIN + '/en/cgi-bin/webscr?cmd=_login-run')

    def is_logged(self):
        # TODO Does not handle disconnect mid-session
        return not self.is_on_page(LoginPage)

    def login(self):
        assert isinstance(self.username, basestring)
        assert isinstance(self.password, basestring)

        if not self.is_on_page(LoginPage):
            self.location('https://' + self.DOMAIN + '/en/cgi-bin/webscr?cmd=_login-run')

        self.page.login(self.username, self.password)
        self.page.validate_useless_captacha()

        if self.is_on_page(LoginPage) or self.is_on_page(ErrorPage):
            raise BrowserIncorrectPassword()

        self.find_account_type()

    def get_accounts(self):
        if not self.is_on_page(AccountPage):
            self.location('/businessexp/money')

        return self.page.get_accounts()

    def get_account(self, _id):
        if not self.is_on_page(AccountPage):
            self.location('/businessexp/money')

        return self.page.get_account(_id)

    def get_download_history(self, account, step_min=None, step_max=None):
        if step_min is None and step_max is None:
            step_min = 10
            step_max = 120

        def fetch_fn(start, end):
            if self.download_history(start, end):
                return self.page.iter_transactions(account)
            return iter([])

        assert step_max <= 365*2  # PayPal limitations as of 2014-06-16
        try:
            for i in self.smart_fetch(beginning=self.BEGINNING,
                                      end=datetime.date.today(),
                                      step_min=step_min,
                                      step_max=step_max,
                                      fetch_fn=fetch_fn):
                yield i
        except BrowserHTTPError:
            self.logger.warning("Paypal timeout")

    def smart_fetch(self, beginning, end, step_min, step_max, fetch_fn):
        """
        Fetches transactions in small chunks to avoid request timeouts.
        Time period of each requested chunk is adjusted dynamically.
        """
{'fraction_non_alphanumeric': 0.11312637084528429, 'fraction_numerical': 0.03610595579551206, 'mean_word_length': 2.4643483343074224, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 22, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4088941', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3657, 'n_words': 784}