diff options
author | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-07-31 10:38:51 +0200 |
---|---|---|
committer | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-07-31 10:38:51 +0200 |
commit | cc3c575a68fd4b585f0e4af586e027fddb0fe605 (patch) | |
tree | 511a34c2f26d8f3ab012fb34ddce3b80fb93d531 /module | |
parent | Fix https://github.com/pyload/pyload/issues/1628 (diff) | |
download | pyload-cc3c575a68fd4b585f0e4af586e027fddb0fe605.tar.xz |
Fix and improve account logic
Diffstat (limited to 'module')
-rw-r--r-- | module/plugins/internal/Account.py | 71 | ||||
-rw-r--r-- | module/plugins/internal/Hoster.py | 92 | ||||
-rw-r--r-- | module/plugins/internal/MultiHook.py | 5 | ||||
-rw-r--r-- | module/plugins/internal/SimpleCrypter.py | 12 | ||||
-rw-r--r-- | module/plugins/internal/SimpleHoster.py | 2 |
5 files changed, 89 insertions, 93 deletions
diff --git a/module/plugins/internal/Account.py b/module/plugins/internal/Account.py index a33cdffac..a402c4ad7 100644 --- a/module/plugins/internal/Account.py +++ b/module/plugins/internal/Account.py @@ -13,7 +13,7 @@ from module.utils import compare_time, lock, parseFileSize as parse_size class Account(Plugin): __name__ = "Account" __type__ = "account" - __version__ = "0.11" + __version__ = "0.12" __status__ = "testing" __description__ = """Base account plugin""" @@ -23,14 +23,14 @@ class Account(Plugin): LOGIN_TIMEOUT = 10 * 60 #: After that time (in minutes) pyload will relogin the account - INFO_THRESHOLD = 10 * 60 #: After that time (in minutes) account data will be reloaded + INFO_THRESHOLD = 30 * 60 #: After that time (in minutes) account data will be reloaded def __init__(self, manager, accounts): - self.pyload = manager.core - self.info = {} #: Provide information in dict here - self.lock = threading.RLock() - + self.pyload = manager.core + self.info = {} #: Provide information in dict here + self.lock = threading.RLock() + self.req = None self.accounts = accounts #@TODO: Remove in 0.4.10 self.init() @@ -73,9 +73,7 @@ class Account(Plugin): self.accounts[user]['valid'] = True #@TODO: Remove in 0.4.10 finally: - if self.req: - self.req.close() - del self.req + self.del_request() return res @@ -86,7 +84,7 @@ class Account(Plugin): req = self.get_request(user) if req: req.clearCookies() - req.close() + self.del_request() if user in self.info: self.info[user]['login'].clear() @@ -110,8 +108,7 @@ class Account(Plugin): 'premium' : None, 'validuntil' : None, 'trafficleft': None, - 'maxtraffic' : None, - 'timestamp' : 0}} + 'maxtraffic' : None}} #@TODO: Remove in 0.4.10 self.accounts[user] = self.info[user]['data'] @@ -133,7 +130,7 @@ class Account(Plugin): """ Updates account and return true if anything changed """ - if not password or not options: + if not (password or options): return if user not in self.info: @@ -173,6 +170,7 @@ class Account(Plugin): return self.remove(*args, **kwargs) + #@NOTE: Remove in 0.4.10? def get_data(self, user, reload=False): if not user: return @@ -213,18 +211,18 @@ class Account(Plugin): self.log_info(_("Parsing account info for user `%s`...") % user) info = self._parse_info(user) + safe_info = copy.deepcopy(info) + safe_info['login']['password'] = "**********" + safe_info['data']['password'] = "**********" #@TODO: Remove in 0.4.10 + self.log_debug("Account info for user `%s`: %s" % (user, safe_info)) + else: info = self.info[user] - if self.INFO_THRESHOLD > 0 and info['data']['timestamp'] + self.INFO_THRESHOLD * 60 < time.time(): + if self.INFO_THRESHOLD > 0 and info['login']['timestamp'] + self.INFO_THRESHOLD < time.time(): self.log_debug("Reached data timeout for %s" % user) self.schedule_refresh(user) - safe_info = copy.deepcopy(info) - safe_info['login']['password'] = "**********" - safe_info['data']['password'] = "**********" #@TODO: Remove in 0.4.10 - self.log_debug("Account info for user `%s`: %s" % (user, safe_info)) - return info @@ -232,32 +230,31 @@ class Account(Plugin): if not user: return False - info = self.get_info(user, reload) - return info.get('data', {}).get('premium', False) + info = self.get_info(user) + return info['data']['premium'] def _parse_info(self, user): info = self.info[user] - try: - info['data']['timestamp'] = time.time() #: Set timestamp for login + if not info['login']['valid']: + return info + try: self.req = self.get_request(user) extra_info = self.parse_info(user, info['login']['password'], info, self.req) if extra_info and isinstance(extra_info, dict): info['data'].update(extra_info) - except Exception, e: + except (Fail, Exception), e: self.log_warning(_("Error loading info for user `%s`") % user, e) if self.pyload.debug: traceback.print_exc() finally: - if self.req: - self.req.close() - del self.req + self.del_request() self.info[user].update(info) return info @@ -291,6 +288,13 @@ class Account(Plugin): return self.pyload.requestFactory.getRequest(self.__name__, user) + def del_request(self): + try: + self.req.close() + finally: + self.req = None + + def get_cookies(self, user=None): if not user: user, info = self.select() @@ -350,10 +354,6 @@ class Account(Plugin): reverse=True)[0] - def can_use(self): - return self.select() != (None, None) - - def parse_traffic(self, value, unit=None): #: Return kilobytes if not unit and not isinstance(value, basestring): unit = "KB" @@ -392,20 +392,19 @@ class Account(Plugin): #: Deprecated method, use `schedule_refresh` instead (Remove in 0.4.10) def scheduleRefresh(self, *args, **kwargs): if 'force' in kwargs: - kwargs['reload'] = kwargs['force'] - kwargs.pop('force', None) + kwargs.pop('force', None) #@TODO: Recheck in 0.4.10 return self.schedule_refresh(*args, **kwargs) @lock - def is_logged(self, user): + def is_logged(self, user, relogin=False): """ Checks if user is still logged in """ if user in self.info: - if self.LOGIN_TIMEOUT > 0 and self.info[user]['login']['timestamp'] + self.LOGIN_TIMEOUT * 60 < time.time(): + if self.LOGIN_TIMEOUT > 0 and self.info[user]['login']['timestamp'] + self.LOGIN_TIMEOUT < time.time(): self.log_debug("Reached login timeout for %s" % user) - return self.relogin(user) + return self.relogin(user) if relogin else False else: return True else: diff --git a/module/plugins/internal/Hoster.py b/module/plugins/internal/Hoster.py index 4d69720ac..1aea3804d 100644 --- a/module/plugins/internal/Hoster.py +++ b/module/plugins/internal/Hoster.py @@ -43,7 +43,7 @@ def create_getInfo(klass): class Hoster(Plugin): __name__ = "Hoster" __type__ = "hoster" - __version__ = "0.13" + __version__ = "0.14" __status__ = "testing" __pattern__ = r'^unmatchable$' @@ -68,43 +68,14 @@ class Hoster(Plugin): self.multiDL = True #@TODO: Change to `multi_dl` in 0.4.10 self.limitDL = 0 #@TODO: Change to `limit_dl` in 0.4.10 - #: Chunk limit - self.chunk_limit = 1 - self.resume_download = False - #: time.time() + wait in seconds self.wait_until = 0 - self.waiting = False - - #: Captcha reader instance - self.ocr = None + self.waiting = False #: Account handler instance, see :py:class:`Account` - self.account = self.pyload.accountManager.getAccountPlugin(self.__name__) - - #: Premium status - self.premium = False - - #: username/login - self.user = None - - if self.account and not self.account.can_use(): - self.account = None - - if self.account: - self.user, data = self.account.select() - - #: Browser instance, see `network.Browser` - self.req = self.account.get_request(self.user) - self.chunk_limit = -1 #: Chunk limit, -1 for unlimited - - #: Enables resume (will be ignored if server dont accept chunks) - self.resume_download = True - - #: Premium status - self.premium = self.account.is_premium(self.user) - else: - self.req = self.pyload.requestFactory.getRequest(self.__name__) + self.account = None + self.user = None + self.req = None #: Associated pyfile instance, see `PyFile` self.pyfile = pyfile @@ -129,6 +100,7 @@ class Hoster(Plugin): #: Dict of the amount of retries already made self.retries = {} + self._setup() self.init() @@ -136,9 +108,9 @@ class Hoster(Plugin): def get_info(cls, url="", html=""): url = _fixurl(url) url_p = urlparse.urlparse(url) - return {'name' : (url_p.path.split('/')[-1] - or url_p.query.split('=', 1)[::-1][0].split('&', 1)[0] - or url_p.netloc.split('.', 1)[0]), + return {'name' : (url_p.path.split('/')[-1] or + url_p.query.split('=', 1)[::-1][0].split('&', 1)[0] or + url_p.netloc.split('.', 1)[0]), 'size' : 0, 'status': 3 if url else 8, 'url' : url} @@ -158,6 +130,41 @@ class Hoster(Plugin): pass + def _setup(self): + if self.account: + self.chunk_limit = -1 #: -1 for unlimited + self.resume_download = True + self.premium = self.account.is_premium(self.user) + else: + self.chunk_limit = 1 + self.resume_download = False + self.premium = False + + + def load_account(self): + oldaccount = self.account + + if not self.account: + self.account = self.pyload.accountManager.getAccountPlugin(self.__name__) + + if self.account: + if self.user: + self.account = self.account.is_logged(self.user, relogin=True) + else: + self.user, data = self.account.select() + if not self.user: + self.account = False + + if self.account == oldaccount: + self.req.clearCookies() + + elif self.account: + self.req = self.account.get_request(self.user) #: Browser instance, see `network.Browser` + + else: + self.req = self.pyload.requestFactory.getRequest(self.__name__) + + def preprocessing(self, thread): """ Handles important things to do before starting @@ -166,16 +173,13 @@ class Hoster(Plugin): self.req.renewHTTPRequest() - if self.account: - self.account.is_logged(self.user) - else: - self.req.clearCookies() - + self.load_account() + self._setup() self.setup() self.pyfile.setStatus("starting") - self.log_debug("PROCESS URL " + self.pyfile.url, "PLUGIN VERSION %s" % self.__version__) + return self.process(self.pyfile) @@ -426,7 +430,7 @@ class Hoster(Plugin): return self.last_download - def check_download(self, rules, delete=False, file_size=0, size_tolerance=1024, read_size=100000): + def check_download(self, rules, delete=False, file_size=0, size_tolerance=1024, read_size=1048576): """ Checks the content of the last downloaded file, re match is saved to `lastCheck` diff --git a/module/plugins/internal/MultiHook.py b/module/plugins/internal/MultiHook.py index b64ff08c0..ea515d69a 100644 --- a/module/plugins/internal/MultiHook.py +++ b/module/plugins/internal/MultiHook.py @@ -11,7 +11,7 @@ from module.utils import decode, remove_chars class MultiHook(Hook): __name__ = "MultiHook" __type__ = "hook" - __version__ = "0.52" + __version__ = "0.53" __status__ = "testing" __config__ = [("pluginmode" , "all;listed;unlisted", "Use for plugins" , "all"), @@ -84,8 +84,7 @@ class MultiHook(Hook): def load_account(self): self.account = self.pyload.accountManager.getAccountPlugin(self.pluginname) - if self.account and not self.account.can_use(): - self.account = None + self.load_account() if not self.account and hasattr(self.pluginclass, "LOGIN_ACCOUNT") and self.pluginclass.LOGIN_ACCOUNT: self.log_warning(_("Hook plugin will be deactivated due missing account reference")) diff --git a/module/plugins/internal/SimpleCrypter.py b/module/plugins/internal/SimpleCrypter.py index 33ebd8cac..20a03df9f 100644 --- a/module/plugins/internal/SimpleCrypter.py +++ b/module/plugins/internal/SimpleCrypter.py @@ -10,7 +10,7 @@ from module.utils import fixup, html_unescape class SimpleCrypter(Crypter, SimpleHoster): __name__ = "SimpleCrypter" __type__ = "crypter" - __version__ = "0.57" + __version__ = "0.58" __status__ = "testing" __pattern__ = r'^unmatchable$' @@ -59,14 +59,8 @@ class SimpleCrypter(Crypter, SimpleHoster): #@TODO: Remove in 0.4.10 def init(self): account_name = (self.__name__ + ".py").replace("Folder.py", "").replace(".py", "") - account = self.pyload.accountManager.getAccountPlugin(account_name) - - if account and account.can_use(): - self.user, data = account.select() - self.req = account.get_request(self.user) - self.premium = account.is_premium(self.user) - - self.account = account + self.account = self.pyload.accountManager.getAccountPlugin(account_name) + self.load_account() def handle_direct(self, pyfile): diff --git a/module/plugins/internal/SimpleHoster.py b/module/plugins/internal/SimpleHoster.py index f015ce9ff..a03e301b3 100644 --- a/module/plugins/internal/SimpleHoster.py +++ b/module/plugins/internal/SimpleHoster.py @@ -338,7 +338,7 @@ class SimpleHoster(Hoster): if self.CHECK_FILE: self.log_debug("Using custom check rules...") with open(fs_encode(self.last_download), "rb") as f: - self.html = f.read(50000) #@TODO: Recheck in 0.4.10 + self.html = f.read(1048576) #@TODO: Recheck in 0.4.10 self.check_errors() self.log_info(_("No errors found")) |