summaryrefslogtreecommitdiffstats
path: root/module/plugins/internal/Captcha.py
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-07-21 22:53:37 +0200
committerGravatar Walter Purcaro <vuolter@users.noreply.github.com> 2015-07-21 22:53:37 +0200
commit027cb529d79558de19c47da88a782b31745a65c9 (patch)
tree18ab9a8568cee2d07e6fc0dc8f9c03202245c594 /module/plugins/internal/Captcha.py
parentNew __status__ magic key (diff)
downloadpyload-027cb529d79558de19c47da88a782b31745a65c9.tar.xz
New Captcha skeleton
Diffstat (limited to 'module/plugins/internal/Captcha.py')
-rw-r--r--module/plugins/internal/Captcha.py102
1 files changed, 82 insertions, 20 deletions
diff --git a/module/plugins/internal/Captcha.py b/module/plugins/internal/Captcha.py
index 814c36756..af7f66ed5 100644
--- a/module/plugins/internal/Captcha.py
+++ b/module/plugins/internal/Captcha.py
@@ -6,24 +6,28 @@ from module.plugins.internal.Plugin import Plugin
class Captcha(Plugin):
__name__ = "Captcha"
__type__ = "captcha"
- __version__ = "0.31"
+ __version__ = "0.01"
__status__ = "stable"
- __description__ = """Base captcha service plugin"""
+ __description__ = """Base anti-captcha plugin"""
__license__ = "GPLv3"
__authors__ = [("Walter Purcaro", "vuolter@gmail.com")]
- def __init__(self, plugin):
+ def __init__(self, plugin): #@TODO: pass pyfile instead plugin, so store plugin's html in its associated pyfile as data
self.pyload = plugin.core
self.info = {} #: Provide information in dict here
self.plugin = plugin
- self.key = None #: Last key detected
+ self.task = None #: captchaManager task
self.init()
+ def _log(self, type, args):
+ return super(Captcha, self)._log(type, (self.plugin.__name__,) + args)
+
+
def init(self):
"""
Initialize additional data structures
@@ -31,29 +35,87 @@ class Captcha(Plugin):
pass
- #@TODO: Recheck in 0.4.10
- def retrieve_key(self, html):
- if self.detect_key(html):
- return self.key
+ def decrypt_image(self, url, get={}, post={}, ref=False, cookies=False, decode=False,
+ input_type='png', output_type='textual', try_ocr=True):
+ image = self.load(url, get=get, post=post, ref=ref, cookies=cookies, decode=decode)
+ return self.decrypt(image, input_type, output_type, try_ocr)
+
+
+ def decrypt(self, data, input_type='png', output_type='textual', try_ocr=True):
+ """
+ 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 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 try_ocr: if True, ocr is not used
+
+ :return: result of decrypting
+ """
+ id = ("%.2f" % time.time())[-6:].replace(".", "")
+
+ with open(os.path.join("tmp", "tmpCaptcha_%s_%s.%s" % (self.plugin.__name__, id, input_type)), "wb") as tmpCaptcha:
+ tmpCaptcha.write(img)
+
+ has_plugin = self.plugin.__name__ in self.pyload.pluginManager.ocrPlugins
+
+ if self.pyload.captcha:
+ Ocr = self.pyload.pluginManager.loadClass("ocr", self.plugin.__name__)
else:
- self.fail(_("%s key not found") % self.__name__)
+ Ocr = None
+ if Ocr and try_ocr:
+ time.sleep(random.randint(3000, 5000) / 1000.0)
+ if self.pyfile.abort:
+ self.abort()
- #@TODO: Recheck in 0.4.10
- def retrieve_html(self):
- if hasattr(self.plugin, "html") and self.plugin.html:
- return self.plugin.html
+ ocr = Ocr(self.pyfile)
+ result = ocr.get_captcha(tmpCaptcha.name)
else:
- self.fail(_("%s html not found") % self.__name__)
+ captchaManager = self.pyload.captchaManager
+ task = captchaManager.newTask(img, input_type, tmpCaptcha.name, output_type)
+ self.task = task
+ captchaManager.handleCaptcha(task)
+
+ while task.isWaiting():
+ if self.pyfile.abort:
+ captchaManager.removeTask(task)
+ self.abort()
+ time.sleep(1)
+
+ captchaManager.removeTask(task)
+
+ if task.error and has_plugin: #: Ignore default error message since the user could use try_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" % result)
+ if not self.pyload.debug:
+ try:
+ os.remove(tmpCaptcha.name)
+ except Exception:
+ pass
- def detect_key(self, html=None):
- raise NotImplementedError
+ return result
- def challenge(self, key=None, html=None):
- raise NotImplementedError
+ def invalid(self):
+ self.log_error(_("Invalid captcha"))
+ if self.task:
+ self.task.invalid()
- def result(self, server, challenge):
- raise NotImplementedError
+ def correct(self):
+ self.log_info(_("Correct captcha"))
+ if self.task:
+ self.task.correct()