diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-12-13 15:56:57 +0100 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-12-13 15:56:57 +0100 |
commit | acc46fc3497a66a427b795b4a22c6e71d69185a1 (patch) | |
tree | 2d315b838a76435fc456b972c99c28d1732b2f70 /pyload/plugin/hook/ImageTyperz.py | |
parent | Code fixes (diff) | |
download | pyload-acc46fc3497a66a427b795b4a22c6e71d69185a1.tar.xz |
Update
Diffstat (limited to 'pyload/plugin/hook/ImageTyperz.py')
-rw-r--r-- | pyload/plugin/hook/ImageTyperz.py | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/pyload/plugin/hook/ImageTyperz.py b/pyload/plugin/hook/ImageTyperz.py new file mode 100644 index 000000000..4ad37eb89 --- /dev/null +++ b/pyload/plugin/hook/ImageTyperz.py @@ -0,0 +1,151 @@ +# -*- coding: utf-8 -*- + +from __future__ import with_statement + +import re + +from base64 import b64encode +from pycurl import FORM_FILE, LOW_SPEED_TIME + +from pyload.network.RequestFactory import getURL, getRequest +from pyload.plugin.Addon import Addon + + +class ImageTyperzException(Exception): + + def __init__(self, err): + self.err = err + + + def getCode(self): + return self.err + + + def __str__(self): + return "<ImageTyperzException %s>" % self.err + + + def __repr__(self): + return "<ImageTyperzException %s>" % self.err + + +class ImageTyperz(Addon): + __name = "ImageTyperz" + __type = "hook" + __version = "0.05" + + __config = [("username", "str", "Username", ""), + ("passkey", "password", "Password", ""), + ("force", "bool", "Force IT even if client is connected", False)] + + __description = """Send captchas to ImageTyperz.com""" + __license = "GPLv3" + __authors = [("RaNaN", "RaNaN@pyload.org"), + ("zoidberg", "zoidberg@mujmail.cz")] + + + SUBMIT_URL = "http://captchatypers.com/Forms/UploadFileAndGetTextNEW.ashx" + RESPOND_URL = "http://captchatypers.com/Forms/SetBadImage.ashx" + GETCREDITS_URL = "http://captchatypers.com/Forms/RequestBalance.ashx" + + + def getCredits(self): + res = getURL(self.GETCREDITS_URL, + post={'action': "REQUESTBALANCE", + 'username': self.getConfig("username"), + 'password': self.getConfig("passkey")}) + + if res.startswith('ERROR'): + raise ImageTyperzException(res) + + try: + balance = float(res) + except Exception: + raise ImageTyperzException("Invalid response") + + self.logInfo(_("Account balance: $%s left") % res) + return balance + + + def submit(self, captcha, captchaType="file", match=None): + req = getRequest() + #raise timeout threshold + req.c.setopt(LOW_SPEED_TIME, 80) + + try: + #workaround multipart-post bug in HTTPRequest.py + if re.match("^\w*$", self.getConfig("passkey")): + multipart = True + data = (FORM_FILE, captcha) + else: + multipart = False + with open(captcha, 'rb') as f: + data = f.read() + data = b64encode(data) + + res = req.load(self.SUBMIT_URL, + post={'action': "UPLOADCAPTCHA", + 'username': self.getConfig("username"), + 'password': self.getConfig("passkey"), "file": data}, + multipart=multipart) + finally: + req.close() + + if res.startswith("ERROR"): + raise ImageTyperzException(res) + else: + data = res.split('|') + if len(data) == 2: + ticket, result = data + else: + raise ImageTyperzException("Unknown response: %s" % res) + + return ticket, result + + + def captchaTask(self, task): + if "service" in task.data: + return False + + if not task.isTextual(): + return False + + if not self.getConfig("username") or not self.getConfig("passkey"): + return False + + if self.core.isClientConnected() and not self.getConfig("force"): + return False + + if self.getCredits() > 0: + task.handler.append(self) + task.data['service'] = self.__name + task.setWaiting(100) + self.processCaptcha(task) + else: + self.logInfo(_("Your %s account has not enough credits") % self.__name) + + + def captchaInvalid(self, task): + if task.data['service'] == self.__name and "ticket" in task.data: + res = getURL(self.RESPOND_URL, + post={'action': "SETBADIMAGE", + 'username': self.getConfig("username"), + 'password': self.getConfig("passkey"), + 'imageid': task.data['ticket']}) + + if res == "SUCCESS": + self.logInfo(_("Bad captcha solution received, requested refund")) + else: + self.logError(_("Bad captcha solution received, refund request failed"), res) + + + def processCaptcha(self, task): + c = task.captchaFile + try: + ticket, result = self.submit(c) + except ImageTyperzException, e: + task.error = e.getCode() + return + + task.data['ticket'] = ticket + task.setResult(result) |