diff options
| author | 2013-10-29 14:45:07 +0100 | |
|---|---|---|
| committer | 2013-10-29 14:45:07 +0100 | |
| commit | e64a821f51c57fc6fb516a115f69d7d3c939e15e (patch) | |
| tree | 5ef15df2bc734ffdcfc2599028cdb2ff75eb93aa /module/plugins/internal | |
| parent | New hoster and folder: DuploadOrg (diff) | |
| download | pyload-e64a821f51c57fc6fb516a115f69d7d3c939e15e.tar.xz | |
SimpleHoster: new PREMIUM_ONLY_PATTERN (see #350).
+ cosmetics
Diffstat (limited to 'module/plugins/internal')
| -rw-r--r-- | module/plugins/internal/SimpleHoster.py | 74 | 
1 files changed, 48 insertions, 26 deletions
| diff --git a/module/plugins/internal/SimpleHoster.py b/module/plugins/internal/SimpleHoster.py index 53f2e9f19..856d3fde6 100644 --- a/module/plugins/internal/SimpleHoster.py +++ b/module/plugins/internal/SimpleHoster.py @@ -25,6 +25,7 @@ from module.utils import html_unescape, fixup, parseFileSize  from module.network.RequestFactory import getURL  from module.network.CookieJar import CookieJar +  def replace_patterns(string, ruleslist):      for r in ruleslist:          rf, rt = r @@ -32,18 +33,22 @@ def replace_patterns(string, ruleslist):          #self.logDebug(rf, rt, string)      return string +  def set_cookies(cj, cookies):      for cookie in cookies:          if isinstance(cookie, tuple) and len(cookie) == 3:              domain, name, value = cookie              cj.setCookie(domain, name, value) +  def parseHtmlTagAttrValue(attr_name, tag): -        m = re.search(r"%s\s*=\s*([\"']?)((?<=\")[^\"]+|(?<=')[^']+|[^>\s\"'][^>\s]*)\1" % attr_name, tag, re.I) -        return m.group(2) if m else None +    m = re.search(r"%s\s*=\s*([\"']?)((?<=\")[^\"]+|(?<=')[^']+|[^>\s\"'][^>\s]*)\1" % attr_name, tag, re.I) +    return m.group(2) if m else None +  def parseHtmlForm(attr_str, html, input_names=None): -    for form in re.finditer(r"(?P<tag><form[^>]*%s[^>]*>)(?P<content>.*?)</?(form|body|html)[^>]*>" % attr_str, html, re.S | re.I): +    for form in re.finditer(r"(?P<tag><form[^>]*%s[^>]*>)(?P<content>.*?)</?(form|body|html)[^>]*>" % attr_str, +                            html, re.S | re.I):          inputs = {}          action = parseHtmlTagAttrValue("action", form.group('tag'))          for inputtag in re.finditer(r'(<(input|textarea)[^>]*>)([^<]*(?=</\2)|)', form.group('content'), re.S | re.I): @@ -65,19 +70,20 @@ def parseHtmlForm(attr_str, html, input_names=None):                          continue                      elif hasattr(val, "search") and re.match(val, inputs[key]):                          continue -                    break # attibute value does not match +                    break  # attibute value does not match                  else: -                    break # attibute name does not match +                    break  # attibute name does not match              else: -                return action, inputs # passed attribute check +                return action, inputs  # passed attribute check          else:              # no attribute check              return action, inputs -    return {}, None # no matching form found +    return {}, None  # no matching form found + -def parseFileInfo(self, url = '', html = ''): -    info = {"name" : url, "size" : 0, "status" : 3} +def parseFileInfo(self, url='', html=''): +    info = {"name": url, "size": 0, "status": 3}      if hasattr(self, "pyfile"):          url = self.pyfile.url @@ -85,10 +91,12 @@ def parseFileInfo(self, url = '', html = ''):      if hasattr(self, "req") and self.req.http.code == '404':          info['status'] = 1      else: -        if not html and hasattr(self, "html"): html = self.html +        if not html and hasattr(self, "html"): +            html = self.html          if isinstance(self.SH_BROKEN_ENCODING, (str, unicode)):              html = unicode(html, self.SH_BROKEN_ENCODING) -            if hasattr(self, "html"): self.html = html +            if hasattr(self, "html"): +                self.html = html          if hasattr(self, "FILE_OFFLINE_PATTERN") and re.search(self.FILE_OFFLINE_PATTERN, html):              # File offline @@ -113,10 +121,12 @@ def parseFileInfo(self, url = '', html = ''):                  if 'N' in info:                      info['name'] = replace_patterns(info['N'], self.FILE_NAME_REPLACEMENTS)                  if 'S' in info: -                    size = replace_patterns(info['S'] + info['U'] if 'U' in info else info['S'], self.FILE_SIZE_REPLACEMENTS) +                    size = replace_patterns(info['S'] + info['U'] if 'U' in info else info['S'], +                                            self.FILE_SIZE_REPLACEMENTS)                      info['size'] = parseFileSize(size)                  elif isinstance(info['size'], (str, unicode)): -                    if 'units' in info: info['size'] += info['units'] +                    if 'units' in info: +                        info['size'] += info['units']                      info['size'] = parseFileSize(info['size'])      if hasattr(self, "file_info"): @@ -124,34 +134,41 @@ def parseFileInfo(self, url = '', html = ''):      return info['name'], info['size'], info['status'], url +  def create_getInfo(plugin):      def getInfo(urls):          for url in urls:              cj = CookieJar(plugin.__name__) -            if isinstance(plugin.SH_COOKIES, list): set_cookies(cj, plugin.SH_COOKIES) +            if isinstance(plugin.SH_COOKIES, list): +                set_cookies(cj, plugin.SH_COOKIES)              file_info = parseFileInfo(plugin, url, getURL(replace_patterns(url, plugin.FILE_URL_REPLACEMENTS), -                                                          decode = not plugin.SH_BROKEN_ENCODING, cookies = cj)) +                                                          decode=not plugin.SH_BROKEN_ENCODING, cookies=cj))              yield file_info +      return getInfo +  def timestamp(): -    return int(time()*1000) +    return int(time() * 1000) +  class PluginParseError(Exception):      def __init__(self, msg):          Exception.__init__(self)          self.value = 'Parse error (%s) - plugin may be out of date' % msg +      def __str__(self):          return repr(self.value) +  class SimpleHoster(Hoster):      __name__ = "SimpleHoster" -    __version__ = "0.30" +    __version__ = "0.31"      __pattern__ = None      __type__ = "hoster"      __description__ = """Base hoster plugin""" -    __author_name__ = ("zoidberg") -    __author_mail__ = ("zoidberg@mujmail.cz") +    __author_name__ = ("zoidberg", "stickell") +    __author_mail__ = ("zoidberg@mujmail.cz", "l.stickell@yahoo.it")      """      These patterns should be defined by each hoster:      FILE_INFO_PATTERN = r'(?P<N>file_name) (?P<S>file_size) (?P<U>units)' @@ -159,15 +176,17 @@ class SimpleHoster(Hoster):      and FILE_SIZE_PATTERN = r'(?P<S>file_size) (?P<U>units)'      FILE_OFFLINE_PATTERN = r'File (deleted|not found)'      TEMP_OFFLINE_PATTERN = r'Server maintainance' + +    You can also define a PREMIUM_ONLY_PATTERN to detect links that can be downloaded only with a premium account.      """      FILE_SIZE_REPLACEMENTS = []      FILE_NAME_REPLACEMENTS = [("&#?\w+;", fixup)]      FILE_URL_REPLACEMENTS = [] -    SH_BROKEN_ENCODING = False # Set to True or encoding name if encoding in http header is not correct -    SH_COOKIES = True # or False or list of tuples [(domain, name, value)] -    SH_CHECK_TRAFFIC = False # True = force check traffic left for a premium account +    SH_BROKEN_ENCODING = False  # Set to True or encoding name if encoding in http header is not correct +    SH_COOKIES = True  # or False or list of tuples [(domain, name, value)] +    SH_CHECK_TRAFFIC = False  # True = force check traffic left for a premium account      def init(self):          self.file_info = {} @@ -189,11 +208,14 @@ class SimpleHoster(Hoster):          else:              # This line is required due to the getURL workaround. Can be removed in 0.5              self.html = self.load(pyfile.url, decode=not self.SH_BROKEN_ENCODING, cookies=self.SH_COOKIES) +            if hasattr(self, 'PREMIUM_ONLY_PATTERN') and re.search(self.PREMIUM_ONLY_PATTERN, self.html): +                self.fail("This link require a premium account")              self.handleFree()      def load(self, url, get={}, post={}, ref=True, cookies=True, just_header=False, decode=False):          if type(url) == unicode: url = url.encode('utf8') -        return Hoster.load(self, url=url, get=get, post=post, ref=ref, cookies=cookies, just_header=just_header, decode=decode) +        return Hoster.load(self, url=url, get=get, post=post, ref=ref, cookies=cookies, +                           just_header=just_header, decode=decode)      def getFileInfo(self):          self.logDebug("URL: %s" % self.pyfile.url) @@ -230,7 +252,7 @@ class SimpleHoster(Hoster):      def parseError(self, msg):          raise PluginParseError(msg) -    def longWait(self, wait_time = None, max_tries = 3): +    def longWait(self, wait_time=None, max_tries=3):          if wait_time and isinstance(wait_time, (int, long, float)):              time_str = "%dh %dm" % divmod(wait_time / 60, 60)          else: @@ -242,7 +264,7 @@ class SimpleHoster(Hoster):          self.setWait(wait_time, True)          self.wait() -        self.retry(max_tries = max_tries, reason="Download limit reached") +        self.retry(max_tries=max_tries, reason="Download limit reached")      def parseHtmlForm(self, attr_str='', input_names=None):          return parseHtmlForm(attr_str, self.html, input_names) @@ -253,4 +275,4 @@ class SimpleHoster(Hoster):              return True          size = self.pyfile.size / 1024          self.logInfo("Filesize: %i KiB, Traffic left for user %s: %i KiB" % (size, self.user, traffic)) -        return  size <= traffic
\ No newline at end of file +        return size <= traffic | 
