new option to load auth info from file

This commit is contained in:
leafee98 2021-10-14 19:47:20 +08:00
parent 6bb5f77141
commit 08680de640

View file

@ -96,11 +96,19 @@ class login_manager:
login_manager.logger.info('logout_state: %s', 'success' if html_title == '信息页' else 'fail') login_manager.logger.info('logout_state: %s', 'success' if html_title == '信息页' else 'fail')
def load_auth_info(path: str):
with open(path, 'r', encoding='utf-8') as file:
username = file.readline().strip()
password = file.readline().strip()
return username, password
def main(): def main():
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('main') logger = logging.getLogger('main')
parser = argparse.ArgumentParser(description='login or logout bistu wifi') parser = argparse.ArgumentParser()
parser.description = 'login or logout bistu wifi'
parser.epilog = 'the config file format(without quote): "{username}\\n{password}". The "\\n" is a newline charactor'
parser.add_argument('username', nargs='?', parser.add_argument('username', nargs='?',
type=str, help='username you use to login bistu wifi, logout if not provided') type=str, help='username you use to login bistu wifi, logout if not provided')
@ -108,6 +116,8 @@ def main():
type=str, help='password you use to login bistu wifi, logout if not provided') type=str, help='password you use to login bistu wifi, logout if not provided')
parser.add_argument('-o', '--logout', nargs='?', type=bool, parser.add_argument('-o', '--logout', nargs='?', type=bool,
help='logout bistu wifi') help='logout bistu wifi')
parser.add_argument('-c', '--config', nargs='?', type=str,
help='load username and password from config file')
args = parser.parse_args() args = parser.parse_args()
@ -115,11 +125,17 @@ def main():
logger.debug('parse argument: password= %s', args.password) logger.debug('parse argument: password= %s', args.password)
logger.debug('parse argument: logout= %s', args.logout) logger.debug('parse argument: logout= %s', args.logout)
if args.username is None or args.password is None or args.logout: if args.config is not None:
login_manager.logout() username, password = load_auth_info(args.config)
else: manager = login_manager(username, password)
manager.login()
elif args.username is not None and args.password is not None:
manager = login_manager(args.username, args.password) manager = login_manager(args.username, args.password)
manager.login() manager.login()
elif args.logout:
login_manager.logout()
else:
parser.print_help()
if __name__ == '__main__': if __name__ == '__main__':