This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
mooc-dl.py
378 lines (326 loc) · 13.8 KB
/
mooc-dl.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import argparse
import hashlib
import os
import re
import sys
import time
from urllib.parse import urlencode
from utils.common import repair_filename, touch_dir
from utils.config import Config
from utils.crawler import Crawler
from utils.downloader import FileManager
from utils.ffmpeg import FFmpeg
from utils.playlist import Dpl
spider = Crawler()
spider.trust_env = False
VIDEO, PDF, RICH_TEXT = 1, 3, 4
COURSEWARE = {VIDEO: "Video", PDF: "PDF", RICH_TEXT: "Rich_text"}
headers = {
"User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10; PCT-AL10 Build/HUAWEIPCT-AL10)",
"edu-app-channel": "ucmooc_offical",
"edu-app-type": "android",
"edu-app-version": "4.19.0",
}
spider.headers.update(headers)
CONFIG = Config()
def handle_args(config):
parser = argparse.ArgumentParser(description="中国大学 MOOC 下载器")
parser.add_argument("-u", "--username", type=str, default=None)
parser.add_argument("-p", "--password", type=str, default=None)
parser.add_argument("-q", "--resolution", type=int, choices=[0, 1, 2], default=None)
parser.add_argument("-d", "--root", type=str, default=None)
parser.add_argument("-n", "--num-thread", type=int, default=None)
parser.add_argument("-w", "--overwrite", action="store_true")
parser.add_argument("-tp", "--file-path-template", type=str, default=None)
parser.add_argument("--range", type=str, default=None)
parser.add_argument("--file-types", type=str, default=None)
parser.add_argument("--use-ffmpeg", action="store_true")
parser.add_argument("url", type=str)
def _parse_range(range_str):
INF = 999
if "~" not in range_str:
range_str = f"{range_str}~{range_str}"
start, end = range_str.split("~")
# 允许 a.b.c~ 或者 ~a.b.c 甚至 ~
start = start if start else "0.0.0"
end = end if end else f"{INF}.{INF}.{INF}"
# 允许 a.b~c,自动补零为 a.b.0~c.INF.INF
start = start.split(".")
start = list(map(lambda x: int(x), start))
start = start + (3 - len(start)) * [0]
end = end.split(".")
end = list(map(lambda x: int(x), end))
end = end + (3 - len(end)) * [INF]
return {
"start": start,
"end": end,
}
def _parse_file_types(file_types_str):
file_types = file_types_str.split(",").strip()
file_types = list(map(lambda x: int(x), file_types))
return file_types
args = parser.parse_args()
if args.username is not None:
config["username"] = args.username
if args.password is not None:
config["password"] = args.password
if args.root is not None:
config["root"] = args.root
if args.resolution is not None:
config["resolution"] = args.resolution
if args.num_thread is not None:
config["num_thread"] = args.num_thread
if args.overwrite:
config["overwrite"] = True
if args.file_path_template is not None:
config["file_path_template"] = args.file_path_template
if args.range is not None:
config["range"] = _parse_range(args.range)
if args.file_types is not None:
config["file_types"] = _parse_file_types(args.file_types)
if args.use_ffmpeg:
config["use_ffmpeg"] = True
return args.url
def login(username, password):
"""登录获取 token"""
pd = hashlib.md5()
pd.update(password.encode("utf-8"))
passwd = pd.hexdigest()
data = {
"username": username,
"passwd": passwd,
"mob-token": "",
}
res = spider.post(
"http://www.icourse163.org/mob/logonByIcourse",
headers=headers,
data=data,
)
result = res.json()
code = result.get("status").get("code")
if code == 0:
return result.get("results").get("mob-token")
elif code == 100:
print("密码错误!")
return None
else:
print("登录失败!")
return None
def get_courseinfo(tid, token):
"""获取完整课程信息"""
data = {
"tid": tid,
"mob-token": token,
}
url = "https://www.icourse163.org/mob/course/courseLearn/v1"
res = spider.post(url, data=data)
return res.json()
def get_summary(url):
"""从课程主页面获取信息"""
url = url.replace("learn/", "course/")
res = spider.get(url).text
term_match = re.search(r'termId : "(\d+)"', res)
if not term_match:
print("无法获取课程信息!")
sys.exit(1)
term_id = term_match.group(1)
names = re.findall(r'name:"(.+)"', res)
course_name = " - ".join(names)
# term_ids = re.findall(r'id : "(\d+)",\ncourse', res)
return term_id, repair_filename(course_name)
def parse_resource(resource, token):
"""解析课件链接、参数"""
if resource[0] == VIDEO:
_, file_path, unit_id, content_id = resource
# get signature
data = {"bizType": 1, "mob-token": token, "bizId": unit_id, "contentType": 1}
while True:
res = spider.post(
"https://www.icourse163.org/mob/j/v1/mobileResourceRpcBean.getResourceToken.rpc",
data=data,
)
if res.json()["results"] is not None:
break
time.sleep(0.5)
signature = res.json()["results"]["videoSignDto"]["signature"]
# get urls
data = {
"enVersion": 1,
"clientType": 2,
"mob-token": token,
"signature": signature,
"videoId": content_id,
}
res = spider.post("https://vod.study.163.com/mob/api/v1/vod/videoByNative", data=data)
videos = res.json()["results"]["videoInfo"]["videos"]
# select quality
resolutions = [3, 2, 1]
resolution = resolutions[CONFIG["resolution"] :] + list(reversed(resolutions[: CONFIG["resolution"]]))
for reso in resolution:
for video in videos:
if video["quality"] == reso:
video_url = video["videoUrl"]
break
else:
continue
break
# download subtitle
srt_info = res.json()["results"]["videoInfo"]["srtCaptions"]
if srt_info:
for srt_item in srt_info:
srt_path = os.path.splitext(file_path)[0] + "_" + srt_item["languageCode"] + ".srt"
srt_url = srt_item["url"]
spider.download_bin(srt_url, srt_path)
return video_url, file_path, None
elif resource[0] == PDF:
_, file_path, unit_id, content_id = resource
api_url = "http://www.icourse163.org/mob/course/learn/v1"
data = {
"t": 3,
"cid": content_id,
"unitId": unit_id,
"mob-token": token,
}
res = spider.post(api_url, data=data)
if res.json()["results"] is None:
print(f"[WARNING] 无法获取课件 {file_path},疑似该学期已关闭")
return None, file_path, None
pdf_url = res.json()["results"]["learnInfo"]["textOrigUrl"]
return pdf_url, file_path, None
elif resource[0] == RICH_TEXT:
_, file_path, json_content = resource
api_url = "http://www.icourse163.org/mob/course/attachment.htm"
data = json_content
return api_url, file_path, data
def get_resource(term_id, token, file_types=[VIDEO, PDF, RICH_TEXT]):
"""获取课件信息"""
resource_list = []
course_info = get_courseinfo(term_id, token)
if course_info["results"] is None:
print(f"[ERROR] 无法获取课件信息!原因:「{course_info['status']['message']}」,请确保以下步骤已经完成:")
print(f"* 修改默认账号密码为自己的账号密码(仅支持爱课程账号)")
print(f"* 已经参加该课程")
print(f"* 该课程处于未关闭状态")
sys.exit(1)
for chapter_num, chapter in enumerate(course_info.get("results").get("termDto").get("chapters")):
for lesson_num, lesson in enumerate(chapter.get("lessons") if chapter.get("lessons") is not None else []):
for unit_num, unit in enumerate(lesson.get("units")):
if unit["contentType"] not in file_types:
continue
courseware_num = (chapter_num + 1, lesson_num + 1, unit_num + 1)
if courseware_num < tuple(CONFIG["range"]["start"]) or courseware_num > tuple(CONFIG["range"]["end"]):
continue
file_path = CONFIG["file_path_template"].format(
base_dir=base_dir,
sep=os.path.sep,
type=COURSEWARE.get(unit["contentType"], "Unknown"),
cnt_1=get_section_num(courseware_num, level=1),
cnt_2=get_section_num(courseware_num, level=2),
cnt_3=get_section_num(courseware_num, level=3),
chapter_name=repair_filename(chapter["name"]),
lesson_name=repair_filename(lesson["name"]),
unit_name=repair_filename(unit["name"]),
)
touch_dir(os.path.dirname(file_path))
if unit["contentType"] == VIDEO:
ext = ".mp4"
file_path += ext
playlist.write_path(file_path)
resource_list.append((VIDEO, file_path, unit["id"], unit["contentId"]))
elif unit["contentType"] == PDF:
file_path += ".pdf"
resource_list.append((PDF, file_path, unit["id"], unit["contentId"]))
elif unit["contentType"] == RICH_TEXT:
if unit.get("jsonContent"):
json_content = eval(unit["jsonContent"])
file_path = CONFIG["file_path_template"].format(
base_dir=base_dir,
sep=os.path.sep,
type="File",
cnt_1=get_section_num(courseware_num, level=1),
cnt_2=get_section_num(courseware_num, level=2),
cnt_3=get_section_num(courseware_num, level=3),
chapter_name=repair_filename(chapter["name"]),
lesson_name=repair_filename(lesson["name"]),
unit_name=repair_filename(os.path.splitext(json_content["fileName"])[0])
+ os.path.splitext(json_content["fileName"])[1],
)
touch_dir(os.path.dirname(file_path))
resource_list.append((RICH_TEXT, file_path, json_content))
return resource_list
def get_section_num(courseware_num, level=3, sep=".", template="{:d}"):
"""根据等级获取课件的标号"""
return sep.join(list((map(lambda x: template.format(x), courseware_num[:level]))))
def merge(merge_list, ffmpeg=None):
"""合并待合并列表"""
for i, merge_file in enumerate(merge_list):
print("merging {}/{}".format(i, len(merge_list)), end="\r")
file_path = merge_file["target"]
if ffmpeg is not None:
ffmpeg.join_videos(merge_file["segments"], file_path)
else:
with open(file_path, "wb") as fw:
for ts_path in merge_file["segments"]:
with open(ts_path, "rb") as fr:
fw.write(fr.read())
for ts_path in merge_file["segments"]:
os.remove(ts_path)
if __name__ == "__main__":
url = handle_args(CONFIG)
root = CONFIG["root"]
num_thread = CONFIG["num_thread"]
# 登录并获取信息
token = login(CONFIG["username"], CONFIG["password"])
match_obj = re.match(r"https?://www.icourse163.org(/spoc)?/(course|learn)/\w+-(\d+)", url)
if match_obj is None:
print("无法解析的链接:{},请检查链接是否错误……".format(url))
sys.exit(1)
term_id, course_name = get_summary(url)
course_id = match_obj.group(3)
print(course_name)
print(course_id)
# 创建必要环境
base_dir = touch_dir(os.path.join(root, course_name))
playlist = Dpl(os.path.join(base_dir, "Playlist.dpl"))
# 获取资源列表
resource_list = get_resource(term_id, token, file_types=CONFIG["file_types"])
# 解析资源
resources = []
merge_list = []
for i, resource in enumerate(resource_list):
print("parse_resource {}/{}".format(i, len(resource_list)), end="\r")
url, file_path, params = parse_resource(resource, token)
if url is None:
continue
# 过滤掉已经下载的资源
if os.path.exists(file_path) and not CONFIG["overwrite"]:
print("[INFO] {} already exists!".format(file_path))
continue
if ".m3u8" in url:
merge_file = {"target": file_path, "segments": []}
id = 0
m3u8_text = spider.get(url).text
for line in m3u8_text.split("\n"):
if line.endswith(".ts"):
ts_url = "/".join(url.split("/")[:-1]) + "/" + line
ts_path = "{}{:03d}.ts".format(file_path.rstrip(".mp4"), id)
resources.append((ts_url, ts_path))
id += 1
merge_file["segments"].append(ts_path)
merge_list.append(merge_file)
else:
if params is not None:
url += "?" + urlencode(params)
resources.append((url, file_path))
# 将资源(片段)分发至线程池,并开始下载
manager = FileManager(num_thread, spider=spider, overwrite=CONFIG["overwrite"])
manager.dispense_resources(resources)
manager.run()
# 启动(主线程)监控器,等待下载完成
manager.monitoring()
# 合并所有 ts 片段
ffmpeg = None
if CONFIG["use_ffmpeg"]:
ffmpeg = FFmpeg()
merge(merge_list, ffmpeg=ffmpeg)
print("\nDone!")