Repository navigation
Expand file tree
/
Copy pathfirstrun.py
More file actions
313 lines (270 loc) · 11.1 KB
/
Copy pathfirstrun.py
File metadata and controls
313 lines (270 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
'''
首次配置向导(多站点版)。
流程:
第 0 步 选站点(yuketang / chaoxing / zhihuishu)
第 1 步 拿登录凭据(各站点不同;雨课堂优先读 Firefox cookie)
第 2 步 调该站点接口列课程,挑一门写进 config.ini
列课程走纯 HTTP(零点几秒),不值得为它开浏览器。
"凭据还有没有效"也用它判定:列得出来就是有效的。
底下三个 fetch_courses_xxx 是各站点的课程列表实现,被 sites/ 适配器调用。
这些接口来自各站点公开的前端 API / 开源项目(ocsjs 等),可能随站点改版失效。
'''
import json
import urllib.parse
import urllib.request
import refresh_cookie
HTTP_TIMEOUT = 12
UA = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36')
def host_of(url: str) -> str:
if not url.startswith('http'):
return url
return 'https://' + url.split('//', 1)[-1].split('/')[0] + '/'
def _open(url, headers, timeout=HTTP_TIMEOUT):
"""先按系统/环境代理请求,失败再直连试一次。"""
last = None
for opener in (urllib.request.build_opener(),
urllib.request.build_opener(urllib.request.ProxyHandler({}))):
try:
return opener.open(urllib.request.Request(url, headers=headers), timeout=timeout)
except Exception as e:
last = e
raise last
def _get_json(url, headers):
with _open(url, headers) as resp:
return json.loads(resp.read().decode('utf-8', 'replace'))
# ---------------- 各站点课程列表 ----------------
def fetch_courses_yuketang(sessionid: str, host: str):
"""长江雨课堂。接口 /v2/api/web/courses/list?identity=2。"""
host = host.rstrip('/') + '/'
headers = {
'Cookie': f'sessionid={sessionid}',
'Accept': 'application/json, text/plain, */*',
'User-Agent': UA,
'Referer': host + 'v2/web/index',
}
data = _get_json(host + 'v2/api/web/courses/list?identity=2', headers)
if data.get('errcode'):
raise RuntimeError(f"接口返回 errcode={data.get('errcode')}: {data.get('errmsg')}")
courses = []
for c in (data.get('data') or {}).get('list') or []:
course = c.get('course') or {}
if not c.get('classroom_id'):
continue
courses.append({
'id': c['classroom_id'],
'title': course.get('name') or c.get('name') or '(无标题课程)',
'class_name': c.get('name') or '',
'teacher': (c.get('teacher') or {}).get('name') or '',
'school': course.get('university_name') or '',
})
return courses
def fetch_courses_zhihuishu(cookie_value: str, host: str):
"""智慧树。尝试从公开接口列课;失败时返回空让向导走手动输入。
智慧树的课程列表接口需登录态且形态多样(共享课/新形态/智慧课程各不同),
裸 arn 未必够。这里尽力尝试,实在不行不阻塞用户手动粘课程地址。
"""
host = host.rstrip('/') + '/'
arn = (cookie_value or '').strip()
if '=' in arn:
arn = arn.split('=', 1)[1].strip()
headers = {
'Cookie': f'arn={arn}',
'Accept': 'application/json, text/plain, */*',
'User-Agent': UA,
'Referer': host,
}
# 智慧树常见的课程列表接口(不同学习页可能不同,逐个试)
api_candidates = [
'https://www.zhihuishu.com/portal/al-api/share/getMyCourse',
'https://www.zhihuishu.com/gateway/portal/course/stu/courseList',
]
last = None
for api in api_candidates:
try:
data = _get_json(api, headers)
rows = None
for key in ('result', 'data', 'list'):
v = data
for k in key.split('.'):
v = (v or {}).get(k) if isinstance(v, dict) else None
if isinstance(v, list):
rows = v
break
if not rows:
continue
courses = []
for c in rows:
cid = c.get('courseId') or c.get('id') or c.get('course_id')
if not cid:
continue
courses.append({
'id': str(cid),
'title': c.get('name') or c.get('courseName') or '(无标题课程)',
'class_name': c.get('className') or '',
'teacher': c.get('teacherName') or '',
'school': c.get('universityName') or '',
'url': c.get('url') or '',
})
if courses:
return courses
except Exception as e:
last = e
# 列不出来不算致命 —— 让向导走"手动粘地址"
return []
def fetch_courses_chaoxing(cookie_value: str, host: str):
"""超星学习通。课程列表接口需要 _d + UID,形态复杂。
同样尽力尝试,失败返回空走手动输入。超星用户一般直接从浏览器粘课程地址。
"""
host = host.rstrip('/') + '/'
headers = {
'Cookie': cookie_value if ('=' in (cookie_value or '')) else f'_d={cookie_value}',
'Accept': 'application/json, text/plain, */*',
'User-Agent': UA,
'Referer': host,
}
api_candidates = [
'https://mooc1.chaoxing.com/mycourse/studentcourse?ajax=1',
'https://mooc1.chaoxing.com/mycourse/studentstudy?ajax=1',
]
for api in api_candidates:
try:
data = _get_json(api, headers)
rows = data if isinstance(data, list) else (data.get('data') or [])
courses = []
for c in rows if isinstance(rows, list) else []:
if not isinstance(c, dict):
continue
cid = c.get('courseId') or c.get('id')
if not cid:
continue
courses.append({
'id': str(cid),
'title': c.get('name') or c.get('courseName') or '(无标题课程)',
'class_name': c.get('className') or '',
'teacher': c.get('teacherName') or '',
'school': '',
'url': c.get('url') or '',
})
if courses:
return courses
except Exception:
continue
return []
# ---------------- 向导交互 ----------------
def _ask(prompt: str, default: str = '') -> str:
try:
return input(prompt).strip() or default
except EOFError:
return default
def choose_site():
import sites
pairs = sites.list_adapters()
print('\n请选择要刷课的站点:')
for i, (key, name) in enumerate(pairs, 1):
print(f' [{i}] {name} ({key})')
while True:
ans = _ask('\n请输入序号(直接回车 = 1): ', '1')
if ans.isdigit() and 1 <= int(ans) <= len(pairs):
return pairs[int(ans) - 1][0]
print(' 输入无效,请重新输入')
def choose_course(courses, adapter):
print('\n可选的课程:')
for i, c in enumerate(courses, 1):
print(f' [{i}] {c["title"]}')
detail = ' · '.join(x for x in (
f'教学班 {c["class_name"]}' if c['class_name'] else '',
f'教师 {c["teacher"]}' if c['teacher'] else '',
c['school'],
) if x)
if detail:
print(f' {detail}')
print(' [0] 手动输入课程地址')
while True:
ans = _ask('\n请输入序号(直接回车 = 1): ', '1')
if ans.isdigit() and int(ans) <= len(courses):
break
print(' 输入无效,请重新输入')
index = int(ans)
if index == 0:
manual = _ask('请粘贴课程地址(https:// 开头): ')
return manual if manual.startswith('http') else None
c = courses[index - 1]
if c.get('url', '').startswith('http'):
url = c['url']
else:
# host 由适配器在 ensure_ready 里传入;这里用占位的默认 host,稍后替换
url = adapter.course_url_of(_CURRENT_HOST[0], c['id'])
print(f' 已选择: {c["title"]} -> {url}')
return url
_CURRENT_HOST = ['']
def ensure_ready(path: str):
"""把 config.ini 补到能跑:site + course_url + cookie。缺哪步补哪步。"""
import sites
cfg = refresh_cookie.read_config(path)
def get(key):
try:
return (cfg.get('Settings', key) or '').strip()
except Exception:
return ''
print('=' * 56)
print('网课自动播放器 - 首次配置')
print('=' * 56)
# ---- 第 0 步:站点 ----
site_key = get('site')
try:
adapter = sites.get_adapter(site_key)
print(f'\n当前站点: {adapter.name}(配置里 site = {site_key})')
change = _ask('换站点?输入 y 重选,直接回车沿用: ', '').lower()
if change == 'y':
site_key = choose_site()
adapter = sites.get_adapter(site_key)
except Exception:
site_key = choose_site()
adapter = sites.get_adapter(site_key)
refresh_cookie.set_config_value(path, 'site', site_key)
from sites import get_adapter
adapter = get_adapter(site_key)
url = get('course_url')
cookie = get('cookie')
host = host_of(url) if url.startswith('http') else adapter.home_url('https://' + adapter.hosts[0] + '/')
_CURRENT_HOST[0] = host
# ---- 第 1 步:凭据 ----
courses = None
if cookie:
try:
courses = adapter.fetch_courses(cookie, host)
print(f' 已保存的凭据可用(读到 {len(courses)} 门课程)')
except Exception as e:
print(f' 已保存的凭据不可用({type(e).__name__}: {e}),需要重新获取')
if not cookie or courses is None:
print('\n第 1 步 / 2:获取登录凭据')
cookie = adapter.get_credential()
if not cookie:
print('\n没有拿到登录凭据,配置未完成。')
return False
refresh_cookie.set_config_value(path, 'cookie', cookie)
print(f' 已写入 config.ini(长度 {len(cookie)},内容不回显)')
try:
courses = adapter.fetch_courses(cookie, host)
except Exception as e:
print(f' 读取课程列表失败({type(e).__name__}: {e}),下面手动填地址')
courses = None
# ---- 第 2 步:课程 ----
if url.startswith('http'):
print(f'\n第 2 步 / 2:课程地址已配置 —— {url}')
print(' 想换课程:把 config.ini 里的 course_url 清空,再跑一次即可')
return True
if courses:
chosen = choose_course(courses, adapter)
else:
print('\n第 2 步 / 2:手动输入课程地址')
print(f' 拿法:{adapter.course_url_hint}')
chosen = _ask('课程地址(https:// 开头): ')
if not chosen or not chosen.startswith('http'):
print('\n课程地址无效,配置未完成。')
return False
refresh_cookie.set_config_value(path, 'course_url', chosen)
print(f' 已写入 config.ini: {chosen}')
print('\n配置完成。')
return True