diff options
Diffstat (limited to 'module/plugins/hoster/BasePlugin.py')
-rw-r--r-- | module/plugins/hoster/BasePlugin.py | 136 |
1 files changed, 65 insertions, 71 deletions
diff --git a/module/plugins/hoster/BasePlugin.py b/module/plugins/hoster/BasePlugin.py index 9206aaabd..d0d8e7cc8 100644 --- a/module/plugins/hoster/BasePlugin.py +++ b/module/plugins/hoster/BasePlugin.py @@ -1,114 +1,108 @@ # -*- coding: utf-8 -*- -from urlparse import urlparse -from re import match, search +import re + from urllib import unquote +from urlparse import urljoin, urlparse from module.network.HTTPRequest import BadHeader +from module.plugins.internal.SimpleHoster import create_getInfo from module.plugins.Hoster import Hoster -from module.utils import html_unescape, remove_chars class BasePlugin(Hoster): - __name__ = "BasePlugin" - __type__ = "hoster" + __name__ = "BasePlugin" + __type__ = "hoster" + __version__ = "0.26" + __pattern__ = r'^unmatchable$' - __version__ = "0.19" + __description__ = """Base Plugin when any other didnt fit""" - __author_name__ = "RaNaN" - __author_mail__ = "RaNaN@pyload.org" + __license__ = "GPLv3" + __authors__ = [("RaNaN", "RaNaN@pyload.org"), + ("Walter Purcaro", "vuolter@gmail.com")] + + + @classmethod + def getInfo(cls, url="", html=""): #@TODO: Move to hoster class in 0.4.10 + return {'name': urlparse(unquote(url)).path.split('/')[-1] or _("Unknown"), 'size': 0, 'status': 3 if url else 1, 'url': unquote(url) or ""} def setup(self): self.chunkLimit = -1 self.resumeDownload = True + def process(self, pyfile): """main function""" - #debug part, for api exerciser - if pyfile.url.startswith("DEBUG_API"): - self.multiDL = False - return - - # self.__name__ = "NetloadIn" - # pyfile.name = "test" - # self.html = self.load("http://localhost:9000/short") - # self.download("http://localhost:9000/short") - # self.api = self.load("http://localhost:9000/short") - # self.decryptCaptcha("http://localhost:9000/captcha") - # - # if pyfile.url == "79": - # self.core.api.addPackage("test", [str(i) for i in xrange(80)], 1) - # - # return - if pyfile.url.startswith("http"): + pyfile.name = self.getInfo(pyfile.url)['name'] + if not pyfile.url.startswith("http"): + self.fail(_("No plugin matched")) + + for _i in xrange(5): try: self.downloadFile(pyfile) + except BadHeader, e: - if e.code in (401, 403): - self.logDebug("Auth required") + if e.code is 404: + self.offline() + + elif e.code in (401, 403): + self.logDebug("Auth required", "Received HTTP status code: %d" % e.code) account = self.core.accountManager.getAccountPlugin('Http') servers = [x['login'] for x in account.getAllAccounts()] - server = urlparse(pyfile.url).netloc + server = urlparse(pyfile.url).netloc if server in servers: self.logDebug("Logging on to %s" % server) self.req.addAuth(account.accounts[server]['password']) else: - for pwd in pyfile.package().password.splitlines(): - if ":" in pwd: - self.req.addAuth(pwd.strip()) - break + pwd = self.getPassword() + if ':' in pwd: + self.req.addAuth(pwd) else: - self.fail(_("Authorization required (username:password)")) - - self.downloadFile(pyfile) + self.fail(_("Authorization required")) else: - raise - + self.fail(e) + else: + break else: - self.fail("No Plugin matched and not a downloadable url.") + self.fail(_("No file downloaded")) #@TODO: Move to hoster class in 0.4.10 + + if self.checkDownload({'empty': re.compile(r"^$")}) is "empty": #@TODO: Move to hoster in 0.4.10 + self.fail(_("Empty file")) + def downloadFile(self, pyfile): url = pyfile.url - for _ in xrange(5): - header = self.load(url, just_header=True) + for i in xrange(1, 7): #@TODO: retrieve the pycurl.MAXREDIRS value set by req + header = self.load(url, ref=True, cookies=True, just_header=True, decode=True) - # self.load does not raise a BadHeader on 404 responses, do it here - if 'code' in header and header['code'] == 404: - raise BadHeader(404) - - if 'location' in header: - self.logDebug("Location: " + header['location']) - base = match(r'https?://[^/]+', url).group(0) - if header['location'].startswith("http"): - url = unquote(header['location']) - elif header['location'].startswith("/"): - url = base + unquote(header['location']) + if 'location' not in header or not header['location']: + if 'code' in header and header['code'] not in (200, 201, 203, 206): + self.logDebug("Received HTTP status code: %d" % header['code']) + self.fail(_("File not found")) else: - url = '%s/%s' % (base, unquote(header['location'])) + break + + location = header['location'] + + self.logDebug("Redirect #%d to: %s" % (i, location)) + + if urlparse(location).scheme: + url = location else: - break + p = urlparse(url) + base = "%s://%s" % (p.scheme, p.netloc) + url = urljoin(base, location) + else: + self.fail(_("Too many redirects")) + + self.download(unquote(url), disposition=True) + - name = html_unescape(unquote(urlparse(url).path.split("/")[-1])) - - if 'content-disposition' in header: - self.logDebug("Content-Disposition: " + header['content-disposition']) - m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", header['content-disposition']) - if m: - disp = m.groupdict() - self.logDebug(disp) - if not disp['enc']: - disp['enc'] = 'utf-8' - name = remove_chars(disp['name'], "\"';").strip() - name = unicode(unquote(name), disp['enc']) - - if not name: - name = url - pyfile.name = name - self.logDebug("Filename: %s" % pyfile.name) - self.download(url, disposition=True) +getInfo = create_getInfo(BasePlugin) |