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
(self, img, input_type='jpg',
output_type='textual', ocr=False, timeout=120)
| 51 | return self.decrypt_image(img, input_type, output_type, ocr, timeout) |
| 52 | |
| 53 | def decrypt_image(self, img, input_type='jpg', |
| 54 | output_type='textual', ocr=False, timeout=120): |
| 55 | """ |
| 56 | Loads a captcha and decrypts it with ocr, plugin, user input |
| 57 | |
| 58 | :param img: image raw data |
| 59 | :param get: get part for request |
| 60 | :param post: post part for request |
| 61 | :param cookies: True if cookies should be enabled |
| 62 | :param input_type: Type of the Image |
| 63 | :param output_type: 'textual' if text is written on the captcha\ |
| 64 | or 'positional' for captcha where the user have to click\ |
| 65 | on a specific region on the captcha |
| 66 | :param ocr: if True, builtin ocr is used. if string, the OCR plugin name is used |
| 67 | |
| 68 | :return: result of decrypting |
| 69 | """ |
| 70 | result = None |
| 71 | time_ref = ("%.2f" % time.time())[-6:].replace(".", "") |
| 72 | |
| 73 | with open(fsjoin("tmp", "captcha_image_%s_%s.%s" % (self.pyfile.plugin.__name__, time_ref, input_type)), "wb") as img_f: |
| 74 | img_f.write(img) |
| 75 | |
| 76 | if ocr: |
| 77 | self.log_info(_("Using OCR to decrypt captcha...")) |
| 78 | |
| 79 | if isinstance(ocr, basestring): |
| 80 | _OCR = self.pyload.pluginManager.loadClass("captcha", ocr) #: Rename `captcha` to `ocr` in 0.4.10 |
| 81 | result = _OCR(self.pyfile).recognize(img_f.name) |
| 82 | else: |
| 83 | result = self.recognize(img_f.name) |
| 84 | |
| 85 | if not result: |
| 86 | self.log_warning(_("No OCR result")) |
| 87 | |
| 88 | if not result: |
| 89 | captchaManager = self.pyload.captchaManager |
| 90 | timeout = max(timeout, 50) |
| 91 | |
| 92 | try: |
| 93 | params = {'src': "data:image/%s;base64,%s" % (input_type, base64.standard_b64encode(img)), |
| 94 | 'file': img_f.name, |
| 95 | 'captcha_plugin': self.__name__, |
| 96 | 'plugin': self.pyfile.plugin.__name__} |
| 97 | self.task = captchaManager.newTask(input_type, params, output_type) |
| 98 | |
| 99 | captchaManager.handleCaptcha(self.task, timeout) |
| 100 | |
| 101 | while self.task.isWaiting(): |
| 102 | self.pyfile.plugin.check_status() |
| 103 | time.sleep(1) |
| 104 | |
| 105 | finally: |
| 106 | captchaManager.removeTask(self.task) |
| 107 | |
| 108 | result = self.task.result |
| 109 | |
| 110 | if self.task.error: |
no test coverage detected