def __init__(self): self._prompt = '> ' self._disk = Cloud189() self._task_mgr = global_task_mgr self._dir_list = '' self._file_list = FileList() self._path_list = PathList() self._parent_id = -11 self._parent_name = '' self._work_name = '' self._work_id = -11 self._last_work_id = -11 self._reader_mode = False self._reader_mode = config.reader_mode self._default_dir_pwd = '' self._disk.set_captcha_handler(captcha_handler)
class Commander: """网盘命令行""" def __init__(self): self._prompt = '> ' self._disk = Cloud189() self._task_mgr = global_task_mgr self._dir_list = '' self._file_list = FileList() self._path_list = PathList() self._parent_id = -11 self._parent_name = '' self._work_name = '' self._work_id = -11 self._last_work_id = -11 self._reader_mode = False self._reader_mode = config.reader_mode self._default_dir_pwd = '' self._disk.set_captcha_handler(captcha_handler) @staticmethod def clear(): clear_screen() @staticmethod def help(): print_help() @staticmethod def update(): check_update() def bye(self): if self._task_mgr.has_alive_task(): info("有任务在后台运行, 退出请直接关闭窗口") else: config.work_id = self._work_id exit_cmd(0) def rmode(self): """适用于屏幕阅读器用户的显示方式""" # TODO choice = input("以适宜屏幕阅读器的方式显示(y): ") if choice and choice.lower() == 'y': config.reader_mode = True self._reader_mode = True info("已启用 Reader Mode") else: config.reader_mode = False self._reader_mode = False info("已关闭 Reader Mode") def cdrec(self): """进入回收站模式""" rec = Recovery(self._disk) rec.run() self.refresh() def refresh(self, dir_id=None, auto=False): """刷新当前文件夹和路径信息""" dir_id = self._work_id if dir_id is None else dir_id self._file_list, self._path_list = self._disk.get_file_list(dir_id) if not self._file_list and not self._path_list: if auto: error(f"文件夹 id={dir_id} 无效(被删除), 将切换到根目录!") return self.refresh(-11) else: error(f"文件夹 id 无效 {dir_id=}, {self._work_id=}") return None self._prompt = '/'.join(self._path_list.all_name) + ' > ' self._last_work_id = self._work_id self._work_name = self._path_list[-1].name self._work_id = self._path_list[-1].id if dir_id != -11: # 如果存在上级路径 self._parent_name = self._path_list[-2].name self._parent_id = self._path_list[-2].id def login(self, args): """登录网盘""" if args: if '--auto' in args: if config.cookie and self._disk.login_by_cookie(config) == Cloud189.SUCCESS: self.refresh(config.work_id, auto=True) return None username = input('输入用户名:') password = getpass('输入密码:') if not username or not password: error('没有用户名或密码 :(') return None code = self._disk.login(username, password) if code == Cloud189.NETWORK_ERROR: error("登录失败,网络连接异常") return None elif code == Cloud189.FAILED: error('登录失败,用户名或密码错误 :(') return None # 登录成功保存用户 cookie config.username = username config.password = password config.cookie = self._disk.get_cookie() code, token = get_token(username, password) if code == Cloud189.SUCCESS: config.set_token(*token) self._disk.set_session(*token) self._work_id = -11 self.refresh(-11) def clogin(self): """使用 cookie 登录""" if platform() == 'Linux' and not os.environ.get('DISPLAY'): info("请使用浏览器打开: https://cloud.189.cn 获取 cookie") else: open_new_tab('https://cloud.189.cn') info("请设置 Cookie 内容:") c_login_user = input("COOKIE_LOGIN_USER="******"请输入正确的 Cookie 信息") return None cookie = {"COOKIE_LOGIN_USER": str(c_login_user)} if self._disk.login_by_cookie(cookie) == Cloud189.SUCCESS: user = self._disk.get_user_infos() if not user: error("发生未知错误!") return None user_infos = { 'name': user.account.replace('@189.cn', ''), 'pwd': '', 'cookie': cookie, 'key': '', 'secret': '', 'token': '', 'save_path': './downloads', 'work_id': -11 } config.set_infos(user_infos) self._work_id = config.work_id self.refresh() else: error("登录失败, 请检查 Cookie 是否正确") def logout(self, args): """注销/删除用户""" if args: # 删除用户 for name in args: result = config.del_user(name) if result: info(f"成功删除用户 {name}") else: error(f"删除用户 {name} 失败!") return None clear_screen() self._prompt = '> ' # self._disk.logout() # TODO([email protected]): 还没有注销登录的方法 self._file_list.clear() self._path_list = '' self._parent_id = -11 self._work_id = -11 self._last_work_id = -11 self._parent_name = '' self._work_name = '' config.cookie = None def su(self, args): """列出、切换用户""" users = config.get_users_name() def list_user(): for i, user in enumerate(users): user_info = config.get_user_info(user) methord = "用户名+密码 登录" if user_info[2] else "Cookie 登录" print(f"[{i}] 用户名: {user}, {methord}") if args: if args[0] == '-l': list_user() return None elif args[0] in users: select_user = args[0] else: error(f"用户名 {args[0]} 无效") return None else: list_user() select = input("请输入用户序号, [0、1 ... ]: ") if select.isnumeric(): select = int(select) if select > len(users): error(f"序号 {select} 无效!") return None select_user = users[select] else: error(f"序号 {select} 无效!") return None config.work_id = self._work_id # 保存旧的工作目录 result = config.change_user(select_user) if result and self._disk.login_by_cookie(config) == Cloud189.SUCCESS: info(f"成功切换至用户 {config.username}") self.refresh(config.work_id) else: error("切换用户失败!") def ls(self, args): """列出文件(夹)""" fid = old_fid = self._work_id flag_full = False flag_arg_l = False if args: if len(args) >= 2: if args[0] == '-l': flag_full = True fname = args[-1] elif args[-1] == '-l': flag_full = True fname = args[0] else: info("暂不支持查看多个文件!") fname = args[0] else: if args[0] == '-l': flag_full = True flag_arg_l = True else: fname = args[0] if not flag_arg_l: if file := self._file_list.find_by_name(fname): if file.isFolder: fid = file.id else: error(f"{fname} 非文件夹,显示当前目录文件") else: error(f"{fname} 不存在,显示当前目录文件") if fid != old_fid: self._file_list, _ = self._disk.get_file_list(fid) if not flag_full: # 只罗列文件名 for file in self._file_list: if file.isFolder: print(f"\033[1;34m{handle_name(file.name)}\033[0m", end=' ') else: print(f"{handle_name(file.name)}", end=' ') print() else: if self._reader_mode: # 方便屏幕阅读器阅读 for file in self._file_list: print( f"{handle_name(file.name)} 大小:{get_file_size_str(file.size)} 上传时间:{file.ctime} ID:{file.id}") else: # 普通用户显示方式 for file in self._file_list: star = '✦' if file.isStarred else '✧' # 好像 没什么卵用 file_name = f"\033[1;34m{handle_name(file.name)}\033[0m" if file.isFolder else handle_name(file.name) print("# {0:<17}{1:<4}{2:<20}{3:>8} {4}".format( file.id, star, file.ctime, get_file_size_str(file.size), file_name)) if fid != old_fid: self._file_list, _ = self._disk.get_file_list(old_fid)
class Commander: """网盘命令行""" def __init__(self): self._prompt = '> ' self._disk = Cloud189() self._task_mgr = global_task_mgr self._dir_list = '' self._file_list = FileList() self._path_list = PathList() self._parent_id = -11 self._parent_name = '' self._work_name = '' self._work_id = -11 self._last_work_id = -11 self._reader_mode = False self._reader_mode = config.reader_mode self._default_dir_pwd = '' self._disk.set_captcha_handler(captcha_handler) @staticmethod def clear(): clear_screen() @staticmethod def help(): print_help() @staticmethod def update(): check_update() def bye(self): if self._task_mgr.has_alive_task(): info("有任务在后台运行, 退出请直接关闭窗口") else: exit_cmd(0) def exit(self): self.bye() def rmode(self): """适用于屏幕阅读器用户的显示方式""" choice = input("以适宜屏幕阅读器的方式显示(y): ") if choice and choice.lower() == 'y': config.reader_mode = True self._reader_mode = True info("已启用 Reader Mode") else: config.reader_mode = False self._reader_mode = False info("已关闭 Reader Mode") def cdrec(self): """进入回收站模式""" rec = Recovery(self._disk) rec.run() self.refresh() def refresh(self, dir_id=None): """刷新当前文件夹和路径信息""" dir_id = self._work_id if dir_id is None else dir_id self._file_list, self._path_list = self._disk.get_file_list(dir_id) self._prompt = '/'.join(self._path_list.all_name) + ' > ' self._last_work_id = self._work_id self._work_name = self._path_list[-1].name self._work_id = self._path_list[-1].id if dir_id != -11: # 如果存在上级路径 self._parent_name = self._path_list[-2].name self._parent_id = self._path_list[-2].id def login(self): """登录网盘""" if not config.cookie or self._disk.login_by_cookie( config) != Cloud189.SUCCESS: username = input('输入用户名:') password = getpass('输入密码:') code = self._disk.login(username, password) if code == Cloud189.NETWORK_ERROR: error("登录失败,网络连接异常") return None elif code == Cloud189.FAILED: error('登录失败,用户名或密码错误 :(') return None # 登录成功保存用户 cookie config.username = username config.password = password config.cookie = self._disk.get_cookie() code, token = get_token(username, password) if code == Cloud189.SUCCESS: config.set_token(*token) self._disk.set_session(*token) self.refresh() def clogin(self): """使用 cookie 登录""" open_new_tab('https://cloud.189.cn') info("请设置 Cookie 内容:") c_login_user = input("COOKIE_LOGIN_USER="******"请输入正确的 Cookie 信息") return None cookie = {"COOKIE_LOGIN_USER": str(c_login_user)} if self._disk.login_by_cookie(cookie) == Cloud189.SUCCESS: config.cookie = cookie self.refresh() else: error("登录失败,请检查 Cookie 是否正确") def logout(self): """注销""" clear_screen() self._prompt = '> ' # self._disk.logout() # TODO([email protected]): 还没有注销登录的方法 self._file_list.clear() self._path_list = '' self._parent_id = -11 self._work_id = -11 self._last_work_id = -11 self._parent_name = '' self._work_name = '' config.cookie = None def ll(self, args): """列出文件(夹),详细模式""" self.ls(['-l', *args]) self.refresh() if choice([0, 1, 0]) else None # 1/3 概率刷新 def ls(self, args): """列出文件(夹)""" fid = old_fid = self._work_id flag_full = False flag_arg_l = False if args: if len(args) >= 2: if args[0] == '-l': flag_full = True fname = args[-1] elif args[-1] == '-l': flag_full = True fname = args[0] else: info("暂不支持查看多个文件!") fname = args[0] else: if args[0] == '-l': flag_full = True flag_arg_l = True else: fname = args[0] if not flag_arg_l: if file := self._file_list.find_by_name(fname): if file.isFolder: fid = file.id else: error(f"{fname} 非文件夹,显示当前目录文件") else: error(f"{fname} 不存在,显示当前目录文件") if fid != old_fid: self._file_list, _ = self._disk.get_file_list(fid) if not flag_full: # 只罗列文件名 for file in self._file_list: if file.isFolder: print(f"\033[1;34m{handle_name(file.name)}\033[0m", end=' ') else: print(f"{handle_name(file.name)}", end=' ') print() else: if self._reader_mode: # 方便屏幕阅读器阅读 for file in self._file_list: print( f"{handle_name(file.name)} 大小:{get_file_size_str(file.size)} 上传时间:{file.ctime} ID:{file.id}" ) else: # 普通用户显示方式 for file in self._file_list: star = '✦' if file.isStarred else '✧' # 好像 没什么卵用 file_name = f"\033[1;34m{handle_name(file.name)}\033[0m" if file.isFolder else handle_name( file.name) print("# {0:<17}{1:<4}{2:<20}{3:>8} {4}".format( file.id, star, file.ctime, get_file_size_str(file.size), file_name)) if fid != old_fid: self._file_list, _ = self._disk.get_file_list(old_fid)