From b8e23365241337ebb8a42a0d93cbc67afbbee480 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 18 Oct 2015 19:08:02 +0200 Subject: [Plugin] Minimize code + spare fixes --- module/plugins/internal/MultiHoster.py | 56 ++---- module/plugins/internal/Plugin.py | 312 +++----------------------------- module/plugins/internal/SimpleHoster.py | 117 ++++-------- 3 files changed, 66 insertions(+), 419 deletions(-) (limited to 'module/plugins/internal') diff --git a/module/plugins/internal/MultiHoster.py b/module/plugins/internal/MultiHoster.py index 5655571b8..a31a6843b 100644 --- a/module/plugins/internal/MultiHoster.py +++ b/module/plugins/internal/MultiHoster.py @@ -13,11 +13,11 @@ class MultiHoster(SimpleHoster): __status__ = "testing" __pattern__ = r'^unmatchable$' - __config__ = [("activated" , "bool", "Activated" , True), - ("use_premium" , "bool", "Use premium account if available" , True), - ("fallback_premium", "bool", "Fallback to free download if premium fails", True), - ("chk_filesize" , "bool", "Check file size" , True), - ("revertfailed" , "bool", "Revert to standard download if fails" , True)] + __config__ = [("activated" , "bool", "Activated" , True), + ("use_premium" , "bool", "Use premium account if available" , True), + ("fallback" , "bool", "Fallback to free download if premium fails", True), + ("chk_filesize", "bool", "Check file size" , True), + ("revertfailed", "bool", "Revert to standard download if fails" , True)] __description__ = """Multi hoster plugin""" __license__ = "GPLv3" @@ -66,47 +66,13 @@ class MultiHoster(SimpleHoster): self.direct_dl = direct_dl - def process(self, pyfile): + def _process(self, thread): try: - self.prepare() - self.check_info() #@TODO: Remove in 0.4.10 + super(MultiHoster, self)._process(thread) - if self.direct_dl: - self.log_info(_("Looking for direct download link...")) - self.handle_direct(pyfile) - - if self.link or was_downloaded(): - self.log_info(_("Direct download link detected")) - else: - self.log_info(_("Direct download link not found")) - - if not self.link and not self.last_download: - self.preload() - - self.check_errors() - self.check_status(getinfo=False) - - if self.premium and (not self.CHECK_TRAFFIC or self.check_traffic()): - self.log_info(_("Processing as premium download...")) - self.handle_premium(pyfile) - - elif not self.LOGIN_ACCOUNT or (not self.CHECK_TRAFFIC or self.check_traffic()): - self.log_info(_("Processing as free download...")) - self.handle_free(pyfile) - - if not self.last_download: - self.log_info(_("Downloading file...")) - self.download(self.link, disposition=self.DISPOSITION) - - self.check_download() - - except Fail, e: #@TODO: Move to PluginThread in 0.4.10 - if self.premium: - self.log_warning(_("Premium download failed")) - self.restart(premium=False) - - elif self.get_config("revertfailed", True) and \ - self.pyload.pluginManager.hosterPlugins[self.classname].get('new_module'): + except Fail, e: + if self.get_config("revertfailed", True) and \ + self.pyload.pluginManager.hosterPlugins[self.classname].get('new_module'): hdict = self.pyload.pluginManager.hosterPlugins[self.classname] tmp_module = hdict['new_module'] @@ -122,7 +88,7 @@ class MultiHoster(SimpleHoster): self.restart(_("Revert to original hoster plugin")) else: - raise Fail(encode(e)) #@TODO: Remove `encode` in 0.4.10 + raise Fail(e) def handle_premium(self, pyfile): diff --git a/module/plugins/internal/Plugin.py b/module/plugins/internal/Plugin.py index f5db49d8b..0b5561df8 100644 --- a/module/plugins/internal/Plugin.py +++ b/module/plugins/internal/Plugin.py @@ -2,284 +2,19 @@ from __future__ import with_statement -import datetime import inspect import os -import re -import sys -import time -import traceback -import urllib -import urlparse - -import pycurl if os.name is not "nt": import grp import pwd -from module.common.json_layer import json_dumps, json_loads -from module.plugins.Plugin import Abort, Fail, Reconnect, Retry, SkipDownload as Skip #@TODO: Remove in 0.4.10 -from module.utils import (fs_encode, fs_decode, get_console_encoding, html_unescape, - parseFileSize as parse_size, save_join as fs_join) - - -#@TODO: Move to utils in 0.4.10 -def isiterable(obj): - return hasattr(obj, "__iter__") - - -#@TODO: Move to utils in 0.4.10 -def decode(string, encoding=None): - """Encoded string (default to UTF-8) -> unicode string""" - if type(string) is str: - try: - res = unicode(string, encoding or "utf-8") - - except UnicodeDecodeError, e: - if encoding: - raise UnicodeDecodeError(e) - - encoding = get_console_encoding(sys.stdout.encoding) - res = unicode(string, encoding) - - elif type(string) is unicode: - res = string - - else: - res = unicode(string) - - return res - - -#@TODO: Remove in 0.4.10 -def _decode(*args, **kwargs): - return decode(*args, **kwargs) - - -#@TODO: Move to utils in 0.4.10 -def encode(string, encoding=None, decoding=None): - """Unicode or decoded string -> encoded string (default to UTF-8)""" - if type(string) is unicode: - res = string.encode(encoding or "utf-8") - - elif type(string) is str: - res = encode(decode(string, decoding), encoding) - - else: - res = str(string) - - return res - - -#@TODO: Move to utils in 0.4.10 -def exists(path): - if os.path.exists(path): - if os.name is "nt": - dir, name = os.path.split(path.rstrip(os.sep)) - return name in os.listdir(dir) - else: - return True - else: - return False - - -def fixurl(url, unquote=None): - old = url - url = urllib.unquote(url) - - if unquote is None: - unquote = url is old - - url = html_unescape(decode(url).decode('unicode-escape')) - url = re.sub(r'(?\s\"'][^>\s]*)\1" % attr_name, tag, re.I) - return m.group(2) if m else None - - -def parse_html_form(attr_str, html, input_names={}): - for form in re.finditer(r"(?P]*%s[^>]*>)(?P.*?)]*>" % attr_str, - html, re.I | re.S): - inputs = {} - action = parse_html_tag_attr_value("action", form.group('TAG')) - - for inputtag in re.finditer(r'(<(input|textarea)[^>]*>)([^<]*(?= 300) + self.wait(wait_time, reconnect=wait_time > self.get_config("max_wait", 10) * 60) self.restart(_("Download limit exceeded")) if self.HAPPY_HOUR_PATTERN and re.search(self.HAPPY_HOUR_PATTERN, self.html): @@ -369,7 +374,7 @@ class SimpleHoster(Hoster): if re.search('limit|wait|slot', errmsg, re.I): wait_time = parse_time(errmsg) - self.wait(wait_time, reconnect=wait_time > 300) + self.wait(wait_time, reconnect=wait_time > self.get_config("max_wait", 10) * 60) self.restart(_("Download limit exceeded")) elif re.search('country|ip|region|nation', errmsg, re.I): @@ -410,80 +415,20 @@ class SimpleHoster(Hoster): waitmsg = m.group(0).strip() wait_time = parse_time(waitmsg) - self.wait(wait_time, reconnect=wait_time > 300) + self.wait(wait_time, reconnect=wait_time > self.get_config("max_wait", 10) * 60) self.info.pop('error', None) - def check_status(self, getinfo=True): - if not self.info or getinfo: - self.log_info(_("Updating file info...")) - old_info = self.info.copy() - self.info.update(self.get_info(self.pyfile.url, self.html)) - self.log_debug("File info: %s" % self.info) - self.log_debug("Previous file info: %s" % old_info) - - try: - status = self.info['status'] or 14 - - if status is 1: - self.offline() - - elif status is 6: - self.temp_offline() - - elif status is 8: - self.fail() - - finally: - self.log_info(_("File status: ") + self.pyfile.getStatusName()) - - - def check_name_size(self, getinfo=True): - if not self.info or getinfo: - self.log_info(_("Updating file info...")) - old_info = self.info.copy() - self.info.update(self.get_info(self.pyfile.url, self.html)) - self.log_debug("File info: %s" % self.info) - self.log_debug("Previous file info: %s" % old_info) - - name = self.info.get('name') - size = self.info.get('size') - - if name and name is not self.info.get('url'): - self.pyfile.name = name - else: - name = self.pyfile.name - - if size > 0: - self.pyfile.size = int(self.info['size']) #@TODO: Fix int conversion in 0.4.10 - else: - size = self.pyfile.size - - self.log_info(_("File name: ") + name) - self.log_info(_("File size: %s bytes") % size or "N/D") - - - #@TODO: Rewrite in 0.4.10 - def check_info(self): - self.check_name_size() - - if self.html: - self.check_errors() - self.check_name_size() - - self.check_status(getinfo=False) - - #: Deprecated method (Remove in 0.4.10) def get_fileInfo(self): - self.info = {} - self.check_info() + self.info.clear() + self.grab_info() return self.info def handle_direct(self, pyfile): - self.link = self.direct_link(pyfile.url, self.resume_download) + self.link = self.is_download(pyfile.url) def handle_multi(self, pyfile): #: Multi-hoster handler @@ -492,7 +437,7 @@ class SimpleHoster(Hoster): def handle_free(self, pyfile): if not self.LINK_FREE_PATTERN: - self.log_error(_("Free download not implemented")) + self.log_warning(_("Free download not implemented")) m = re.search(self.LINK_FREE_PATTERN, self.html) if m is None: @@ -503,7 +448,7 @@ class SimpleHoster(Hoster): def handle_premium(self, pyfile): if not self.LINK_PREMIUM_PATTERN: - self.log_error(_("Premium download not implemented")) + self.log_warning(_("Premium download not implemented")) self.restart(premium=False) m = re.search(self.LINK_PREMIUM_PATTERN, self.html) -- cgit v1.2.3