forked from prcwcy/sjtu-canvas-video-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsjtu_real_canvas_video.py
82 lines (68 loc) · 2.2 KB
/
sjtu_real_canvas_video.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
import requests
from bs4 import BeautifulSoup
def get_sub_cookies(course_id, oc_cookies):
data = {
i["name"]: i["value"]
for i in
BeautifulSoup(
requests.get(
f"https://oc.sjtu.edu.cn/courses/{course_id}/external_tools/162",
cookies=oc_cookies
).content, "html.parser"
).find(
"form",
attrs={
"action": "https://courses.sjtu.edu.cn/lti/launch"
}
).children
if i.name == "input"
}
r = requests.post(
"https://courses.sjtu.edu.cn/lti/launch",
data=data,
allow_redirects=False
)
return r.cookies, r.headers["location"].partition("?canvasCourseId=")[-1]
def get_real_canvas_video_single(i, sub_cookies):
return requests.post(
"https://courses.sjtu.edu.cn/lti/vodVideo/getVodVideoInfos",
data={
"playTypeHls": "true",
"id": i["videoId"],
"isAudit": "true"
},
cookies=sub_cookies
).json()["body"]
class RealCourse:
def __init__(self, i, sub_cookies):
self.i = i
self.sub_cookies = sub_cookies
self.flag = False
self.course = None
def get(self):
if not self.flag:
self.flag = True
self.course = get_real_canvas_video_single(
self.i, self.sub_cookies
)
return self.course
def __getitem__(self, key):
return self.get()[key]
def get_real_canvas_videos_using_sub_cookies(sub_cookies, canvasCourseId):
return [
[
RealCourse(i, sub_cookies)
for i in requests.post(
"https://courses.sjtu.edu.cn/lti/vodVideo/findVodVideoList",
data={
"pageIndex": "1",
"pageSize": "1000",
"canvasCourseId": canvasCourseId
},
cookies=sub_cookies
).json()["body"]["list"]
][::-1]
]
def get_real_canvas_videos(course_id, oc_cookies):
sub_cookies, canvasCourseId = get_sub_cookies(course_id, oc_cookies)
return get_real_canvas_videos_using_sub_cookies(sub_cookies, canvasCourseId)