forked from baipzy123/ttt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyk.py
355 lines (334 loc) · 15 KB
/
yk.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
import os
import re, requests, time, json
from hashlib import md5
from urllib.parse import parse_qsl, urlsplit
import base64
from Crypto.Cipher import AES
from tabulate import tabulate
from pywidevine.L3.cdm import deviceconfig
from pywidevine.L3.decrypt.wvdecryptcustom import WvDecrypt
from tools import get_pssh, dealck
requests = requests.Session()
class YouKu:
def __init__(self, cookie):
self.cookie = dealck(cookie)
self.r = "xWrtQpP4Z4RsrRCY"
self.R = "aq1mVooivzaolmJY5NrQ3A=="
self.key = ""
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
}
requests.headers.update(self.headers)
requests.cookies.update(self.cookie)
self.ptoken = self.cookie.get("P_pck_rm")
self.utida = "ZIH81OVlRSMDAOQQiG52i4cO"
def youku_sign(self, t, data, token):
appKey = '24679788' # 固定值
'''token值在cookie中'''
sign = token + '&' + t + '&' + appKey + '&' + data
md = md5()
md.update(sign.encode('UTF-8'))
sign = md.hexdigest()
return sign
def utid(self):
json_cookie = requests.cookies.get_dict()
requests.cookies.clear()
requests.cookies.update(json_cookie)
utid = json_cookie.get("cna")
token = json_cookie.get("_m_h5_tk").split("_")[0]
return {"utid": utid, "token": token}
# 若直接在首页小窗口上复制的视频网址,是重定向的网址。
def redirect(self, url):
headers = {
"referer": "https://www.youku.com/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
}
resp = requests.get(url=url)
return resp.url
def page_parser(self, url):
vid = re.findall(r"id_(.*?)\.html", url)[0]
url = "https://openapi.youku.com/v2/videos/show.json"
params = {
"client_id": "53e6cc67237fc59a",
"package": "com.huawei.hwvplayer.youku",
"ext": "show",
"video_id": vid
}
try:
response = requests.get(url, params=params).json()
showid = response["show"]["id"]
return {"current_showid": showid, "videoId": 0, "vid": vid}
except Exception as e:
print(f"获取showid失败:{e}")
print(f"[red]获取showid失败[/red]")
def get_emb(self, videoId):
emb = base64.b64encode(("%swww.youku.com/" % videoId).encode('utf-8')).decode('utf-8')
return emb
# 这个函数用来获取元素的第一个值
def takeOne(self, elem):
return float(elem[0])
def m3u8_url(self, t, params_data, sign, vid):
url = "https://acs.youku.com/h5/mtop.youku.play.ups.appinfo.get/1.1/"
params = {
"jsv": "2.5.8",
"appKey": "24679788",
"t": t,
"sign": sign,
"api": "mtop.youku.play.ups.appinfo.get",
"v": "1.1",
"timeout": "20000",
"YKPid": "20160317PLF000211",
"YKLoginRequest": "true",
"AntiFlood": "true",
"AntiCreep": "true",
"type": "jsonp",
"dataType": "jsonp",
"callback": "mtopjsonp1",
"data": params_data,
}
resp = requests.get(url=url, params=params)
result = resp.text
# print(result)
data = json.loads(result[12:-1])
# print(data)
ret = data["ret"]
video_lists = []
if ret == ["SUCCESS::调用成功"]:
stream = data["data"]["data"]["stream"]
title = data["data"]["data"]["video"]["title"]
print("解析成功:")
keys = {}
tv_stream = self.get_TV_stream(vid)
stream.extend(tv_stream)
for video in stream:
m3u8_url = video["m3u8_url"]
width = video["width"]
height = video["height"]
size = video.get("size", 0)
size = '{:.1f}'.format(float(size) / 1048576)
drm_type = video["drm_type"]
audio_lang = video["audio_lang"]
if audio_lang == "default":
audio_lang = "guoyu"
if video['drm_type'] == "default":
key = ""
elif audio_lang not in keys.keys():
if drm_type == "cbcs":
license_url = video["stream_ext"]["uri"]
key = self.get_cbcs_key(license_url, m3u8_url)
if key[0]:
key = key[1][0]
else:
encryptR_server = video['encryptR_server']
copyright_key = video['stream_ext']['copyright_key']
key = self.copyrightDRM(self.r, encryptR_server, copyright_key)
keys[audio_lang] = key
else:
key = keys[audio_lang]
video_lists.append(
[title, size + "M", f"{width}x{height}", drm_type, key, video["stream_type"], m3u8_url,
video.get("size", 0)])
video_lists = sorted(video_lists, key=lambda x: x[-1], reverse=True)
tb = tabulate([[*video_lists[i][:6]] for i in range(len(video_lists))],
headers=["标题", "分辨率", "视频大小", "drm_type", "base64key", "stream_type"],
tablefmt="pretty",
showindex=range(1, len(video_lists) + 1))
ch = input(f"{tb}\n请输入要下载的视频序号:")
ch = ch.split(",")
for i in ch:
title, size, resolution, drm_type, key, stream_type, m3u8_url, _ = video_lists[int(i) - 1]
savename = f"{title}_{resolution}_{size}"
savepath = os.path.join(os.getcwd(), "/download/yk")
rm3u8_url = m3u8_url.replace("%", "%%")
if rm3u8_url.startswith("http"):
common_args = f"N_m3u8DL-RE.exe \"{rm3u8_url}\" --tmp-dir ./cache --save-name \"{savename}\" --save-dir \"{savepath}\" --thread-count 16 --download-retry-count 30 --auto-select --check-segments-count"
if drm_type == "default":
cmd = common_args
elif drm_type == "cbcs":
cmd = f"{common_args} --key {key} -M format=mp4"
else:
key = key if ":" not in key else base64.b64encode(bytes.fromhex(key.split(":")[1])).decode()
txt = f'''
#OUT,{savepath}
#DECMETHOD,ECB
#KEY,{key}
{savename},{m3u8_url}
'''
with open("{}.txt".format(title), "a", encoding="gbk") as f:
f.write(txt)
print("下载链接已生成")
continue
else:
uri = re.findall(r'URI="(.*)"', m3u8_url)[0]
m3u8_text = requests.get(uri).text
keyid = re.findall(r'KEYID=0x(.*),IV', m3u8_text)[0].lower()
m3u8_path = "{}.m3u8".format(title)
with open(m3u8_path, "w", encoding="utf-8") as f:
f.write(m3u8_url)
key = "{}:{}".format(keyid, base64.b64decode(key).hex()) if ":" not in key else key
common_args = f"N_m3u8DL-RE.exe \"{m3u8_path}\" --tmp-dir ./cache --save-name \"{title}\" --save-dir \"{savepath}\" --thread-count 16 --download-retry-count 30 --auto-select --check-segments-count"
cmd = f"{common_args} --key {key} -M format=mp4"
with open("{}.bat".format(title), "a", encoding="gbk") as f:
f.write(cmd)
f.write("\n")
print("下载链接已生成")
elif ret == ["FAIL_SYS_ILLEGAL_ACCESS::非法请求"]:
print("请求参数错误")
elif ret == ["FAIL_SYS_TOKEN_EXOIRED::令牌过期"]:
print("Cookie过期")
return 10086
else:
print(ret[0])
return 0
def copyrightDRM(self, r, encryptR_server, copyright_key):
crypto_1 = AES.new(r.encode(), AES.MODE_ECB)
key_2 = crypto_1.decrypt(base64.b64decode(encryptR_server))
crypto_2 = AES.new(key_2, AES.MODE_ECB)
return base64.b64encode(base64.b64decode(crypto_2.decrypt(base64.b64decode(copyright_key)))).decode()
def get_cbcs_key(self, license_url, m3u8_url):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82"
}
m3u8data = requests.get(m3u8_url, headers=headers).text
key_url = re.findall(r"URI=\"(.*?)\"", m3u8data)[0]
response = requests.get(key_url, headers=headers).text
pssh = response.split("data:text/plain;base64,").pop().split('",')[0]
wvdecrypt = WvDecrypt(init_data_b64=pssh, cert_data_b64="", device=deviceconfig.device_android_generic)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.82",
}
dic = dict(parse_qsl(urlsplit(license_url).query))
url = license_url.split("?")[0]
dic["licenseRequest"] = base64.b64encode(wvdecrypt.get_challenge()).decode()
dic["drmType"] = "widevine"
response = requests.post(url, data=dic, headers=headers)
license_b64 = response.json()["data"]
wvdecrypt.update_license(license_b64)
Correct, keyswvdecrypt = wvdecrypt.start_process()
if Correct:
return Correct, keyswvdecrypt
def get_TV_stream(self, vid):
headers = {
"user-agent": "OTTSDK;1.0.8.6;Android;9;2203121C"
}
def getdata():
response = requests.get(url, headers=headers, params=params)
try:
data = response.json()["data"]
title = data['show']["title"]
streams = data["stream"]
streamss.extend(streams)
return title, streams
except Exception as e:
return None, []
url = "https://ups.youku.com/ups/get.json" # light_get.json
params = {
"ckey": "7B19C0AB12633B22E7FE81271162026020570708D6CC189E4924503C49D243A0DE6CD84A766832C2C99898FC5ED31F3709BB3CDD82C96492E721BDD381735026",
"client_ip": "192.168.3.1",
"client_ts": "1697343919",
"utid": self.utida,
"pid": "36281532078091",
# HAIER_PID = "36214723575196"; JIMI_PID = "3b777e6ae3c99e26";SONY_PID = "36281532078091";
"player_type": "dnahard", # system:hls,dnahard: cmfv
"app_ver": "11.4.6.4", # 2121104604,2121100600,11.0.6.0,11.4.6.4
"ccode": "0103010261", # sony :0103010261, jimi:010301025C,haier:0103010275 280
"player_source": "21", # 20 sdr 21hfr 22dolby 23bit10
"encryptR_client": "fTWuKHLOVUoOide+VH/h8w==",
"key_index": "key01",
"vid": vid,
"h265": "1",
"media_type": "standard,sei",
"client_id": "",
"ptoken": "",
"ptoken": self.ptoken,
"drm_type": "7",
"extag": "EXT-X-PRIVINF",
"extag_fields": "STREAMTYPE",
"device_name": "XR-98X90L",
"play_ability": "405929984",
"preferClarity": "23",
"master_m3u8": "0",
"play_ability_v2": "2222222",
"site": "1",
"fu": "1",
"vs": "1.0",
"os": "android",
"osv": "12.1.1",
"bt": "tv",
"aw": "a",
"p": "27",
"mdl": "XR-98X90L",
"device_model": "XR-98X90L",
"": ""
}
streamss = []
player_source = [20, 23, 22, 21]
player_type = ["system", "dnahard"]
for i in player_source:
params["player_source"] = str(i)
for j in player_type:
params["player_type"] = j
getdata()
params["ccode"] = "0103010275"
params["player_source"] = "21"
params["player_type"] = "dnahard"
getdata()
url = "https://ups.youku.com/ups/light_get.json"
params["ccode"] = "0103010280"
params["drm_type"] = 0
getdata()
streamss = sorted(streamss, key=lambda x: x["size"], reverse=True)
return streamss
def get(self, url):
t = str(int(time.time() * 1000))
user_info = self.utid()
userid = user_info["utid"]
page_info = self.page_parser(url)
emb = self.get_emb(page_info["videoId"])
steal_params = {
"ccode": "0502",
"utid": userid,
"version": "9.4.39",
"ckey": "DIl58SLFxFNndSV1GFNnMQVYkx1PP5tKe1siZu/86PR1u/Wh1Ptd+WOZsHHWxysSfAOhNJpdVWsdVJNsfJ8Sxd8WKVvNfAS8aS8fAOzYARzPyPc3JvtnPHjTdKfESTdnuTW6ZPvk2pNDh4uFzotgdMEFkzQ5wZVXl2Pf1/Y6hLK0OnCNxBj3+nb0v72gZ6b0td+WOZsHHWxysSo/0y9D2K42SaB8Y/+aD2K42SaB8Y/+ahU+WOZsHcrxysooUeND",
"client_ip": "192.168.1.1",
"client_ts": 1698373135
}
biz_params = {
"vid": page_info["vid"],
"h265": 1,
"preferClarity": 4,
"media_type": "standard,subtitle",
"app_ver": "9.4.39",
"extag": "EXT-X-PRIVINF",
"play_ability": 16782592,
"master_m3u8": 1,
"drm_type": 19,
"key_index": "web01",
"encryptR_client": self.R,
"skh": 1,
"last_clarity": 5,
"clarity_chg_ts": 1689341442
}
ad_params = {
"vip": 1,
}
params_data = {
"steal_params": json.dumps(steal_params),
"biz_params": json.dumps(biz_params),
"ad_params": json.dumps(ad_params),
}
params_data = json.dumps(params_data)
sign = self.youku_sign(t, params_data, user_info["token"])
return self.m3u8_url(t, params_data, sign, page_info["vid"])
def start(self, url=None):
url = input("请输入视频链接:") if url is None else url
url = self.redirect(url)
for i in range(3):
ret = self.get(url)
if ret:
continue
break
if __name__ == '__main__':
cookie = ''
youku = YouKu(cookie)
youku.start()