diff options
author | Willnix <willnix@pyload.org> | 2009-06-11 00:11:18 +0200 |
---|---|---|
committer | Willnix <willnix@pyload.org> | 2009-06-11 00:11:18 +0200 |
commit | e6f056dece47ef23313faf5a691a84ad6ec72219 (patch) | |
tree | c88e1d9c823e5bcb6193ae3818db6c2bd83b0c46 | |
parent | Fixed the YouPorn.com Plugin (diff) | |
download | pyload-e6f056dece47ef23313faf5a691a84ad6ec72219.tar.xz |
Added CCF Plugin, closed #8
-rw-r--r-- | Plugins/CCF.py | 60 | ||||
-rw-r--r-- | module/network/MultipartPostHandler.py | 139 |
2 files changed, 199 insertions, 0 deletions
diff --git a/Plugins/CCF.py b/Plugins/CCF.py new file mode 100644 index 000000000..9fe3a8546 --- /dev/null +++ b/Plugins/CCF.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import urllib2 +from module.network.MultipartPostHandler import MultipartPostHandler +import tempfile +import os.path +import random +import re + +from Plugin import Plugin + +class CCF(Plugin): + + def __init__(self, parent): + Plugin.__init__(self, parent) + props = {} + props['name'] = "CCF" + props['type'] = "container" + props['pattern'] = r".*\.ccf" + props['version'] = "0.1" + props['description'] = """CCF Container Convert Plugin""" + props['author_name'] = ("Willnix") + props['author_mail'] = ("Willnix@pyload.org") + self.props = props + self.parent = parent + self.multi_dl = True + self.links = [] + + def file_exists(self): + """ returns True or False + """ + return True + + def proceed(self, url, location): + infile = url.replace("\n", "") + + opener = urllib2.build_opener(MultipartPostHandler) + params = { "src" : "ccf", + "filename" : "test.ccf", + "upload" : open(infile, "rb") } + tempdlc_content = opener.open('http://service.jdownloader.net/dlcrypt/getDLC.php', params).read() + + random.seed() + tempdir = tempfile.gettempdir() + if tempdir[0] == '/': + delim = '/' + else: + delim = '\\' + tempdlc_name = tempdir+delim+str(random.randint(0, 100))+'-tmp.dlc' + while os.path.exists(tempdlc_name): + tempdlc_name = tempfile.gettempdir()+'/'+str(random.randint(0, 100))+'-tmp.dlc' + + tempdlc = open(tempdlc_name,"w") + tempdlc.write(re.search(r'<dlc>(.*)</dlc>', tempdlc_content, re.DOTALL).group(1)) + tempdlc.close + + self.links.append(tempdlc_name) + + return True diff --git a/module/network/MultipartPostHandler.py b/module/network/MultipartPostHandler.py new file mode 100644 index 000000000..f25c6fa1b --- /dev/null +++ b/module/network/MultipartPostHandler.py @@ -0,0 +1,139 @@ +#!/usr/bin/python + +#### +# 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. +""" + +import urllib +import urllib2 +import mimetools, mimetypes +import os, stat +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(urllib2.BaseHandler): + handler_order = urllib2.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 = urllib.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 = urllib2.build_opener(MultipartPostHandler) + + def validateFile(url): + temp = tempfile.mkstemp(suffix=".html") + os.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() + os.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() + |