import re, requests, time, subprocess, json, base64
from hashlib import md5
class YouKu:
def __init__ (self, cookie):
self.cookie = cookie
def youku_sign (self, t, data, token):
appKey = sign = token + md = md5 ()
md.update (sign.encode ( sign = md.hexdigest ()
return sign
def utid (self):
cna = re.compile ("cna= (.*?);")
_m_h5_tk = re.compile ("_m_h5_tk= (.*?)_.*?;")
token = _m_h5_tk.findall (self.cookie+";")
utid_ = cna.findall (self.cookie+";")
return {"utid": utid_[0], "token": token[0]}
# 若直接在首页小窗口上复制的视频网址,是重定向的网址。
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, headers=headers)
return resp.url
def page_parser (self, url):
headers = {
"authority": "v.youku.com",
"method": "GET",
"path": url.replace ("https://v.youku.com/",""),
"scheme": "https",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "max-age=0",
"cookie": self.cookie,
"referer": "https://www.youku.com/",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"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, headers=headers)
html = resp.content.decode ("utf-8")
# print (html)
videoId = re.compile ("videoId: showid = re.compile ("showid: currentEncodeVid = re.compile ("currentEncodeVid: videoId = videoId.findall (html, re.S | re.M | re.I)
current_showid = showid.findall (html, re.S | re.M | re.I)
vid = currentEncodeVid.findall (html, re.S | re.M | re.I)
return {"current_showid": current_showid[0], "videoId": videoId[0], "vid": vid[0]}
def get_emb (self, videoId):
emb = base64.b64encode ( ("%swww.youku.com/" % videoId).encode ( return emb
# 这个函数用来获取元素的第一个值
def takeOne (self, elem):
return float (elem[0])
def m3u8_url (self, t, params_data, sign):
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,
}
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Cookie": self.cookie,
"Host": "acs.youku.com",
"Referer": "https://v.youku.com/v_show/id_XNTA1MTYwMzU0OA==.html?spm=a2h0c.8166622.PhoneSokuUgc_3.dscreenshot",
"Sec-Fetch-Dest": "script",
"Sec-Fetch-Mode": "no-cors",
"Sec-Fetch-Site": "same-site",
"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, params=params, headers=headers)
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 ("解析成功:")
for video in stream:
m3u8_url = video["m3u8_url"]
width = video["width"]
height = video["height"]
size = video["size"]
size = video_lists.append ([size, width, height, title, m3u8_url])
# print (f">>> {title} 分辨率:{width}x{height} 视频大小:{size}M \tm3u8播放地址:{m3u8_url}")
video_lists.sort (key=self.takeOne)
for video_list in video_lists:
print (f">>> {title} 分辨率:{video_list[1]}x{video_list[2]} 视频大小:{video_list[0]}M \tm3u8播放地址:{video_list[4]}")
self.play (video_lists[-1][4]) # 选择播放列表最后一个视频(经过sort排序后,最后一个即为清晰度最高的一个)
elif ret == ["FAIL_SYS_ILLEGAL_ACCESS::非法请求"]:
print ("请求参数错误")
elif ret == ["FAIL_SYS_TOKEN_EXOIRED::令牌过期"]:
print ("Cookie过期")
else:
print (ret[0])
def play (self, x):
text = subprocess.call (text, shell=True)
def start (self):
t = str (int (time.time () * 1000))
user_info = self.utid ()
userid = user_info["utid"]
url = input ("请将优酷视频播放链接粘贴到这:\n")
url = self.redirect (url)
page_info = self.page_parser (url)
emb = self.get_emb (page_info["videoId"])
params_data = r sign = self.youku_sign (t, params_data, user_info["token"])
self.m3u8_url (t, params_data, sign)
if __name__ == cookie = youku = YouKu (cookie)
youku.start ()