-
Notifications
You must be signed in to change notification settings - Fork 17
feat: support terminal input for course_url and cookie #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,44 +56,96 @@ def load_config(): | |||||||||||||||||||
| # 如果config.ini不存在,创建模板 | ||||||||||||||||||||
| print(f'未找到配置文件,正在创建模板: {config_path}') | ||||||||||||||||||||
| create_config_template(config_path) | ||||||||||||||||||||
| print('\n请编辑 config.ini 文件填写你的配置信息后重新运行程序') | ||||||||||||||||||||
| input('按回车键退出...') | ||||||||||||||||||||
| sys.exit(0) | ||||||||||||||||||||
| print('\n已创建配置模板,接下来可直接在终端输入 course_url 和 cookie') | ||||||||||||||||||||
|
|
||||||||||||||||||||
| try: | ||||||||||||||||||||
| config.read(config_path, encoding='utf-8') | ||||||||||||||||||||
| print(f'成功加载配置文件: {config_path}') | ||||||||||||||||||||
| return config | ||||||||||||||||||||
| return config, config_path | ||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||
| print(f'读取配置文件失败: {e}') | ||||||||||||||||||||
| input('按回车键退出...') | ||||||||||||||||||||
| sys.exit(1) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def save_config(config, config_path): | ||||||||||||||||||||
| """保存配置到config.ini""" | ||||||||||||||||||||
| try: | ||||||||||||||||||||
| with open(config_path, 'w', encoding='utf-8') as f: | ||||||||||||||||||||
| config.write(f) | ||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||
| print(f'写入配置文件失败: {e}') | ||||||||||||||||||||
| input('按回车键退出...') | ||||||||||||||||||||
| sys.exit(1) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def is_valid_course_url(course_url): | ||||||||||||||||||||
| return bool(course_url) and '在此填写' not in course_url and course_url.startswith('https://') | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def is_valid_cookie(cookie): | ||||||||||||||||||||
| return bool(cookie) and '在此填写' not in cookie | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| def prompt_course_url_and_cookie(config, config_path): | ||||||||||||||||||||
| """在终端输入course_url和cookie,并写回配置文件""" | ||||||||||||||||||||
| if not config.has_section('Settings'): | ||||||||||||||||||||
| config.add_section('Settings') | ||||||||||||||||||||
|
|
||||||||||||||||||||
| print('\n你可以直接在终端输入课程地址和cookie(回车可保留当前值)') | ||||||||||||||||||||
| while True: | ||||||||||||||||||||
| current_course_url = config.get('Settings', 'course_url', fallback='').strip() | ||||||||||||||||||||
| current_cookie = config.get('Settings', 'cookie', fallback='').strip() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| course_status = current_course_url if is_valid_course_url(current_course_url) else '未配置' | ||||||||||||||||||||
| cookie_status = '已配置' if is_valid_cookie(current_cookie) else '未配置' | ||||||||||||||||||||
| print(f'当前 course_url: {course_status}') | ||||||||||||||||||||
| print(f'当前 cookie: {cookie_status}') | ||||||||||||||||||||
|
|
||||||||||||||||||||
| input_course_url = input('请输入 course_url(https://...): ').strip() | ||||||||||||||||||||
| input_cookie = input('请输入 cookie(sessionid): ').strip() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| is_updated = False | ||||||||||||||||||||
| if input_course_url: | ||||||||||||||||||||
| config.set('Settings', 'course_url', input_course_url) | ||||||||||||||||||||
| is_updated = True | ||||||||||||||||||||
| if input_cookie: | ||||||||||||||||||||
| config.set('Settings', 'cookie', input_cookie) | ||||||||||||||||||||
| is_updated = True | ||||||||||||||||||||
|
|
||||||||||||||||||||
| final_course_url = config.get('Settings', 'course_url', fallback='').strip() | ||||||||||||||||||||
| final_cookie = config.get('Settings', 'cookie', fallback='').strip() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if not is_valid_course_url(final_course_url): | ||||||||||||||||||||
| print('\n错误: course_url 无效,必须以 https:// 开头') | ||||||||||||||||||||
|
||||||||||||||||||||
| print('\n错误: course_url 无效,必须以 https:// 开头') | |
| if not final_course_url: | |
| print('\n错误: course_url 不能为空') | |
| elif '在此填写' in final_course_url: | |
| print('\n错误: course_url 不能使用模板占位符“在此填写”,请填写真实课程地址') | |
| elif not final_course_url.startswith('https://'): | |
| print('\n错误: course_url 无效,必须以 https:// 开头') | |
| else: | |
| print('\n错误: course_url 无效,请填写真实的 https:// 课程地址') |
Copilot
AI
Apr 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的错误提示与实际校验条件不一致:is_valid_cookie 会拒绝包含“在此填写”的模板占位符,但提示只说“cookie 不能为空”。如果用户沿用模板值会得到不准确的报错。建议将提示改为“cookie 不能为空且不能为模板占位符”等更精确的说明,或区分不同失败原因。
| print('\n错误: cookie 不能为空') | |
| print('\n错误: cookie 不能为空且不能为模板占位符,请填写真实的 sessionid') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save_config以覆盖写的方式直接config.write到文件,会把create_config_template生成的说明性注释头(以;开头的配置说明)全部抹掉,降低后续手工编辑配置时的可读性。建议在写回时保留这些注释(例如写回同样的注释头,或只更新需要变更的键值而不是整文件重写)。