Skip to content

feat: support terminal input for course_url and cookie#21

Open
Alice060404 wants to merge 1 commit intoLetMeFly666:masterfrom
Alice060404:feat/terminal-input-config
Open

feat: support terminal input for course_url and cookie#21
Alice060404 wants to merge 1 commit intoLetMeFly666:masterfrom
Alice060404:feat/terminal-input-config

Conversation

@Alice060404
Copy link
Copy Markdown

@Alice060404 Alice060404 commented Apr 12, 2026

改进

  • 支持在程序启动后通过终端直接输入 course_urlcookie
  • 用户输入的新值会自动写回 config.ini
  • 支持“回车保留当前值”,不强制覆盖已有配置
  • config.ini 不存在时,自动创建模板后继续引导输入
  • 增加基础输入校验:
    • course_url 必须以 https:// 开头
    • cookie 不能为空
  • README 增加“终端直接输入(新)”说明

变更说明

1. 配置加载流程调整(main.py

  • load_config() 返回 (config, config_path),后续可直接写回原配置文件
  • config.ini 不存在时,不再提示“编辑后重启并退出”,改为创建模板后继续执行输入流程

2. 新增配置写回与输入函数(main.py

  • 新增 save_config(config, config_path):统一负责写入 config.ini
  • 新增 is_valid_course_url(course_url):校验课程链接
  • 新增 is_valid_cookie(cookie):校验 cookie
  • 新增 prompt_course_url_and_cookie(config, config_path)
    • 启动后在终端提示输入 course_urlcookie
    • 回车可保留当前值
    • 输入合法时写回 config.ini
    • 输入不合法会提示并重新输入

3. 主流程接入(main.py

  • 原先“配置无效直接退出”的逻辑替换为终端交互获取:
    • COURSE_URL, COOKIE = prompt_course_url_and_cookie(config, config_path)
  • 其余刷课逻辑保持不变(登录、找视频、播放检测循环等不受影响)

4. 文档更新(README.md

在“设置刷课信息”章节新增“终端直接输入(新)”:

  • 输入后自动写入 config.ini
  • 回车可保留当前值
  • 配置不完整时可在终端补全后继续运行

兼容性

  • 对已有 config.ini 用户兼容:可直接回车沿用旧值
  • headlessimplicitly_wait 的读取与原行为一致
  • 未改变原有课程播放和完成判定逻辑

使用方式(更新后)

python main.py

程序会在启动后提示输入:

- course_url(例如 https://www.yuketang.cn/...)
- cookie(sessionid)

输入后自动写入 config.ini 并继续执行

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 12, 2026 08:26
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

此 PR 改进了脚本的配置获取流程:在启动后通过终端交互引导用户输入/补全 course_urlcookie,并可将新值写回 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.

Comment thread main.py
final_cookie = config.get('Settings', 'cookie', fallback='').strip()

if not is_valid_course_url(final_course_url):
print('\n错误: course_url 无效,必须以 https:// 开头')
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的错误提示与实际校验条件不一致:is_valid_course_url 除了要求以 https:// 开头外,还会拒绝包含“在此填写”的模板占位符,但提示只说“必须以 https:// 开头”,会让用户困惑(模板值本身也以 https:// 开头)。建议让提示覆盖所有失败原因,或在校验时分别给出更准确的提示。

Suggested change
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 uses AI. Check for mistakes.
Comment thread main.py
print('\n错误: course_url 无效,必须以 https:// 开头')
continue
if not is_valid_cookie(final_cookie):
print('\n错误: cookie 不能为空')
Copy link

Copilot AI Apr 12, 2026

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 不能为空且不能为模板占位符”等更精确的说明,或区分不同失败原因。

Suggested change
print('\n错误: cookie 不能为空')
print('\n错误: cookie 不能为空且不能为模板占位符,请填写真实的 sessionid')

Copilot uses AI. Check for mistakes.
Comment thread main.py
Comment on lines +71 to +74
def save_config(config, config_path):
"""保存配置到config.ini"""
try:
with open(config_path, 'w', encoding='utf-8') as f:
Copy link

Copilot AI Apr 12, 2026

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 生成的说明性注释头(以 ; 开头的配置说明)全部抹掉,降低后续手工编辑配置时的可读性。建议在写回时保留这些注释(例如写回同样的注释头,或只更新需要变更的键值而不是整文件重写)。

Suggested change
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')

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants