Beispiel #1
0
    def isCaptchaWaiting(self):
        """Indicates wether a captcha task is available

        :return: bool
        """
        self.core.lastClientConnected = time()
        task = get_captcha_manager().getTask()
        return task is not None
Beispiel #2
0
    def getCaptchaTaskStatus(self, tid):
        """Get information about captcha task

        :param tid: task id
        :return: string
        """
        self.core.lastClientConnected = time()
        t = get_captcha_manager().getTaskByID(tid)
        return t.getStatus() if t else ""
Beispiel #3
0
    def event_c(self, args):
        """
        Captcha answer
        """
        if not args:
            return ["ERROR: Captcha ID missing."]

        task = get_captcha_manager().getTaskByID(args[0])
        if not task:
            return ["ERROR: Captcha Task with ID %s does not exists." % args[0]]

        task.setResult(" ".join(args[1:]))
        return ["INFO: Result %s saved." % " ".join(args[1:])]
Beispiel #4
0
    def setCaptchaResult(self, tid, result):
        """Set result for a captcha task

        :param tid: task id
        :param result: captcha result
        """
        self.core.lastClientConnected = time()

        captcha_manager = get_captcha_manager()

        task = captcha_manager.getTaskByID(tid)
        if task:
            task.setResult(result)
            captcha_manager.removeTask(task)
Beispiel #5
0
    def getCaptchaTask(self, exclusive=False):
        """Returns a captcha task

        :param exclusive: unused
        :return: `CaptchaTask`
        """
        self.core.lastClientConnected = time()
        task = get_captcha_manager().getTask()
        if task:
            task.setWatingForUser(exclusive=exclusive)
            data, type, result = task.getCaptcha()
            t = CaptchaTask(int(task.id), standard_b64encode(data), type,
                            result)
            return t
        else:
            return CaptchaTask(-1)
Beispiel #6
0
    def decrypt_image(self,
                      img,
                      input_type='jpg',
                      output_type='textual',
                      ocr=False,
                      timeout=120):
        """
        Loads a captcha and decrypts it with ocr, plugin, user input

        :param img: image raw data
        :param get: get part for request
        :param post: post part for request
        :param cookies: True if cookies should be enabled
        :param input_type: Type of the Image
        :param output_type: 'textual' if text is written on the captcha\
        or 'positional' for captcha where the user have to click\
        on a specific region on the captcha
        :param ocr: if True, builtin ocr is used. if string, the OCR plugin name is used

        :return: result of decrypting
        """
        result = None
        time_ref = ("%.2f" % time.time())[-6:].replace(".", "")

        with open(
                os.path.join(
                    "tmp", "captcha_image_%s_%s.%s" %
                    (self.pyfile.plugin.__name__, time_ref, input_type)),
                "wb") as img_f:
            img_f.write(img)

        if ocr:
            self.log_info(_("Using OCR to decrypt captcha..."))

            if isinstance(ocr, six.string_types):
                _OCR = get_plugin_manager().loadClass(
                    "captcha", ocr)  #: Rename `captcha` to `ocr` in 0.4.10
                result = _OCR(self.pyfile).recognize(img_f.name)
            else:
                result = self.recognize(img_f.name)

                if not result:
                    self.log_warning(_("No OCR result"))

        if not result:
            captcha_manager = get_captcha_manager()

            try:
                self.task = captcha_manager.newTask(img, input_type,
                                                    img_f.name, output_type)

                captcha_manager.handleCaptcha(self.task)

                # @TODO: Move to `CaptchaManager` in 0.4.10
                self.task.setWaiting(max(timeout, 50))
                while self.task.isWaiting():
                    self.pyfile.plugin.check_status()
                    time.sleep(1)

            finally:
                captcha_manager.removeTask(self.task)

            result = self.task.result

            if self.task.error:
                if not self.task.handler and not self.pyload.isClientConnected(
                ):
                    self.log_warning(
                        _("No Client connected for captcha decrypting"))
                    self.fail(_("No Client connected for captcha decrypting"))
                else:
                    self.pyfile.plugin.retry_captcha(msg=self.task.error)

            elif self.task.result:
                self.log_info(_("Captcha result: `%s`") % (result, ))

            else:
                self.pyfile.plugin.retry_captcha(
                    msg=_("No captcha result obtained in appropriate timing"))

        if not self.pyload.debug:
            self.remove(img_f.name, trash=False)

        return result
Beispiel #7
0
    def decryptCaptcha(self,
                       url,
                       get={},
                       post={},
                       cookies=False,
                       forceUser=False,
                       imgtype='jpg',
                       result_type='textual'):
        """ Loads a captcha and decrypts it with ocr, plugin, user input

        :param url: url of captcha image
        :param get: get part for request
        :param post: post part for request
        :param cookies: True if cookies should be enabled
        :param forceUser: if True, ocr is not used
        :param imgtype: Type of the Image
        :param result_type: 'textual' if text is written on the captcha\
        or 'positional' for captcha where the user have to click\
        on a specific region on the captcha

        :return: result of decrypting
        """

        img = self.load(url, get=get, post=post, cookies=cookies)

        id = ("%.2f" % time())[-6:].replace(".", "")
        temp_file = open(
            join("tmp", "tmpCaptcha_%s_%s.%s" % (self.__name__, id, imgtype)),
            "wb")
        temp_file.write(img)
        temp_file.close()

        plugin_manager = get_plugin_manager()

        has_plugin = self.__name__ in plugin_manager.captchaPlugins

        if self.core.captcha:
            Ocr = plugin_manager.loadClass("captcha", self.__name__)
        else:
            Ocr = None

        if Ocr and not forceUser:
            sleep(randint(3000, 5000) / 1000.0)
            if self.pyfile.abort:
                raise Abort

            ocr = Ocr()
            result = ocr.get_captcha(temp_file.name)
        else:
            captcha_manager = get_captcha_manager()
            task = captcha_manager.newTask(img, imgtype, temp_file.name,
                                           result_type)
            self.cTask = task
            captcha_manager.handleCaptcha(task)

            while task.isWaiting():
                if self.pyfile.abort:
                    captcha_manager.removeTask(task)
                    raise Abort
                sleep(1)

            captcha_manager.removeTask(task)

            if task.error and has_plugin:  #ignore default error message since the user could use OCR
                self.fail(
                    _("Pil and tesseract not installed and no Client connected for captcha decrypting"
                      ))
            elif task.error:
                self.fail(task.error)
            elif not task.result:
                self.fail(
                    _("No captcha result obtained in appropiate time by any of the plugins."
                      ))

            result = task.result
            self.log.debug("Received captcha result: %s" % str(result))

        if not self.core.debug:
            try:
                remove(temp_file.name)
            except:
                pass

        return result