diff options
| author | 2011-01-31 19:06:22 +0100 | |
|---|---|---|
| committer | 2011-01-31 19:06:22 +0100 | |
| commit | cca1c563a0acc1c5dbd0552b4d610f1325f53559 (patch) | |
| tree | 7fc238d5f7d724ef21a0d99ed89f567e1d1b6def /module/lib | |
| parent | Added tag v0.4.4 for changeset e328358b2009 (diff) | |
| download | pyload-cca1c563a0acc1c5dbd0552b4d610f1325f53559.tar.xz | |
added captchatrader.com support, fixed lof.cc
Diffstat (limited to 'module/lib')
| -rw-r--r-- | module/lib/MultipartPostHandler.py | 139 | ||||
| -rw-r--r-- | module/lib/captchatrader.py | 119 | 
2 files changed, 258 insertions, 0 deletions
| diff --git a/module/lib/MultipartPostHandler.py b/module/lib/MultipartPostHandler.py new file mode 100644 index 000000000..94aee0193 --- /dev/null +++ b/module/lib/MultipartPostHandler.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +#### +# 02/2006 Will Holcomb <wholcomb@gmail.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU +# Lesser General Public License for more details. +# +# 7/26/07 Slightly modified by Brian Schneider +# in order to support unicode files ( multipart_encode function ) +""" +Usage: +  Enables the use of multipart/form-data for posting forms + +Inspirations: +  Upload files in python: +    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306 +  urllib2_file: +    Fabien Seisen: <fabien@seisen.org> + +Example: +  import MultipartPostHandler, urllib2, cookielib + +  cookies = cookielib.CookieJar() +  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), +                                MultipartPostHandler.MultipartPostHandler) +  params = { "username" : "bob", "password" : "riviera", +             "file" : open("filename", "rb") } +  opener.open("http://wwww.bobsite.com/upload/", params) + +Further Example: +  The main function of this file is a sample which downloads a page and +  then uploads it to the W3C validator. +""" + +from urllib import urlencode +from urllib2 import BaseHandler, HTTPHandler, build_opener +import mimetools, mimetypes +from os import write, remove +from cStringIO import StringIO + +class Callable: +    def __init__(self, anycallable): +        self.__call__ = anycallable + +# Controls how sequences are uncoded. If true, elements may be given multiple values by +#  assigning a sequence. +doseq = 1 + +class MultipartPostHandler(BaseHandler): +    handler_order = HTTPHandler.handler_order - 10 # needs to run first + +    def http_request(self, request): +        data = request.get_data() +        if data is not None and type(data) != str: +            v_files = [] +            v_vars = [] +            try: +                 for(key, value) in data.items(): +                     if type(value) == file: +                         v_files.append((key, value)) +                     else: +                         v_vars.append((key, value)) +            except TypeError: +                systype, value, traceback = sys.exc_info() +                raise TypeError, "not a valid non-string sequence or mapping object", traceback + +            if len(v_files) == 0: +                data = urlencode(v_vars, doseq) +            else: +                boundary, data = self.multipart_encode(v_vars, v_files) + +                contenttype = 'multipart/form-data; boundary=%s' % boundary +                if(request.has_header('Content-Type') +                   and request.get_header('Content-Type').find('multipart/form-data') != 0): +                    print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data') +                request.add_unredirected_header('Content-Type', contenttype) + +            request.add_data(data) + +        return request + +    def multipart_encode(vars, files, boundary = None, buf = None): +        if boundary is None: +            boundary = mimetools.choose_boundary() +        if buf is None: +            buf = StringIO() +        for(key, value) in vars: +            buf.write('--%s\r\n' % boundary) +            buf.write('Content-Disposition: form-data; name="%s"' % key) +            buf.write('\r\n\r\n' + value + '\r\n') +        for(key, fd) in files: +            #file_size = os.fstat(fd.fileno())[stat.ST_SIZE] +            filename = fd.name.split('/')[-1] +            contenttype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' +            buf.write('--%s\r\n' % boundary) +            buf.write('Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (key, filename)) +            buf.write('Content-Type: %s\r\n' % contenttype) +            # buffer += 'Content-Length: %s\r\n' % file_size +            fd.seek(0) +            buf.write('\r\n' + fd.read() + '\r\n') +        buf.write('--' + boundary + '--\r\n\r\n') +        buf = buf.getvalue() +        return boundary, buf +    multipart_encode = Callable(multipart_encode) + +    https_request = http_request + +def main(): +    import tempfile, sys + +    validatorURL = "http://validator.w3.org/check" +    opener = build_opener(MultipartPostHandler) + +    def validateFile(url): +        temp = tempfile.mkstemp(suffix=".html") +        write(temp[0], opener.open(url).read()) +        params = { "ss" : "0",            # show source +                   "doctype" : "Inline", +                   "uploaded_file" : open(temp[1], "rb") } +        print opener.open(validatorURL, params).read() +        remove(temp[1]) + +    if len(sys.argv[1:]) > 0: +        for arg in sys.argv[1:]: +            validateFile(arg) +    else: +        validateFile("http://www.google.com") + +if __name__=="__main__": +    main() diff --git a/module/lib/captchatrader.py b/module/lib/captchatrader.py new file mode 100644 index 000000000..171944c28 --- /dev/null +++ b/module/lib/captchatrader.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +    This program is free software; you can redistribute it and/or modify +    it under the terms of the GNU General Public License as published by +    the Free Software Foundation; either version 3 of the License, +    or (at your option) any later version. + +    This program is distributed in the hope that it will be useful, +    but WITHOUT ANY WARRANTY; without even the implied warranty of +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +    See the GNU General Public License for more details. + +    You should have received a copy of the GNU General Public License +    along with this program; if not, see <http://www.gnu.org/licenses/>. +     +    @author: mkaay +""" + +from json import loads +from urllib2 import build_opener +from MultipartPostHandler import MultipartPostHandler + +PYLOAD_KEY = "9f65e7f381c3af2b076ea680ae96b0b7" + +opener = build_opener(MultipartPostHandler) + +class CaptchaTraderException(Exception): +    def __init__(self, err): +        self.err = err +     +    def getCode(self): +        return self.err +     +    def __str__(self): +        return "<CaptchaTraderException %s>" % self.err +     +    def __repr__(self): +        return "<CaptchaTraderException %s>" % self.err + +class CaptchaTrader(): +    SUBMIT_URL = "http://captchatrader.com/api/submit" +    RESPOND_URL = "http://captchatrader.com/api/respond" +    GETCREDITS_URL = "http://captchatrader.com/api/get_credits/username:%(user)s/password:%(password)s/" +     +    def __init__(self, user, password, api_key=PYLOAD_KEY): +        self.api_key = api_key +        self.user = user +        self.password = password +     +    def getCredits(self): +        json = opener.open(CaptchaTrader.GETCREDITS_URL % {"user":self.user, "password":self.password}).read() +        response = loads(json) +        if response[0] < 0: +            raise CaptchaTraderException(response[1]) +        else: +            return response[1] +     +    def submit(self, captcha, captchaType="file", match=None): +        if not self.api_key: +            raise CaptchaTraderException("No API Key Specified!") +        if type(captcha) == str and captchaType == "file": +            raise CaptchaTraderException("Invalid Type") +        assert captchaType in ("file", "url-jpg", "url-jpeg", "url-png", "url-bmp") +        json = opener.open(CaptchaTrader.SUBMIT_URL, data={"api_key":self.api_key, +                                                       "username":self.user, +                                                       "password":self.password, +                                                       "value":captcha, +                                                       "type":captchaType}).read() +        response = loads(json) +        if response[0] < 0: +            raise CaptchaTraderException(response[1]) +         +        class Result(): +            def __init__(self, api, ticket, result): +                self.api = api +                self.ticket = ticket +                self.result = result +             +            def getTicketID(self): +                return self.ticket +             +            def getResult(self): +                return self.result +             +            def success(self): +                self.sendResponse(True) +             +            def fail(self): +                self.sendResponse(False) +             +            def sendResponse(self, success): +                self.api.respond(self.ticket, success) +         +        return Result(self, response[0], response[1]) +     +    def respond(self, ticket, success): +        json = opener.open(CaptchaTrader.RESPOND_URL, data={"is_correct":1 if success else 0, +                                                       "username":self.user, +                                                       "password":self.password, +                                                       "ticket":ticket}).read() +        response = loads(json) +        if response[0] < 0: +            raise CaptchaTraderException(response[1]) + +if __name__ == "__main__": +    ct = CaptchaTrader("<user>", "<password>") +    print "credits", ct.getCredits() +     +    print "testing..." +     +    result = ct.submit(open("test_captcha.jpg", "rb")) +    print "result", result.getResult() +    if result.getResult() == "bettand trifting": +        result.success() +        print "captcha recognized" +    else: +        result.fail() +        print "captcha not recognized" | 
