def is_usable(self): """ Check several constraints to determine if account should be used. """ if not self.valid or not self.activated: return False # TODO: not in ui currently if "time" in self.options and self.options['time']: time_data = "" try: time_data = self.options['time'] start, end = time_data.split("-") if not time.compare(start.split(":"), end.split(":")): return False except Exception: self.log_warning( _("Your Time {0} has a wrong format, use: 1:22-3:44").format(time_data)) if 0 <= self.validuntil < time.time(): return False if self.trafficleft is 0: #: test explicitly for 0 return False return True
def _login(self, req): # set timestamp for login self.login_ts = time.time() try: try: self.login(req) except TypeError: # TODO: temporary self.log_debug( "Deprecated .login(...) signature omit user, data") self.login(self.loginname, {'password': self.password}, req) self.valid = True except WrongPassword: self.log_warning( _("Could not login with account {0} | {1}").format(self.loginname, _("Wrong Password"))) self.valid = False except Exception as e: self.log_warning( _("Could not login with account {0} | {1}").format(self.loginname, str(e))) self.valid = False # self.pyload.print_exc() return self.valid
def _login(self, req): # set timestamp for login self.login_ts = time.time() try: try: self.login(req) except TypeError: # TODO: temporary self.log_debug( "Deprecated .login(...) signature omit user, data") self.login(self.loginname, {'password': self.password}, req) self.valid = True except WrongPassword: self.log_warning( _("Could not login with account {0} | {1}").format(self.loginname, _("Wrong Password"))) self.valid = False except Exception as e: self.log_warning( _("Could not login with account {0} | {1}").format(self.loginname, e.message)) self.valid = False # self.pyload.print_exc() return self.valid
def expired(self, user=None): if user: self.log_debug("Deprecated argument user for .expired()", user) self.log_warning( _("Account {0} is expired, checking again in 1h").format(user)) self.validuntil = time.time() - 1 self.schedule_refresh(60 * 60)
def get_account_info(self, force=False): """ Retrieve account info's for an user, do **not** overwrite this method! just use it to retrieve info's in hoster plugins. See `load_account_info` :param name: username :param force: reloads cached account information """ if force or self.timestamp + self.info_threshold * 60 < time.time(): # make sure to login with closing(self.get_account_request()) as req: self.check_login(req) self.log_info( _("Get Account Info for {0}").format(self.loginname)) try: try: infos = self.load_account_info(req) except TypeError: # TODO: temporary self.log_debug( "Deprecated .load_account_info(...) signature, omit user argument" ) infos = self.load_account_info(self.loginname, req) except Exception as e: infos = {'error': e.message} self.log_error(_("Error: {0}").format(e.message)) self.restore_defaults() #: reset to initial state if isinstance(infos, dict): #: copy result from dict to class for k, v in infos.items(): if hasattr(self, k): setattr(self, k, v) else: self.log_debug("Unknown attribute {0}={1}".format( k, v)) self.log_debug("Account Info: {0}".format(infos)) self.timestamp = time.time() self.pyload.evm.fire("account:loaded", self.to_info_data())
def get_account_info(self, force=False): """ Retrieve account info's for an user, do **not** overwrite this method! just use it to retrieve info's in hoster plugins. See `load_account_info` :param name: username :param force: reloads cached account information """ if force or self.timestamp + self.info_threshold * 60 < time.time(): # make sure to login with closing(self.get_account_request()) as req: self.check_login(req) self.log_info( _("Get Account Info for {0}").format(self.loginname)) try: try: infos = self.load_account_info(req) except TypeError: # TODO: temporary self.log_debug( "Deprecated .load_account_info(...) signature, omit user argument") infos = self.load_account_info(self.loginname, req) except Exception as e: infos = {'error': e.message} self.log_error(_("Error: {0}").format(e.message)) self.restore_defaults() #: reset to initial state if isinstance(infos, dict): #: copy result from dict to class for k, v in infos.items(): if hasattr(self, k): setattr(self, k, v) else: self.log_debug("Unknown attribute {0}={1}".format(k, v)) self.log_debug("Account Info: {0}".format(infos)) self.timestamp = time.time() self.pyload.evm.fire("account:loaded", self.to_info_data())
def check_login(self, req): """ Checks if the user is still logged in. """ if self.login_ts + self.login_timeout * 60 < time.time(): if self.login_ts: #: separate from fresh login to have better debug logs self.log_debug( "Reached login timeout for {0}".format(self.loginname)) else: self.log_info(_("Login with {0}").format(self.loginname)) self._login(req) return False return True