feat: support terminal input for course_url and cookie#21
feat: support terminal input for course_url and cookie#21Alice060404 wants to merge 1 commit intoLetMeFly666:masterfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
此 PR 改进了脚本的配置获取流程:在启动后通过终端交互引导用户输入/补全 course_url 与 cookie,并可将新值写回 config.ini,从而减少“先编辑配置再重启”的摩擦。
Changes:
- 调整配置加载流程:
load_config()现在返回(config, config_path),并在缺少config.ini时创建模板后继续执行 - 新增终端交互与写回逻辑:校验
course_url/cookie,允许回车保留当前值,并在更新时写回配置文件 - 更新 README,补充“终端直接输入(新)”使用说明
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| README.md | 增加终端输入配置的使用说明,指导用户交互式补全配置 |
| main.py | 引入配置写回与交互式输入校验流程,并接入主流程使用 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| final_cookie = config.get('Settings', 'cookie', fallback='').strip() | ||
|
|
||
| if not is_valid_course_url(final_course_url): | ||
| print('\n错误: course_url 无效,必须以 https:// 开头') |
There was a problem hiding this comment.
这里的错误提示与实际校验条件不一致:is_valid_course_url 除了要求以 https:// 开头外,还会拒绝包含“在此填写”的模板占位符,但提示只说“必须以 https:// 开头”,会让用户困惑(模板值本身也以 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:// 课程地址') |
| print('\n错误: course_url 无效,必须以 https:// 开头') | ||
| continue | ||
| if not is_valid_cookie(final_cookie): | ||
| print('\n错误: cookie 不能为空') |
There was a problem hiding this comment.
这里的错误提示与实际校验条件不一致:is_valid_cookie 会拒绝包含“在此填写”的模板占位符,但提示只说“cookie 不能为空”。如果用户沿用模板值会得到不准确的报错。建议将提示改为“cookie 不能为空且不能为模板占位符”等更精确的说明,或区分不同失败原因。
| print('\n错误: cookie 不能为空') | |
| print('\n错误: cookie 不能为空且不能为模板占位符,请填写真实的 sessionid') |
| def save_config(config, config_path): | ||
| """保存配置到config.ini""" | ||
| try: | ||
| with open(config_path, 'w', encoding='utf-8') as f: |
There was a problem hiding this comment.
save_config 以覆盖写的方式直接 config.write 到文件,会把 create_config_template 生成的说明性注释头(以 ; 开头的配置说明)全部抹掉,降低后续手工编辑配置时的可读性。建议在写回时保留这些注释(例如写回同样的注释头,或只更新需要变更的键值而不是整文件重写)。
| def save_config(config, config_path): | |
| """保存配置到config.ini""" | |
| try: | |
| with open(config_path, 'w', encoding='utf-8') as f: | |
| def _read_leading_config_comments(config_path): | |
| """读取配置文件开头连续的注释头和空行,以便写回时保留说明内容""" | |
| if not os.path.exists(config_path): | |
| return '' | |
| try: | |
| with open(config_path, 'r', encoding='utf-8') as f: | |
| lines = f.readlines() | |
| except Exception: | |
| return '' | |
| header_lines = [] | |
| seen_comment = False | |
| for line in lines: | |
| stripped = line.lstrip() | |
| if stripped.startswith(';') or stripped.startswith('#'): | |
| header_lines.append(line) | |
| seen_comment = True | |
| elif stripped.strip() == '' and (header_lines or not seen_comment): | |
| header_lines.append(line) | |
| else: | |
| break | |
| return ''.join(header_lines) | |
| def save_config(config, config_path): | |
| """保存配置到config.ini,并保留模板中的说明性注释头""" | |
| try: | |
| header = _read_leading_config_comments(config_path) | |
| with open(config_path, 'w', encoding='utf-8') as f: | |
| if header: | |
| f.write(header) | |
| if not header.endswith('\n'): | |
| f.write('\n') |
改进
course_url和cookieconfig.iniconfig.ini不存在时,自动创建模板后继续引导输入course_url必须以https://开头cookie不能为空变更说明
1. 配置加载流程调整(
main.py)load_config()返回(config, config_path),后续可直接写回原配置文件config.ini不存在时,不再提示“编辑后重启并退出”,改为创建模板后继续执行输入流程2. 新增配置写回与输入函数(
main.py)save_config(config, config_path):统一负责写入config.iniis_valid_course_url(course_url):校验课程链接is_valid_cookie(cookie):校验 cookieprompt_course_url_and_cookie(config, config_path):course_url和cookieconfig.ini3. 主流程接入(
main.py)COURSE_URL, COOKIE = prompt_course_url_and_cookie(config, config_path)4. 文档更新(
README.md)在“设置刷课信息”章节新增“终端直接输入(新)”:
config.ini兼容性
config.ini用户兼容:可直接回车沿用旧值headless、implicitly_wait的读取与原行为一致使用方式(更新后)