Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ COOKIE用来告诉雨课堂你是你。获取方式如下:

![/how-to-get-cookie](img/how-to-get-cookie.jpg)

#### ④ 终端直接输入(新)

运行 `python main.py` 后,程序会在终端提示你输入 `course_url` 和 `cookie`:

- 输入新值后会自动写入 `config.ini`
- 直接回车可保留 `config.ini` 中当前值
- 若 `config.ini` 未正确填写,可直接在终端补全后继续运行

### 四、开始刷课

```python
Expand Down
78 changes: 65 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment on lines +71 to +74
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.
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:// 开头')
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.
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.
continue

if is_updated:
save_config(config, config_path)
print('已将输入内容写入 config.ini')
return final_course_url, final_cookie


# 加载配置
print('='*50)
print('YuketangAutoPlayer - 雨课堂视频自动播放器')
print('='*50)

config = load_config()
config, config_path = load_config()

# 读取配置项
try:
IF_HEADLESS = config.getboolean('Settings', 'headless', fallback=False)
COURSE_URL = config.get('Settings', 'course_url', fallback='')
COOKIE = config.get('Settings', 'cookie', fallback='')
IMPLICITLY_WAIT = config.getint('Settings', 'implicitly_wait', fallback=10)
except Exception as e:
print(f'\n配置文件格式错误: {e}')
input('按回车键退出...')
sys.exit(1)

# 验证配置
if not COURSE_URL or not COOKIE or '在此填写' in COURSE_URL or '在此填写' in COOKIE:
print('\n错误: 检测到配置文件未正确填写!')
print('请编辑 config.ini 文件,填写正确的 course_url 和 cookie')
input('按回车键退出...')
sys.exit(1)
COURSE_URL, COOKIE = prompt_course_url_and_cookie(config, config_path)

print(f'\n配置信息:')
print(f' 无窗口模式: {IF_HEADLESS}')
Expand Down
Loading