-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsjtu_real_canvas_video_v2.py
127 lines (104 loc) · 3.48 KB
/
sjtu_real_canvas_video_v2.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
import requests
from bs4 import BeautifulSoup
def get_sub_cookies_v2(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/7032",
cookies=oc_cookies,
).content, "html.parser"
).find(
"form",
attrs={
"action": "https://v.sjtu.edu.cn/jy-application-canvas-sjtu/oidc/login_initiations"
}
).children
if i.name == "input"
}
r = requests.post(
"https://v.sjtu.edu.cn/jy-application-canvas-sjtu/oidc/login_initiations",
data=data,
cookies=oc_cookies,
allow_redirects=True
)
data_2 = {
i["name"]: i["value"]
for i in
BeautifulSoup(
r.content, "html.parser"
).find(
"form",
attrs={
"action": "https://v.sjtu.edu.cn/jy-application-canvas-sjtu/lti3/lti3Auth/ivs"
}
).children
if i.name == "input"
}
r_2 = requests.post(
"https://v.sjtu.edu.cn/jy-application-canvas-sjtu/lti3/lti3Auth/ivs",
data=data_2,
cookies=oc_cookies,
allow_redirects=False
)
loc = r_2.headers.get("location")
params_arr = [i.partition('=') for i in loc.partition('?')[-1].split('&')]
params_dict = {i: j for i, _, j in params_arr}
tokenId = params_dict["tokenId"]
canvasCourseId = params_dict["courId"]
r_3 = requests.get(
"https://v.sjtu.edu.cn/jy-application-canvas-sjtu/lti3/getAccessTokenByTokenId",
params={"tokenId": tokenId, },
cookies=oc_cookies,
)
access_token = r_3.json()["data"]["token"]
v_header = {
"token": access_token
}
return oc_cookies, canvasCourseId, v_header
def get_real_canvas_video_single_v2(i, sub_cookies, v_header):
return requests.post(
"https://v.sjtu.edu.cn/jy-application-canvas-sjtu/directOnDemandPlay/getVodVideoInfos",
data={
"playTypeHls": "true",
"id": i["videoId"],
"isAudit": "true"
},
cookies=sub_cookies,
headers=v_header,
).json()["data"]
class RealCourseV2:
def __init__(self, i, sub_cookies, v_header):
self.i = i
self.sub_cookies = sub_cookies
self.v_header = v_header
self.flag = False
self.course = None
def get(self):
if not self.flag:
self.flag = True
self.course = get_real_canvas_video_single_v2(
self.i, self.sub_cookies, self.v_header
)
return self.course
def __getitem__(self, key):
return self.get()[key]
def get_real_canvas_videos_using_sub_cookies_v2(sub_cookies, canvasCourseId, v_header):
return [
[
RealCourseV2(i, sub_cookies, v_header)
for i in requests.post(
"https://v.sjtu.edu.cn/jy-application-canvas-sjtu/directOnDemandPlay/findVodVideoList",
json={
"canvasCourseId": canvasCourseId
},
cookies=sub_cookies,
headers=v_header,
).json()["data"]["records"]
]
]
def get_real_canvas_videos_v2(course_id, oc_cookies):
sub_cookies, canvasCourseId, v_header = get_sub_cookies_v2(
course_id, oc_cookies)
return get_real_canvas_videos_using_sub_cookies_v2(sub_cookies, canvasCourseId, v_header)