Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
342 lines (294 loc) · 11.5 KB
/
Copy pathmain.py
File metadata and controls
342 lines (294 loc) · 11.5 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""橙啦课程下载器 - 主入口
"""
import os
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed
from config import DOWNLOAD_DIR
from login import create_browser_session, login_interactive, load_cookies
from api import OrangeAPI
from extractor import extract_video_info
from downloader import download_file, download_ev1_to_mp4, download_ppt_images, merge_video_audio
from board_renderer import BoardRenderer
from post_process import organize_directory, generate_dpl
def download_period(page, api, course_id, period, chapter_name, index, dirs):
"""下载单个课时"""
pid = period["guid"]
title = period["coursePeriodTitle"]
teacher = period.get("teacherName", "")
safe = _safe(f"[{index}]--{title}")
ch_dir = _safe(chapter_name)
print(f"\n{'='*50}")
print(f" 课时 {index}: {title} ({teacher})")
print(f"{'='*50}")
# ── 课时级跳过检查:有音频或板书即视为已下载 ──
lesson_dir = os.path.join(dirs["lessons"], ch_dir)
audio_mp3 = os.path.join(lesson_dir, f"{safe}.mp3")
board_merged = os.path.join(lesson_dir, f"{safe}_板书.mp4")
if os.path.exists(audio_mp3) or os.path.exists(board_merged):
print(f" ⏭️ 课时已下载,跳过: {title}")
return []
# 使用预获取的播放信息
info = period.get("_playInfo")
if not info:
try:
info = api.get_review_play_info(course_id, pid)
except Exception as e:
print(f" 跳过: {e}")
return []
if not info:
print(f" 跳过: 无播放信息")
return []
# 提取视频信息
try:
data = extract_video_info(page, api, course_id, pid)
except Exception as e:
print(f" 提取失败: {e}")
return []
vi = data["videoInfo"]
wb = data["whiteboardMessages"]
if not vi.get("videos", {}).get("superHD") and not vi.get("audio"):
print(f" 跳过: 无视频数据")
return []
os.makedirs(lesson_dir, exist_ok=True)
files = []
# ── 并行下载:视频、音频、PPT 同时进行 ──
tasks = {} # future -> (type, args)
with ThreadPoolExecutor(max_workers=3) as pool:
# 教师视频
vids = vi.get("videos", {}).get("superHD", [])
if vids and vids[0].get("url"):
mp4 = os.path.join(lesson_dir, f"{safe}.mp4")
if not os.path.exists(mp4):
print(f"\n 提交下载: 教师视频")
fut = pool.submit(download_ev1_to_mp4, vids[0]["url"], mp4, f"{title} 教师")
tasks[fut] = ("video", mp4)
# 音频
audio_url = vi.get("audio", "")
audio_file = ""
if audio_url:
if audio_url.startswith("//"):
audio_url = "https:" + audio_url
mp3 = os.path.join(lesson_dir, f"{safe}.mp3")
if not os.path.exists(mp3):
print(f" 提交下载: 音频")
fut = pool.submit(download_file, audio_url, mp3, f"{title} 音频")
tasks[fut] = ("audio", mp3)
else:
audio_file = mp3
else:
audio_file = ""
# PPT 图片
pages = vi.get("pages", [])
ppt_dir = ""
ppt_map = {}
if pages and wb:
doc = pages[0].get("doc", {})
prefix = doc.get("urlPrefix", "")
total = doc.get("totalPages", 1)
if prefix:
ppt_dir = os.path.join(lesson_dir, f".ppt_{pid}")
print(f" 提交下载: PPT 图片")
fut = pool.submit(download_ppt_images, prefix, total, ppt_dir)
tasks[fut] = ("ppt", None)
# 等待所有并发任务完成
for fut in as_completed(tasks):
task_type, arg = tasks[fut]
try:
result = fut.result()
if task_type == "video" and result:
files.append({"path": arg, "title": f"{title} 教师"})
elif task_type == "audio" and result:
files.append({"path": arg, "title": f"{title} 音频"})
audio_file = arg
elif task_type == "ppt":
imgs = result or []
ppt_map = {i: p for i, p in enumerate(imgs)}
except Exception as e:
print(f" 下载异常 ({task_type}): {e}")
# ── 串行:渲染板书 + 合并(依赖 PPT 图片和音频) ──
if ppt_map:
print(f"\n 渲染板书视频...")
dur = 0
dur_source = ""
if vids:
dur = vids[0].get("duration", 0)
dur_source = "videoInfo.duration"
if not dur:
dur = info.get("timeLong", 0) or info.get("videoLength", 0)
dur_source = "reviewPlayInfo"
if not dur:
dur = data.get("playbackInfo", {}).get("timeLong", 0)
dur_source = "playbackInfo.timeLong"
if not dur:
if wb:
last_ts = max(m.get("offset_timestamp", 0) for m in wb)
# offset_timestamp 可能是毫秒或秒,>10000 视为毫秒
if last_ts > 10000:
dur = last_ts // 1000 + 10
else:
dur = last_ts + 10
dur_source = "whiteboard_last_ts"
if not dur:
dur = 60
dur_source = "fallback"
# 百家云 duration 可能是毫秒而非秒,>24h 视为毫秒
if dur > 86400:
dur = dur // 1000
print(f" 视频时长: {dur}秒 (来源: {dur_source})")
r = BoardRenderer(ppt_map, wb, fps=3)
raw_board = os.path.join(lesson_dir, f"{safe}_board_tmp.mp4")
if r.render_video(raw_board, dur):
if audio_file:
merged = os.path.join(lesson_dir, f"{safe}_板书.mp4")
print(f"\n 合并板书+音频...")
if merge_video_audio(raw_board, audio_file, merged):
files.append({"path": merged, "title": f"{title} 板书"})
try:
os.remove(raw_board)
except Exception:
pass
else:
files.append({"path": raw_board, "title": f"{title} 板书(无声)"})
else:
final = os.path.join(lesson_dir, f"{safe}_板书.mp4")
os.rename(raw_board, final)
files.append({"path": final, "title": f"{title} 板书(无声)"})
return files
def download_course(page, api, course):
"""下载整个课程"""
cid = course["guid"]
name = course["courseName"]
print(f"\n{'#'*50}")
print(f" 课程: {name}")
print(f"{'#'*50}")
dirs = organize_directory(DOWNLOAD_DIR, name)
detail = api.get_course_detail(cid)
ch_map = {ch["id"]: ch["chapterName"] for ch in detail.get("chapterClass", [])}
periods = api.get_period_list(cid, detail=detail)
for p in periods:
p["_ch"] = p.get("_chapterName") or ch_map.get(p.get("courseChapterId"), "未分类")
print(f" 总课时: {len(periods)}")
# 先用本地数据快速筛选可能有回放的课时
# 排除明显的直播课(未来时间、无 roomId 等)
candidates = []
for p in periods:
# 跳过非视频类型
if p.get("coursePeriodType") not in ("1", None, ""):
continue
# 跳过没有 roomId 的
if not p.get("roomId"):
continue
# 跳过未来的直播(startTime 在未来)
start = p.get("startTime", "")
if start:
try:
st = datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
if st > datetime.now():
continue # 未来的课,跳过
except Exception:
pass
candidates.append(p)
print(f" 候选课时(已结束): {len(candidates)}")
# 只对候选课时调用 reviewPlayInfo API
available = []
for i, p in enumerate(candidates):
try:
info = api.get_review_play_info(cid, p["guid"])
if info and info.get("classInfo", {}).get("token"):
p["_playInfo"] = info
available.append(p)
except Exception:
pass
if (i + 1) % 10 == 0:
print(f" 检查进度: {i+1}/{len(candidates)}, 有回放: {len(available)}")
print(f" 有回放: {len(available)}/{len(periods)}")
if not available:
print(" 没有可下载的课时")
return
all_files = []
for i, period in enumerate(available, 1):
try:
f = download_period(page, api, cid, period, period["_ch"], i, dirs)
all_files.extend(f)
except Exception as e:
print(f" 错误: {e}")
if all_files:
generate_dpl(dirs["lessons"], all_files)
print(f"\n完成: {name}, {len(all_files)} 个文件")
def _safe(name):
for c in '<>:"/\\|?*':
name = name.replace(c, "_")
return name.strip()
def _safe_close(browser, pw):
"""安全关闭浏览器,Ctrl+C 等中断场景也不会报错"""
try:
browser.close()
except BaseException:
pass
try:
pw.stop()
except BaseException:
pass
def main():
print("=" * 50)
print(" 橙啦课程下载器")
print("=" * 50)
# 直接从文件加载 Cookie,不启动浏览器验证
cookies = load_cookies()
if not cookies:
print("未找到 cookies.json,需要登录...")
pw, browser, context, page = create_browser_session(headless=False)
try:
cookies = login_interactive(page, context)
if not cookies:
return
finally:
_safe_close(browser, pw)
# API 客户端(直接用文件 Cookie)
api = OrangeAPI(cookies)
# 验证 Cookie 是否有效
print("验证登录状态...")
try:
courses = api.get_course_list()
print("Cookie 有效!")
except Exception:
print("Cookie 已过期,需要重新登录...")
pw, browser, context, page = create_browser_session(headless=False)
try:
page.goto("https://www.orangevip.com/", wait_until="domcontentloaded", timeout=15000)
page.wait_for_timeout(2000)
cookies = login_interactive(page, context)
if not cookies:
return
api = OrangeAPI(cookies)
courses = api.get_course_list()
finally:
_safe_close(browser, pw)
if not courses:
print("没有课程")
return
# 创建无头浏览器(仅用于 extract_video_info,注入 Cookie)
pw, browser, context, page = create_browser_session(headless=True)
context.add_cookies([
{"name": k, "value": v, "domain": ".orangevip.com", "path": "/"}
for k, v in cookies.items()
])
try:
print(f"\n共 {len(courses)} 个课程:\n")
for i, c in enumerate(courses, 1):
print(f" [{i}] {c['courseName']} "
f"({c.get('learnedCount',0)}/{c.get('totalPeriodCount',0)})")
choice = input("\n输入序号 (all=全部): ").strip()
if choice.lower() == "all":
selected = courses
else:
idxs = [int(x.strip()) - 1 for x in choice.split(",") if x.strip().isdigit()]
selected = [courses[i] for i in idxs if 0 <= i < len(courses)]
for course in selected:
download_course(page, api, course)
print("\n全部完成!")
input("按回车关闭...")
finally:
_safe_close(browser, pw)
if __name__ == "__main__":
main()