forked from AuYang261/BIT_yanhe_download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
184 lines (151 loc) · 5.46 KB
/
utils.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
# coding: utf-8
import requests
import m3u8dl
import time
from hashlib import md5
import os
# 在延河课堂网站的main.js中4937号的O[N(149, 270, 240, 274)]["k"]()函数的返回值
magic = "1138b69dfef641d9d7ba49137d2d4875"
headers = {
"Origin": "https://www.yanhekt.cn",
"Referer": "https://www.yanhekt.cn/",
"xdomain-client": "web_user",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26",
"Xdomain-Client": "web_user",
"Xclient-Signature": md5((magic + "_v1_undefined").encode()).hexdigest(),
"Xclient-Version": "v1",
"Xclient-Timestamp": str(int(time.time())),
"Authorization": "",
}
def auth_prompt(code=True):
return [
"请先在浏览器登录延河课堂",
"并在延河课堂的地址栏输入 javascript:alert(JSON.parse(localStorage.auth).token)",
'注意粘贴时浏览器会自动去掉"javascript:",需要手动补上',
"或者按F12打开控制台粘贴这段代码",
"然后将弹出的内容粘贴到" + ("这里:" if code else '"身份认证码"栏'),
]
def encryptURL(url):
url_list = url.split("/")
# "c3d47d7b3aa8caf2983b313cb6cd142f"
url_list.insert(-1, md5((magic + "_100").encode()).hexdigest())
return "/".join(url_list)
def getSignature():
timestamp = str(int(time.time()))
signature = md5((magic + "_v1_" + timestamp).encode()).hexdigest()
return timestamp, signature
def getToken():
req = requests.get(
"https://cbiz.yanhekt.cn/v1/auth/video/token?id=0", headers=headers
)
data = req.json()["data"]
if not data:
read_auth()
req = requests.get(
"https://cbiz.yanhekt.cn/v1/auth/video/token?id=0", headers=headers
)
data = req.json()["data"]
if not data:
raise Exception("获取Token失败")
return data["token"]
def add_signature_for_url(url, token, timestamp, signature):
url = (
url
+ "?Xvideo_Token="
+ token
+ "&Xclient_Timestamp="
+ timestamp
+ "&Xclient_Signature="
+ signature
+ "&Xclient_Version=v1&Platform=yhkt_user"
)
return url
def read_auth():
if not os.path.exists("auth.txt"):
return ""
with open("auth.txt", "r") as f:
auth = f.read().strip()
headers["Authorization"] = "Bearer " + auth
return auth
def write_auth(auth):
headers["Authorization"] = "Bearer " + auth
with open("auth.txt", "w") as f:
f.write(auth)
def remove_auth():
headers["Authorization"] = ""
if os.path.exists("auth.txt"):
os.remove("auth.txt")
def test_auth(courseID):
"""
Test if the auth in headers is valid.
Return True if the auth is valid, otherwise False.
"""
res = requests.get(
f"https://cbiz.yanhekt.cn/v2/course/session/list?course_id={courseID}",
headers=headers,
)
return bool(res.json()["data"])
def get_course_info(courseID):
courseID = courseID.strip()
course = requests.get(
f"https://cbiz.yanhekt.cn/v1/course?id={courseID}&with_professor_badges=true",
headers=headers,
)
res = requests.get(
f"https://cbiz.yanhekt.cn/v2/course/session/list?course_id={courseID}",
headers=headers,
)
if course.json()["code"] != "0" and course.json()["code"] != 0:
# print(course.json()["code"])
# print(course.json()["message"])
raise Exception(
f"courseID: {courseID}, {course.json()['message']}。请检查您的课程ID,注意它应该是5位数字,从课程信息界面的链接yanhekt.cn/course/***获取,而不是课程播放界面的链接yanhekt.cn/session/***"
)
# print(course.json()["data"]["name_zh"])
videoList = res.json()["data"]
name = course.json()["data"]["name_zh"].strip()
if not videoList:
raise Exception(f"该课程({name})没有视频信息,请检查课程ID是否正确")
return (
videoList,
name,
(
course.json()["data"]["professors"][0]["name"].strip()
if course.json()["data"]["professors"]
else "未知教师"
),
)
def get_audio_url(video_id):
res = requests.get(
f"https://cbiz.yanhekt.cn/v1/video?id={video_id}",
headers=headers,
)
return res.json()["data"].get("audio", "")
def download_audio(url, path, name):
token = getToken()
url = add_signature_for_url(url, token, *getSignature())
_headers = headers.copy()
_headers["Host"] = "cvideo.yanhekt.cn"
res = requests.get(url, headers=_headers)
while res.status_code != 200:
time.sleep(0.1)
res = requests.get(url, headers=_headers)
with open(f"{path}/{name}.aac", "wb") as f:
f.write(res.content)
def print_help(f: callable):
def wrap():
try:
f()
except Exception as e:
print(e)
print(
"If the problem is still not solved, you can report an issue in https://github.com/AuYang261/BIT_yanhe_download/issues."
)
print(
"Or contact with the author [email protected]. Thanks for your report!"
)
print(
"如果问题仍未解决,您可以在https://github.com/AuYang261/BIT_yanhe_download/issues 中报告问题。"
)
print("或者联系作者[email protected]。感谢您的报告!")
return wrap