From 01e76034bcad5e5aab7f6e141782071e5c328649 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sun, 6 Jul 2014 19:08:06 +0200 Subject: Fix account checkLogin + related code cosmetics --- module/plugins/Account.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index e75fc0ecb..f6c59ae25 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -36,16 +36,16 @@ class Account(Base): associated hoster plugin. Plugin should also provide `loadAccountInfo` """ __name__ = "Account" - __version__ = "0.2" + __version__ = "0.3" __type__ = "account" __description__ = """Base account plugin""" __author_name__ = "mkaay" __author_mail__ = "mkaay@mkaay.de" - #: after that time [in minutes] pyload will relogin the account - login_timeout = 600 - #: account data will be reloaded after this time - info_threshold = 600 + #: after that time (in minutes) pyload will relogin the account + login_timeout = 10 * 60 + #: after that time (in minutes) account data will be reloaded + info_threshold = 10 * 60 def __init__(self, manager, accounts): @@ -84,17 +84,20 @@ class Account(Base): self.logWarning( _("Could not login with account %(user)s | %(msg)s") % {"user": user , "msg": _("Wrong Password")}) - data["valid"] = False - + success = data["valid"] = False except Exception, e: self.logWarning( _("Could not login with account %(user)s | %(msg)s") % {"user": user , "msg": e}) - data["valid"] = False + success = data["valid"] = False if self.core.debug: print_exc() + else: + success = True finally: - if req: req.close() + if req: + req.close() + return success def relogin(self, user): req = self.getAccountRequest(user) @@ -104,7 +107,7 @@ class Account(Base): if user in self.infos: del self.infos[user] #delete old information - self._login(user, self.accounts[user]) + return self._login(user, self.accounts[user]) def setAccounts(self, accounts): self.accounts = accounts @@ -284,9 +287,10 @@ class Account(Base): def checkLogin(self, user): """ checks if user is still logged in """ if user in self.timestamps: - if self.timestamps[user] + self.login_timeout * 60 < time(): + if self.login_timeout > 0 and self.timestamps[user] + login_timeout * 60 > time(): self.logDebug("Reached login timeout for %s" % user) - self.relogin(user) - return False - - return True + return self.relogin(user) + else: + return True + else: + return False -- cgit v1.2.3 From c34ce034ceff9cd99e9e49c32128d090888d93cc Mon Sep 17 00:00:00 2001 From: Hoai-Thu Vuong Date: Tue, 8 Jul 2014 12:50:41 +0700 Subject: Update Account.py I running and received error no define of login_timeout, after adding self. it work perfectly --- module/plugins/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index f6c59ae25..847a686ec 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -287,7 +287,7 @@ class Account(Base): def checkLogin(self, user): """ checks if user is still logged in """ if user in self.timestamps: - if self.login_timeout > 0 and self.timestamps[user] + login_timeout * 60 > time(): + if self.login_timeout > 0 and self.timestamps[user] + self.login_timeout * 60 > time(): self.logDebug("Reached login timeout for %s" % user) return self.relogin(user) else: -- cgit v1.2.3 From 01890bf1370b801dd95c97559530cb57321a3559 Mon Sep 17 00:00:00 2001 From: stickell Date: Sun, 13 Jul 2014 20:32:44 +0200 Subject: [Account] fixed bug introduced in 01e76034bcad5e5aab7f6e141782071e5c328649 --- module/plugins/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 847a686ec..abeafad3a 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -287,7 +287,7 @@ class Account(Base): def checkLogin(self, user): """ checks if user is still logged in """ if user in self.timestamps: - if self.login_timeout > 0 and self.timestamps[user] + self.login_timeout * 60 > time(): + if self.login_timeout > 0 and self.timestamps[user] + self.login_timeout * 60 < time(): self.logDebug("Reached login timeout for %s" % user) return self.relogin(user) else: -- cgit v1.2.3 From 5060e4c6374a5116d0d8b02528f910f8c5f8bcf9 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 15 Jul 2014 16:25:41 +0200 Subject: Fix code indentation, some bad whitespaces and missing authors + use 'not' instead 'is None' + replace __pattern__'s r" with r' + other minor cosmetics --- module/plugins/Account.py | 1 + 1 file changed, 1 insertion(+) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index abeafad3a..21f65816a 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -25,6 +25,7 @@ from threading import RLock from Plugin import Base from module.utils import compare_time, parseFileSize, lock + class WrongPassword(Exception): pass -- cgit v1.2.3 From a1495eb2f9502fdf29974458845f928025bedf53 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 26 Jul 2014 02:36:15 +0200 Subject: Prefer single quote for dict key --- module/plugins/Account.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 21f65816a..c4d5c982c 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -85,12 +85,12 @@ class Account(Base): self.logWarning( _("Could not login with account %(user)s | %(msg)s") % {"user": user , "msg": _("Wrong Password")}) - success = data["valid"] = False + success = data['valid'] = False except Exception, e: self.logWarning( _("Could not login with account %(user)s | %(msg)s") % {"user": user , "msg": e}) - success = data["valid"] = False + success = data['valid'] = False if self.core.debug: print_exc() else: @@ -120,15 +120,15 @@ class Account(Base): """ updates account and return true if anything changed """ if user in self.accounts: - self.accounts[user]["valid"] = True #do not remove or accounts will not login + self.accounts[user]['valid'] = True #do not remove or accounts will not login if password: - self.accounts[user]["password"] = password + self.accounts[user]['password'] = password self.relogin(user) return True if options: - before = self.accounts[user]["options"] - self.accounts[user]["options"].update(options) - return self.accounts[user]["options"] != before + before = self.accounts[user]['options'] + self.accounts[user]['options'].update(options) + return self.accounts[user]['options'] != before else: self.accounts[user] = {"password": password, "options": options, "valid": True} self._login(user, self.accounts[user]) @@ -168,7 +168,7 @@ class Account(Base): self.logDebug("Account Info: %s" % str(infos)) - infos["timestamp"] = time() + infos['timestamp'] = time() self.infos[name] = infos elif "timestamp" in self.infos[name] and self.infos[name][ "timestamp"] + self.info_threshold * 60 < time(): @@ -180,7 +180,7 @@ class Account(Base): def isPremium(self, user): info = self.getAccountInfo(user) - return info["premium"] + return info['premium'] def loadAccountInfo(self, name, req=None): """this should be overwritten in account plugin,\ @@ -193,9 +193,9 @@ class Account(Base): return { "validuntil": None, # -1 for unlimited "login": name, - #"password": self.accounts[name]["password"], #@XXX: security - "options": self.accounts[name]["options"], - "valid": self.accounts[name]["valid"], + #"password": self.accounts[name]['password'], #@XXX: security + "options": self.accounts[name]['options'], + "valid": self.accounts[name]['valid'], "trafficleft": None, # in kb, -1 for unlimited "maxtraffic": None, "premium": True, #useful for free accounts @@ -231,12 +231,12 @@ class Account(Base): """ returns an valid account name and data""" usable = [] for user, data in self.accounts.iteritems(): - if not data["valid"]: continue + if not data['valid']: continue - if "time" in data["options"] and data["options"]["time"]: + if "time" in data['options'] and data['options']['time']: time_data = "" try: - time_data = data["options"]["time"][0] + time_data = data['options']['time'][0] start, end = time_data.split("-") if not compare_time(start.split(":"), end.split(":")): continue @@ -245,10 +245,10 @@ class Account(Base): if user in self.infos: if "validuntil" in self.infos[user]: - if self.infos[user]["validuntil"] > 0 and time() > self.infos[user]["validuntil"]: + if self.infos[user]['validuntil'] > 0 and time() > self.infos[user]['validuntil']: continue if "trafficleft" in self.infos[user]: - if self.infos[user]["trafficleft"] == 0: + if self.infos[user]['trafficleft'] == 0: continue usable.append((user, data)) -- cgit v1.2.3 From ba916633f2bedb04c7358000b91aed69f52e8e43 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Fri, 1 Aug 2014 19:35:59 +0200 Subject: Remove trailing whitespaces + remove license headers + import urllib methods directly + sort and fix key attributes + use save_join instead join + sort some import declarations + other minor code cosmetics --- module/plugins/Account.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index c4d5c982c..bd7f97cba 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -1,28 +1,11 @@ # -*- coding: utf-8 -*- -""" - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, - or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see . - - @author: mkaay -""" - from random import choice from time import time from traceback import print_exc from threading import RLock -from Plugin import Base +from module.plugins.Plugin import Base from module.utils import compare_time, parseFileSize, lock @@ -37,8 +20,9 @@ class Account(Base): associated hoster plugin. Plugin should also provide `loadAccountInfo` """ __name__ = "Account" - __version__ = "0.3" __type__ = "account" + __version__ = "0.3" + __description__ = """Base account plugin""" __author_name__ = "mkaay" __author_mail__ = "mkaay@mkaay.de" -- cgit v1.2.3 From 0d220d634e512d89bda540f91c643b361c82ea8a Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 27 Sep 2014 01:38:32 +0200 Subject: Logging string cosmetics --- module/plugins/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index bd7f97cba..15e75b965 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -150,7 +150,7 @@ class Account(Base): if req: req.close() - self.logDebug("Account Info: %s" % str(infos)) + self.logDebug("Account Info: %s" % infos) infos['timestamp'] = time() self.infos[name] = infos -- cgit v1.2.3 From 48cfb042a9b75b7928d39ee2fd1190fd39e0db4b Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 4 Oct 2014 15:56:53 +0200 Subject: [OboomCom] Fix account login --- module/plugins/Account.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 15e75b965..291ebf1f5 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -147,8 +147,10 @@ class Account(Base): raise Exception("Wrong return format") except Exception, e: infos = {"error": str(e)} + print_exc() - if req: req.close() + if req: + req.close() self.logDebug("Account Info: %s" % infos) -- cgit v1.2.3 From b0868ae6446078bacf1635dde5e4ab316b4a94cb Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 7 Oct 2014 18:57:59 +0200 Subject: New __authors__ key replaces __author_name__ and __author_mail__ + Whitespaces and EOF fixup --- module/plugins/Account.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 291ebf1f5..28d9ffd68 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -24,8 +24,8 @@ class Account(Base): __version__ = "0.3" __description__ = """Base account plugin""" - __author_name__ = "mkaay" - __author_mail__ = "mkaay@mkaay.de" + __authors__ = [("mkaay", "mkaay@mkaay.de")] + #: after that time (in minutes) pyload will relogin the account login_timeout = 10 * 60 @@ -67,13 +67,13 @@ class Account(Base): self.login(user, data, req) except WrongPassword: self.logWarning( - _("Could not login with account %(user)s | %(msg)s") % {"user": user - , "msg": _("Wrong Password")}) + _("Could not login with account %(user)s | %(msg)s") % {"user": user, + "msg": _("Wrong Password")}) success = data['valid'] = False except Exception, e: self.logWarning( - _("Could not login with account %(user)s | %(msg)s") % {"user": user - , "msg": e}) + _("Could not login with account %(user)s | %(msg)s") % {"user": user, + "msg": e}) success = data['valid'] = False if self.core.debug: print_exc() -- cgit v1.2.3 From ae7a7e66981456e5bbe2b54006d79b6f907be7a4 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Wed, 8 Oct 2014 20:18:13 +0200 Subject: Add __license__ key attribute to plugins --- module/plugins/Account.py | 1 + 1 file changed, 1 insertion(+) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 28d9ffd68..1adba6af4 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -24,6 +24,7 @@ class Account(Base): __version__ = "0.3" __description__ = """Base account plugin""" + __license__ = "GPLv3" __authors__ = [("mkaay", "mkaay@mkaay.de")] -- cgit v1.2.3 From f70cd18b0c7cb303597a06becc791035f7fd7bd6 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 20 Oct 2014 02:20:56 +0200 Subject: [XFSPAccount] Fixed broken login due missing HOSTER_URL --- module/plugins/Account.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 1adba6af4..9c97d2cf9 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -39,16 +39,19 @@ class Account(Base): self.manager = manager self.accounts = {} - self.infos = {} # cache for account information + self.infos = {} #: cache for account information self.lock = RLock() - self.timestamps = {} - self.setAccounts(accounts) + self.init() + self.setAccounts(accounts) + + def init(self): pass + def login(self, user, data, req): """login into account, the cookies will be saved so user can be recognized @@ -58,6 +61,7 @@ class Account(Base): """ pass + @lock def _login(self, user, data): # set timestamp for login @@ -85,6 +89,7 @@ class Account(Base): req.close() return success + def relogin(self, user): req = self.getAccountRequest(user) if req: @@ -95,12 +100,14 @@ class Account(Base): return self._login(user, self.accounts[user]) + def setAccounts(self, accounts): self.accounts = accounts for user, data in self.accounts.iteritems(): self._login(user, data) self.infos[user] = {} + def updateAccounts(self, user, password=None, options={}): """ updates account and return true if anything changed """ @@ -119,6 +126,7 @@ class Account(Base): self._login(user, self.accounts[user]) return True + def removeAccount(self, user): if user in self.accounts: del self.accounts[user] @@ -127,6 +135,7 @@ class Account(Base): if user in self.timestamps: del self.timestamps[user] + @lock def getAccountInfo(self, name, force=False): """retrieve account infos for an user, do **not** overwrite this method!\\ @@ -165,10 +174,12 @@ class Account(Base): data.update(self.infos[name]) return data + def isPremium(self, user): info = self.getAccountInfo(user) return info['premium'] + def loadAccountInfo(self, name, req=None): """this should be overwritten in account plugin,\ and retrieving account information for user @@ -190,9 +201,11 @@ class Account(Base): "type": self.__name__, } + def getAllAccounts(self, force=False): return [self.getAccountInfo(user, force) for user, data in self.accounts.iteritems()] + def getAccountRequest(self, user=None): if not user: user, data = self.selectAccount() @@ -202,6 +215,7 @@ class Account(Base): req = self.core.requestFactory.getRequest(self.__name__, user) return req + def getAccountCookies(self, user=None): if not user: user, data = self.selectAccount() @@ -211,9 +225,11 @@ class Account(Base): cj = self.core.requestFactory.getCookieJar(self.__name__, user) return cj + def getAccountData(self, user): return self.accounts[user] + def selectAccount(self): """ returns an valid account name and data""" usable = [] @@ -243,15 +259,19 @@ class Account(Base): if not usable: return None, None return choice(usable) + def canUse(self): return False if self.selectAccount() == (None, None) else True + def parseTraffic(self, string): #returns kbyte return parseFileSize(string) / 1024 + def wrongPassword(self): raise WrongPassword + def empty(self, user): if user in self.infos: self.logWarning(_("Account %s has not enough traffic, checking again in 30min") % user) @@ -259,6 +279,7 @@ class Account(Base): self.infos[user].update({"trafficleft": 0}) self.scheduleRefresh(user, 30 * 60) + def expired(self, user): if user in self.infos: self.logWarning(_("Account %s is expired, checking again in 1h") % user) @@ -266,11 +287,13 @@ class Account(Base): self.infos[user].update({"validuntil": time() - 1}) self.scheduleRefresh(user, 60 * 60) + def scheduleRefresh(self, user, time=0, force=True): """ add task to refresh account info to sheduler """ self.logDebug("Scheduled Account refresh for %s in %s seconds." % (user, time)) self.core.scheduler.addJob(time, self.getAccountInfo, [user, force]) + @lock def checkLogin(self, user): """ checks if user is still logged in """ -- cgit v1.2.3 From 29de13dc5098cc9214f3f8f111da140a22e3e2ff Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 25 Oct 2014 01:15:58 +0200 Subject: Use parseTraffic instead parseFileSize in accounts --- module/plugins/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 9c97d2cf9..72b10b45d 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -265,7 +265,7 @@ class Account(Base): def parseTraffic(self, string): #returns kbyte - return parseFileSize(string) / 1024 + return parseFileSize(string, unit="kb") def wrongPassword(self): -- cgit v1.2.3 From 34984dae733c3f3d47b41a0acfba3724d53c65a1 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 28 Oct 2014 16:52:10 +0100 Subject: Code cosmetics: plugin class attributes --- module/plugins/Account.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 72b10b45d..7bdca729c 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -19,13 +19,13 @@ class Account(Base): Just overwrite `login` and cookies will be stored and account becomes accessible in\ associated hoster plugin. Plugin should also provide `loadAccountInfo` """ - __name__ = "Account" - __type__ = "account" + __name__ = "Account" + __type__ = "account" __version__ = "0.3" __description__ = """Base account plugin""" - __license__ = "GPLv3" - __authors__ = [("mkaay", "mkaay@mkaay.de")] + __license__ = "GPLv3" + __authors__ = [("mkaay", "mkaay@mkaay.de")] #: after that time (in minutes) pyload will relogin the account -- cgit v1.2.3 From 227a0eb5157875e75a385118ec3485b044ccdb8a Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Tue, 28 Oct 2014 21:55:21 +0100 Subject: Improve loadAccountInfo stuff --- module/plugins/Account.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 7bdca729c..4fac3525f 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -188,18 +188,16 @@ class Account(Base): :param req: `Request` instance :return: """ - return { - "validuntil": None, # -1 for unlimited - "login": name, - #"password": self.accounts[name]['password'], #@XXX: security - "options": self.accounts[name]['options'], - "valid": self.accounts[name]['valid'], - "trafficleft": None, # in kb, -1 for unlimited - "maxtraffic": None, - "premium": True, #useful for free accounts - "timestamp": 0, #time this info was retrieved - "type": self.__name__, - } + return {"validuntil": None, #: -1 for unlimited + "login": name, + # "password": self.accounts[name]['password'], #: commented due security reason + "options": self.accounts[name]['options'], + "valid": self.accounts[name]['valid'], + "trafficleft": None, #: in kb, -1 for unlimited + "maxtraffic": None, + "premium": False, + "timestamp": 0, #: time this info was retrieved + "type": self.__name__} def getAllAccounts(self, force=False): -- cgit v1.2.3 From fc8a6509755418a40e67220d378ed90b4e335390 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Thu, 30 Oct 2014 17:11:58 +0100 Subject: [Account] Revert parseFileSize --- module/plugins/Account.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 4fac3525f..d0736476d 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -195,7 +195,7 @@ class Account(Base): "valid": self.accounts[name]['valid'], "trafficleft": None, #: in kb, -1 for unlimited "maxtraffic": None, - "premium": False, + "premium": None, "timestamp": 0, #: time this info was retrieved "type": self.__name__} @@ -263,7 +263,7 @@ class Account(Base): def parseTraffic(self, string): #returns kbyte - return parseFileSize(string, unit="kb") + return parseFileSize(string) def wrongPassword(self): -- cgit v1.2.3 From 8e921a978e79d93da3036ce3bdc1fb176c74fabb Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Mon, 3 Nov 2014 12:09:04 +0100 Subject: Spare code cosmetics --- module/plugins/Account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index d0736476d..2f7c5d30d 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -21,7 +21,7 @@ class Account(Base): """ __name__ = "Account" __type__ = "account" - __version__ = "0.3" + __version__ = "0.03" __description__ = """Base account plugin""" __license__ = "GPLv3" -- cgit v1.2.3 From ef4bc4b73756565e40c7453f6b71bc1021735033 Mon Sep 17 00:00:00 2001 From: Walter Purcaro Date: Sat, 22 Nov 2014 19:38:25 +0100 Subject: Revert plugins to stable --- module/plugins/Account.py | 159 +++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 86 deletions(-) (limited to 'module/plugins/Account.py') diff --git a/module/plugins/Account.py b/module/plugins/Account.py index 2f7c5d30d..c147404e0 100644 --- a/module/plugins/Account.py +++ b/module/plugins/Account.py @@ -1,14 +1,30 @@ # -*- coding: utf-8 -*- +""" + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . + + @author: mkaay +""" + from random import choice from time import time from traceback import print_exc from threading import RLock -from module.plugins.Plugin import Base +from Plugin import Base from module.utils import compare_time, parseFileSize, lock - class WrongPassword(Exception): pass @@ -19,19 +35,17 @@ class Account(Base): Just overwrite `login` and cookies will be stored and account becomes accessible in\ associated hoster plugin. Plugin should also provide `loadAccountInfo` """ - __name__ = "Account" - __type__ = "account" - __version__ = "0.03" - - __description__ = """Base account plugin""" - __license__ = "GPLv3" - __authors__ = [("mkaay", "mkaay@mkaay.de")] + __name__ = "Account" + __version__ = "0.2" + __type__ = "account" + __description__ = """Account Plugin""" + __author_name__ = ("mkaay") + __author_mail__ = ("mkaay@mkaay.de") - - #: after that time (in minutes) pyload will relogin the account - login_timeout = 10 * 60 - #: after that time (in minutes) account data will be reloaded - info_threshold = 10 * 60 + #: after that time [in minutes] pyload will relogin the account + login_timeout = 600 + #: account data will be reloaded after this time + info_threshold = 600 def __init__(self, manager, accounts): @@ -39,19 +53,16 @@ class Account(Base): self.manager = manager self.accounts = {} - self.infos = {} #: cache for account information + self.infos = {} # cache for account information self.lock = RLock() - self.timestamps = {} - - self.init() + self.timestamps = {} self.setAccounts(accounts) - + self.init() def init(self): pass - def login(self, user, data, req): """login into account, the cookies will be saved so user can be recognized @@ -61,34 +72,29 @@ class Account(Base): """ pass - @lock def _login(self, user, data): # set timestamp for login self.timestamps[user] = time() - + req = self.getAccountRequest(user) try: self.login(user, data, req) except WrongPassword: self.logWarning( - _("Could not login with account %(user)s | %(msg)s") % {"user": user, - "msg": _("Wrong Password")}) - success = data['valid'] = False + _("Could not login with account %(user)s | %(msg)s") % {"user": user + , "msg": _("Wrong Password")}) + data["valid"] = False + except Exception, e: self.logWarning( - _("Could not login with account %(user)s | %(msg)s") % {"user": user, - "msg": e}) - success = data['valid'] = False + _("Could not login with account %(user)s | %(msg)s") % {"user": user + , "msg": e}) + data["valid"] = False if self.core.debug: print_exc() - else: - success = True finally: - if req: - req.close() - return success - + if req: req.close() def relogin(self, user): req = self.getAccountRequest(user) @@ -98,8 +104,7 @@ class Account(Base): if user in self.infos: del self.infos[user] #delete old information - return self._login(user, self.accounts[user]) - + self._login(user, self.accounts[user]) def setAccounts(self, accounts): self.accounts = accounts @@ -107,26 +112,24 @@ class Account(Base): self._login(user, data) self.infos[user] = {} - def updateAccounts(self, user, password=None, options={}): """ updates account and return true if anything changed """ if user in self.accounts: - self.accounts[user]['valid'] = True #do not remove or accounts will not login + self.accounts[user]["valid"] = True #do not remove or accounts will not login if password: - self.accounts[user]['password'] = password + self.accounts[user]["password"] = password self.relogin(user) return True if options: - before = self.accounts[user]['options'] - self.accounts[user]['options'].update(options) - return self.accounts[user]['options'] != before + before = self.accounts[user]["options"] + self.accounts[user]["options"].update(options) + return self.accounts[user]["options"] != before else: self.accounts[user] = {"password": password, "options": options, "valid": True} self._login(user, self.accounts[user]) return True - def removeAccount(self, user): if user in self.accounts: del self.accounts[user] @@ -135,7 +138,6 @@ class Account(Base): if user in self.timestamps: del self.timestamps[user] - @lock def getAccountInfo(self, name, force=False): """retrieve account infos for an user, do **not** overwrite this method!\\ @@ -157,14 +159,12 @@ class Account(Base): raise Exception("Wrong return format") except Exception, e: infos = {"error": str(e)} - print_exc() - if req: - req.close() + if req: req.close() - self.logDebug("Account Info: %s" % infos) + self.logDebug("Account Info: %s" % str(infos)) - infos['timestamp'] = time() + infos["timestamp"] = time() self.infos[name] = infos elif "timestamp" in self.infos[name] and self.infos[name][ "timestamp"] + self.info_threshold * 60 < time(): @@ -174,11 +174,9 @@ class Account(Base): data.update(self.infos[name]) return data - def isPremium(self, user): info = self.getAccountInfo(user) - return info['premium'] - + return info["premium"] def loadAccountInfo(self, name, req=None): """this should be overwritten in account plugin,\ @@ -188,22 +186,22 @@ class Account(Base): :param req: `Request` instance :return: """ - return {"validuntil": None, #: -1 for unlimited - "login": name, - # "password": self.accounts[name]['password'], #: commented due security reason - "options": self.accounts[name]['options'], - "valid": self.accounts[name]['valid'], - "trafficleft": None, #: in kb, -1 for unlimited - "maxtraffic": None, - "premium": None, - "timestamp": 0, #: time this info was retrieved - "type": self.__name__} - + return { + "validuntil": None, # -1 for unlimited + "login": name, + #"password": self.accounts[name]["password"], #@XXX: security + "options": self.accounts[name]["options"], + "valid": self.accounts[name]["valid"], + "trafficleft": None, # in kb, -1 for unlimited + "maxtraffic": None, + "premium": True, #useful for free accounts + "timestamp": 0, #time this info was retrieved + "type": self.__name__, + } def getAllAccounts(self, force=False): return [self.getAccountInfo(user, force) for user, data in self.accounts.iteritems()] - def getAccountRequest(self, user=None): if not user: user, data = self.selectAccount() @@ -213,7 +211,6 @@ class Account(Base): req = self.core.requestFactory.getRequest(self.__name__, user) return req - def getAccountCookies(self, user=None): if not user: user, data = self.selectAccount() @@ -223,21 +220,19 @@ class Account(Base): cj = self.core.requestFactory.getCookieJar(self.__name__, user) return cj - def getAccountData(self, user): return self.accounts[user] - def selectAccount(self): """ returns an valid account name and data""" usable = [] for user, data in self.accounts.iteritems(): - if not data['valid']: continue + if not data["valid"]: continue - if "time" in data['options'] and data['options']['time']: + if "time" in data["options"] and data["options"]["time"]: time_data = "" try: - time_data = data['options']['time'][0] + time_data = data["options"]["time"][0] start, end = time_data.split("-") if not compare_time(start.split(":"), end.split(":")): continue @@ -246,10 +241,10 @@ class Account(Base): if user in self.infos: if "validuntil" in self.infos[user]: - if self.infos[user]['validuntil'] > 0 and time() > self.infos[user]['validuntil']: + if self.infos[user]["validuntil"] > 0 and time() > self.infos[user]["validuntil"]: continue if "trafficleft" in self.infos[user]: - if self.infos[user]['trafficleft'] == 0: + if self.infos[user]["trafficleft"] == 0: continue usable.append((user, data)) @@ -257,19 +252,15 @@ class Account(Base): if not usable: return None, None return choice(usable) - def canUse(self): return False if self.selectAccount() == (None, None) else True - def parseTraffic(self, string): #returns kbyte - return parseFileSize(string) - + return parseFileSize(string) / 1024 def wrongPassword(self): raise WrongPassword - def empty(self, user): if user in self.infos: self.logWarning(_("Account %s has not enough traffic, checking again in 30min") % user) @@ -277,7 +268,6 @@ class Account(Base): self.infos[user].update({"trafficleft": 0}) self.scheduleRefresh(user, 30 * 60) - def expired(self, user): if user in self.infos: self.logWarning(_("Account %s is expired, checking again in 1h") % user) @@ -285,21 +275,18 @@ class Account(Base): self.infos[user].update({"validuntil": time() - 1}) self.scheduleRefresh(user, 60 * 60) - def scheduleRefresh(self, user, time=0, force=True): """ add task to refresh account info to sheduler """ self.logDebug("Scheduled Account refresh for %s in %s seconds." % (user, time)) self.core.scheduler.addJob(time, self.getAccountInfo, [user, force]) - @lock def checkLogin(self, user): """ checks if user is still logged in """ if user in self.timestamps: - if self.login_timeout > 0 and self.timestamps[user] + self.login_timeout * 60 < time(): + if self.timestamps[user] + self.login_timeout * 60 < time(): self.logDebug("Reached login timeout for %s" % user) - return self.relogin(user) - else: - return True - else: - return False + self.relogin(user) + return False + + return True -- cgit v1.2.3