browser.py 5.5 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

# Copyright(C) 2018      Vincent A
#
5
# This file is part of a weboob module.
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
8 9 10 11
# 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.
#
12
# This weboob module is distributed in the hope that it will be useful,
13 14 15 16 17
# 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
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26

from __future__ import unicode_literals

from base64 import b64encode

from weboob.browser.browsers import APIBrowser
from weboob.exceptions import BrowserIncorrectPassword, BrowserBanned
from weboob.capabilities.captcha import (
27
    ImageCaptchaJob, RecaptchaJob, RecaptchaV3Job, NocaptchaJob, FuncaptchaJob, CaptchaError,
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    InsufficientFunds, UnsolvableCaptcha, InvalidCaptcha,
)


class AnticaptchaBrowser(APIBrowser):
    BASEURL = 'https://api.anti-captcha.com/'

    def __init__(self, apikey, captcha_proxy, *args, **kwargs):
        super(AnticaptchaBrowser, self).__init__(*args, **kwargs)
        self.apikey = apikey
        self.captcha_proxy = captcha_proxy

    def post_image(self, data):
        data = {
            "clientKey": self.apikey,
            "task": {
                "type": "ImageToTextTask",
45
                "body": b64encode(data).decode('ascii'),
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
                "phrase": False,
                "case": False,
                "numeric": False,
                "math": 0,
                "minLength": 0,
                "maxLength": 0,
            }
        }
        r = self.request('/createTask', data=data)
        return str(r['taskId'])

    def post_recaptcha(self, url, key):
        return self.post_gcaptcha(url, key, 'RecaptchaV1')

    def post_nocaptcha(self, url, key):
        return self.post_gcaptcha(url, key, 'NoCaptcha')

    def post_gcaptcha(self, url, key, prefix):
        data = {
            "clientKey": self.apikey,
            "task": {
                "type": "%sTaskProxyless" % prefix,
                "websiteURL": url,
                "websiteKey": key,
            },
            "softId": 0,
            "languagePool": "en",
        }
        r = self.request('/createTask', data=data)
        return str(r['taskId'])

77 78 79 80 81 82 83 84 85 86 87 88 89 90
    def post_gcaptchav3(self, url, key, action):
        data = {
            "clientKey": self.apikey,
            "task":{
                "type":"RecaptchaV3TaskProxyless",
                "websiteURL": url,
                "websiteKey": key,
                "minScore": 0.3,
                "pageAction": action
            }
        }
        r = self.request('/createTask', data=data)
        return str(r['taskId'])

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    def post_funcaptcha(self, url, key, sub_domain):
        data = {
            "clientKey": self.apikey,
            "task": {
                "type": "FunCaptchaTaskProxyless",
                "websiteURL": url,
                "funcaptchaApiJSSubdomain": sub_domain,
                "websitePublicKey": key,
            },
            "softId": 0,
            "languagePool": "en",
        }
        r = self.request('/createTask', data=data)
        return str(r['taskId'])

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
    def check_reply(self, r):
        excs = {
            'ERROR_KEY_DOES_NOT_EXIST': BrowserIncorrectPassword,
            'ERROR_PROXY_NOT_AUTHORISED': BrowserIncorrectPassword,
            'ERROR_ZERO_CAPTCHA_FILESIZE': InvalidCaptcha,
            'ERROR_TOO_BIG_CAPTCHA_FILESIZE': InvalidCaptcha,
            'ERROR_IMAGE_TYPE_NOT_SUPPORTED': InvalidCaptcha,
            'ERROR_RECAPTCHA_INVALID_SITEKEY': InvalidCaptcha,
            'ERROR_RECAPTCHA_INVALID_DOMAIN': InvalidCaptcha,
            'ERROR_ZERO_BALANCE': InsufficientFunds,
            'ERROR_CAPTCHA_UNSOLVABLE': UnsolvableCaptcha,
            'ERROR_IP_BLOCKED': BrowserBanned,
            'ERROR_PROXY_BANNED': BrowserBanned,
        }

        if not r['errorId']:
            return

        err = r.get('errorCode')
        exc_type = excs.get(err, CaptchaError)
        raise exc_type(r['errorDescription'])

    def poll(self, job):
        data = {
            "clientKey": self.apikey,
            "taskId": int(job.id),
        }
        r = self.request('/getTaskResult', data=data)
        self.check_reply(r)

        if r['status'] != 'ready':
            return False

        sol = r['solution']
        if isinstance(job, ImageCaptchaJob):
            job.solution = sol['text']
        elif isinstance(job, RecaptchaJob):
            job.solution = sol['recaptchaResponse']
            job.solution_challenge = sol['recaptchaChallenge']
145
        elif isinstance(job, NocaptchaJob) or isinstance(job, RecaptchaV3Job):
146
            job.solution = sol['gRecaptchaResponse']
147 148
        elif isinstance(job, FuncaptchaJob):
            job.solution = sol['token']
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        else:
            raise NotImplementedError()

        return True

    def get_balance(self):
        data = {
            "clientKey": self.apikey
        }
        r = self.request('/getBalance', data=data)
        return r['balance']

    def report_wrong(self, job):
        data = {
            "clientKey": self.apikey,
            "taskId": int(job.id),
        }
        r = self.request('/reportIncorrectImageCaptcha', data=data)
        self.logger.debug('complaint accepted? %s', r['errorId'] == 0)