diff options
Diffstat (limited to 'module/plugins/internal/SimpleHoster.py')
-rw-r--r-- | module/plugins/internal/SimpleHoster.py | 104 |
1 files changed, 65 insertions, 39 deletions
diff --git a/module/plugins/internal/SimpleHoster.py b/module/plugins/internal/SimpleHoster.py index c87a6160f..5a32ac943 100644 --- a/module/plugins/internal/SimpleHoster.py +++ b/module/plugins/internal/SimpleHoster.py @@ -10,6 +10,7 @@ from urlparse import urljoin, urlparse from module.PyFile import statusMap as _statusMap from module.network.CookieJar import CookieJar +from module.network.HTTPRequest import BadHeader from module.network.RequestFactory import getURL from module.plugins.Hoster import Hoster from module.plugins.Plugin import Fail @@ -126,7 +127,7 @@ def timestamp(): #@TODO: Move to hoster class in 0.4.10 -def _isDirectLink(self, url, resumable=False): +def directLink(self, url, resumable=False): link = "" for i in xrange(5 if resumable else 1): @@ -182,7 +183,7 @@ def secondsToMidnight(gmt=0): class SimpleHoster(Hoster): __name__ = "SimpleHoster" __type__ = "hoster" - __version__ = "0.90" + __version__ = "0.94" __pattern__ = r'^unmatchable$' @@ -244,31 +245,46 @@ class SimpleHoster(Hoster): CHECK_TRAFFIC = False #: Set to True to force checking traffic left for premium account DIRECT_LINK = None #: Set to True to looking for direct link (as defined in handleDirect method), set to None to do it if self.account is True else False MULTI_HOSTER = False #: Set to True to leech other hoster link (as defined in handleMulti method) + LOGIN_ACCOUNT = False #: Set to True to require account login + + directLink = directLink #@TODO: Remove in 0.4.10 @classmethod - def parseInfos(cls, urls): + def parseInfos(cls, urls): #@TODO: Built-in in 0.4.10 core, then remove from plugins for url in urls: url = replace_patterns(url, cls.FILE_URL_REPLACEMENTS if hasattr(cls, "FILE_URL_REPLACEMENTS") else cls.URL_REPLACEMENTS) #@TODO: Remove FILE_URL_REPLACEMENTS check in 0.4.10 yield cls.getInfo(url) @classmethod + def apiInfo(cls, url="", get={}, post={}): + url = unquote(url) + return {'name' : (urlparse(url).path.split('/')[-1] + or urlparse(url).query.split('=', 1)[::-1][0].split('&', 1)[0] + or _("Unknown")), + 'size' : 0, + 'status': 3 if url else 8, + 'url' : url} + + + @classmethod def getInfo(cls, url="", html=""): - info = {'name': urlparse(unquote(url)).path.split('/')[-1] or _("Unknown"), 'size': 0, 'status': 3, 'url': url} + info = cls.apiInfo(url) online = False try: - info['pattern'] = re.match(cls.__pattern__, url).groupdict() #: pattern groups will be saved here, please save api stuff to info['api'] + info['pattern'] = re.match(cls.__pattern__, url).groupdict() #: pattern groups will be saved here + except Exception: - pass + info['pattern'] = {} if not html: if not url: info['error'] = "missing url" info['status'] = 1 - else: + elif info['status'] is 3: try: html = getURL(url, cookies=cls.COOKIES, decode=not cls.TEXT_ENCODING) @@ -312,7 +328,7 @@ class SimpleHoster(Hoster): else: online = True - if 'pattern' in info and not info['pattern']: + if not info['pattern']: info.pop('pattern', None) if online: @@ -343,11 +359,16 @@ class SimpleHoster(Hoster): def prepare(self): + self.pyfile.error = "" #@TODO: Remove in 0.4.10 + self.info = {} self.link = "" #@TODO: Move to hoster class in 0.4.10 self.directDL = False #@TODO: Move to hoster class in 0.4.10 self.multihost = False #@TODO: Move to hoster class in 0.4.10 + if self.LOGIN_ACCOUNT and not self.account: + self.fail(_("Required account not found")) + self.req.setOption("timeout", 120) if isinstance(self.COOKIES, list): @@ -397,11 +418,11 @@ class SimpleHoster(Hoster): if self.premium and (not self.CHECK_TRAFFIC or self.checkTrafficLeft()): self.logDebug("Handled as premium download") - self.handlePremium() + self.handlePremium(pyfile) - else: + elif not self.LOGIN_ACCOUNT or (not self.CHECK_TRAFFIC or self.checkTrafficLeft()): self.logDebug("Handled as free download") - self.handleFree() + self.handleFree(pyfile) self.downloadLink(self.link) self.checkFile() @@ -410,7 +431,7 @@ class SimpleHoster(Hoster): def downloadLink(self, link): if link and isinstance(link, basestring): self.correctCaptcha() - self.download(link, disposition=True) + self.download(link, disposition=False) #@TODO: Set `disposition=True` in 0.4.10 def checkFile(self): @@ -420,15 +441,12 @@ class SimpleHoster(Hoster): elif not self.lastDownload or not exists(fs_encode(self.lastDownload)): self.lastDownload = "" - - errmsg = _("No file downloaded") - if 'error' in self.info: - self.fail(errmsg, self.info['error']) - else: - self.fail(errmsg) + self.fail(self.pyfile.error or _("No file downloaded")) else: - rules = {'empty file': re.compile(r"^$")} + rules = {'empty file': re.compile(r'\A\Z'), + 'html file' : re.compile(r'\A\s*<!DOCTYPE html'), + 'html error': re.compile(r'\A\s*(<.+>)?\d{3}(\Z|\s+)')} if hasattr(self, 'ERROR_PATTERN'): rules['error'] = re.compile(self.ERROR_PATTERN) @@ -444,6 +462,10 @@ class SimpleHoster(Hoster): def checkErrors(self): + if not self.html: + self.logWarning(_("No html code to check")) + return + if hasattr(self, 'PREMIUM_ONLY_PATTERN') and self.premium and re.search(self.PREMIUM_ONLY_PATTERN, self.html): self.fail(_("Link require a premium account to be handled")) @@ -465,42 +487,44 @@ class SimpleHoster(Hoster): def checkStatus(self, getinfo=True): - if getinfo: + if not self.info or getinfo: self.logDebug("File info (BEFORE): %s" % self.info) self.info.update(self.getInfo(self.pyfile.url, self.html)) - if 'status' not in self.info: - return + try: + status = self.info['status'] - status = self.info['status'] + if status is 1: + self.offline() - if status is 1: - self.offline() + elif status is 6: + self.tempOffline() - elif status is 6: - self.tempOffline() + elif status is 8: + self.fail() - elif status is not 2: + finally: self.logDebug("File status: %s" % statusMap[status], "File info: %s" % self.info) def checkNameSize(self, getinfo=True): - if getinfo: + if not self.info or getinfo: self.logDebug("File info (BEFORE): %s" % self.info) self.info.update(self.getInfo(self.pyfile.url, self.html)) self.logDebug("File info (AFTER): %s" % self.info) try: - name = self.info['name'] - size = self.info['size'] url = self.info['url'] - + name = self.info['name'] if name and name != url: self.pyfile.name = name - else: - self.pyfile.name = name = self.info['name'] = urlparse(name).path.split('/')[-1] + except Exception: + pass + + try: + size = self.info['size'] if size > 0: self.pyfile.size = size @@ -529,7 +553,7 @@ class SimpleHoster(Hoster): def handleDirect(self, pyfile): - link = _isDirectLink(self, pyfile.url, self.resumeDownload) + link = self.directLink(pyfile.url, self.resumeDownload) if link: self.logInfo(_("Direct download link detected")) @@ -543,9 +567,9 @@ class SimpleHoster(Hoster): pass - def handleFree(self, pyfile=None): + def handleFree(self, pyfile): if not hasattr(self, 'LINK_FREE_PATTERN'): - self.fail(_("Free download not implemented")) + self.logError(_("Free download not implemented")) try: m = re.search(self.LINK_FREE_PATTERN, self.html) @@ -558,9 +582,11 @@ class SimpleHoster(Hoster): self.fail(e) - def handlePremium(self, pyfile=None): + def handlePremium(self, pyfile): if not hasattr(self, 'LINK_PREMIUM_PATTERN'): - self.fail(_("Premium download not implemented")) + self.logError(_("Premium download not implemented")) + self.logDebug("Handled as free download") + self.handleFree(pyfile) try: m = re.search(self.LINK_PREMIUM_PATTERN, self.html) |