diff options
| author | 2014-10-03 09:08:53 +0200 | |
|---|---|---|
| committer | 2014-10-03 09:08:53 +0200 | |
| commit | 37e66401e68c082900ff98da6e38ca092a024933 (patch) | |
| tree | e0ea4f774845bf1b813ae7316e63ca99d50c6bee | |
| parent | Fix captcha import of OCR class (diff) | |
| download | pyload-37e66401e68c082900ff98da6e38ca092a024933.tar.xz | |
[CaptchaService] Improved
| -rw-r--r-- | module/plugins/internal/CaptchaService.py | 169 | 
1 files changed, 134 insertions, 35 deletions
| diff --git a/module/plugins/internal/CaptchaService.py b/module/plugins/internal/CaptchaService.py index 26482379d..72a8f8789 100644 --- a/module/plugins/internal/CaptchaService.py +++ b/module/plugins/internal/CaptchaService.py @@ -7,96 +7,195 @@ from random import random  class CaptchaService:      __name__ = "CaptchaService" -    __version__ = "0.06" +    __version__ = "0.07" -    __description__ = """Captcha service plugin""" +    __description__ = """Base captcha service plugin"""      __author_name__ = "pyLoad Team"      __author_mail__ = "admin@pyload.org" +    KEY_PATTERN = None + +    key = None +      def __init__(self, plugin):          self.plugin = plugin -class ReCaptcha: -    RECAPTCHA_KEY_PATTERN = r"https?://(?:www\.)?google\.com/recaptcha/api/challenge\?k=(?P<key>\w+)" -    RECAPTCHA_KEY_AJAX_PATTERN = r"Recaptcha\.create\s*\(\s*[\"'](?P<key>\w+)[\"']\s*," +    def detect_key(self, html=None): +        if not html: +            if hasattr(self.plugin, "html") and self.plugin.html: +                html = self.plugin.html +            else: +                errmsg = "%s html missing" % self.__name__ +                self.plugin.fail(errmsg) +                raise TypeError(errmsg) + +        m = re.search(self.KEY_PATTERN, html) +        if m: +            self.key = m.group("KEY") +            self.plugin.logDebug("%s key: %s" % (self.__name__, self.key) +            return self.key +        else: +            self.plugin.logDebug("%s key not found" % self.__name__) +            return None + + +    def challenge(self, key): +        raise NotImplementedError -    recaptcha_key = None +    def result(self, server, challenge): +        raise NotImplementedError -    def __init__(self, plugin): -        self.plugin = plugin +class ReCaptcha(CaptchaService): +    __name__ = "ReCaptcha" +    __version__ = "0.02" -    def detect_key(self, html): -        m = re.search(self.RECAPTCHA_KEY_PATTERN, html) +    __description__ = """ReCaptcha captcha service plugin""" +    __author_name__ = "pyLoad Team" +    __author_mail__ = "admin@pyload.org" + + +    KEY_PATTERN = r"https?://(?:www\.)?google\.com/recaptcha/api/challenge\?k=(?P<KEY>\w+?)" +    KEY_AJAX_PATTERN = r"Recaptcha\.create\s*\(\s*[\"'](?P<KEY>\w+)[\"']\s*," + + +    def detect_key(self, html=None): +        if not html: +            if hasattr(self.plugin, "html") and self.plugin.html: +                html = self.plugin.html +            else: +                errmsg = "ReCaptcha html missing" +                self.plugin.fail(errmsg) +                raise TypeError(errmsg) + +        m = re.search(self.KEY_PATTERN, html)          if m is None: -            m = re.search(self.RECAPTCHA_KEY_AJAX_PATTERN, html) +            m = re.search(self.KEY_AJAX_PATTERN, html)          if m: -            self.recaptcha_key = m.group('key') -            return self.recaptcha_key +            self.key = m.group("KEY") +            self.plugin.logDebug("ReCaptcha key: %s" % self.key) +            return self.key          else: +            self.plugin.logDebug("ReCaptcha key not found")              return None -    def challenge(self, key=None): +   def challenge(self, key=key):          if not key: -            if self.recaptcha_key: -                key = self.recaptcha_key -            else: -                raise TypeError("ReCaptcha key not found") +            errmsg = "ReCaptcha key missing" +            self.plugin.fail(errmsg) +            raise TypeError(errmsg) -        js = self.plugin.req.load("http://www.google.com/recaptcha/api/challenge", get={"k": key}, cookies=True) +        js = self.plugin.req.load("http://www.google.com/recaptcha/api/challenge", get={'k': key}, cookies=True)          try: -            challenge = re.search("challenge : '(.*?)',", js).group(1) -            server = re.search("server : '(.*?)',", js).group(1) +            challenge = re.search("challenge : '(.+?)',", js).group(1) +            server = re.search("server : '(.+?)',", js).group(1)          except: -            self.plugin.fail("recaptcha error") +            self.plugin.parseError("ReCaptcha challenge pattern not found") +          result = self.result(server, challenge)          return challenge, result      def result(self, server, challenge): -        return self.plugin.decryptCaptcha("%simage" % server, get={"c": challenge}, +        return self.plugin.decryptCaptcha("%simage" % server, get={'c': challenge},                                            cookies=True, forceUser=True, imgtype="jpg")  class AdsCaptcha(CaptchaService): +    __name__ = "AdsCaptcha" +    __version__ = "0.02" -    def challenge(self, src): -        js = self.plugin.req.load(src, cookies=True) +    __description__ = """AdsCaptcha captcha service plugin""" +    __author_name__ = "pyLoad Team" +    __author_mail__ = "admin@pyload.org" + + +    ID_PATTERN = r'http://api\.adscaptcha\.com/Get\.aspx\?[^"\']*CaptchaId=(?P<ID>\d+)' +    KEY_PATTERN = r'http://api\.adscaptcha\.com/Get\.aspx\?[^"\']*PublicKey=(?P<KEY>[\w-]+)' + + +    def detect_key(self, html=None): +        if not html: +            if hasattr(self.plugin, "html") and self.plugin.html: +                html = self.plugin.html +            else: +                errmsg = "AdsCaptcha html missing" +                self.plugin.fail(errmsg) +                raise TypeError(errmsg) + +        m = re.search(self.ID_PATTERN, html) +        n = re.search(self.KEY_PATTERN, html) +        if m and n: +            self.key = (m.group("ID"), m.group("KEY")) +            self.plugin.logDebug("AdsCaptcha id|key: %s | %s" % self.key) +            return self.key +        else: +            self.plugin.logDebug("AdsCaptcha id or key not found") +            return None + + +    def challenge(self, key=key):  #: key is tuple(CaptchaId, PublicKey) +        CaptchaId, PublicKey = key + +        if not CaptchaId or not PublicKey: +            errmsg = "AdsCaptcha key missing" +            self.plugin.fail(errmsg) +            raise TypeError(errmsg) + +        js = self.plugin.req.load("http://api.adscaptcha.com/Get.aspx", get={'CaptchaId': CaptchaId, 'PublicKey': PublicKey}, cookies=True)          try: -            challenge = re.search("challenge: '(.*?)',", js).group(1) -            server = re.search("server: '(.*?)',", js).group(1) +            challenge = re.search("challenge: '(.+?)',", js).group(1) +            server = re.search("server: '(.+?)',", js).group(1)          except: -            self.plugin.fail("adscaptcha error") +            self.plugin.parseError("AdsCaptcha challenge pattern not found") +          result = self.result(server, challenge)          return challenge, result      def result(self, server, challenge): -        return self.plugin.decryptCaptcha("%sChallenge.aspx" % server, get={"cid": challenge, "dummy": random()}, +        return self.plugin.decryptCaptcha("%sChallenge.aspx" % server, get={'cid': challenge, 'dummy': random()},                                            cookies=True, imgtype="jpg")  class SolveMedia(CaptchaService): +    __name__ = "SolveMedia" +    __version__ = "0.02" -    def challenge(self, src): -        html = self.plugin.req.load("http://api.solvemedia.com/papi/challenge.noscript?k=%s" % src, cookies=True) +    __description__ = """SolveMedia captcha service plugin""" +    __author_name__ = "pyLoad Team" +    __author_mail__ = "admin@pyload.org" + + +    KEY_PATTERN = r'http://api\.solvemedia\.com/papi/challenge\.(no)?script\?k=(?P<KEY>.+?)"' + + +    def challenge(self, key=key): +        if not key: +            errmsg = "SolveMedia key missing" +            self.plugin.fail(errmsg) +            raise TypeError(errmsg) + +        html = self.plugin.req.load("http://api.solvemedia.com/papi/challenge.noscript", get={'k': key}, cookies=True)          try:              challenge = re.search(r'<input type=hidden name="adcopy_challenge" id="adcopy_challenge" value="([^"]+)">',                                    html).group(1) +            server = "http://api.solvemedia.com/papi/media"          except: -            self.plugin.fail("solvemedia error") -        result = self.result(challenge) +            self.plugin.parseError("SolveMedia challenge pattern not found") + +        result = self.result(server, challenge)          return challenge, result -    def result(self, challenge): -        return self.plugin.decryptCaptcha("http://api.solvemedia.com/papi/media?c=%s" % challenge, imgtype="gif") +    def result(self, server, challenge): +        return self.plugin.decryptCaptcha(server, get={'c': challenge}, imgtype="gif") | 
