Python獲取優酷真實m3u8播放連結,成品來了
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 = '24679788' # 固定值
'''token 值在 cookie 中 '''
sign = token + '&' + t + '&' + appKey + '&' + data
md = md5()
md.update(sign.encode('UTF-8'))
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": "
"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("),
"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": "
"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(("%s % 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):
url = "
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": "
"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 = '{:.1f}'.format(float(size) / 1048576)
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]) # 外匯跟單gendan5.com 選擇播放列表最後一個影片(經過 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 = 'ffplay -protocol_whitelist "file,http,https,rtp,udp,tcp,tls" -loglevel quiet -i "%s"' % x
subprocess.call(text, shell=True)
def start(self):
while True:
try:
t = str(int(time.time() * 1000))
user_info = self.utid()
userid = user_info["utid"]
url = input("\n\n 請將優酷影片播放連結貼上到這 :\n")
url = self.redirect(url)
page_info = self.page_parser(url)
emb = self.get_emb(page_info["videoId"])
params_data = r'''{"steal_params":"{\"ccode\":\"0502\",\"client_ip\":\"192.168.1.1\",\"utid\":\"%s\",\"client_ts\":%s,\"version\":\"2.1.69\",\"ckey\":\"DIl58SLFxFNndSV1GFNnMQVYkx1PP5tKe1siZu/86PR1u/Wh1Ptd+WOZsHHWxysSfAOhNJpdVWsdVJNsfJ8Sxd8WKVvNfAS8aS8fAOzYARzPyPc3JvtnPHjTdKfESTdnuTW6ZPvk2pNDh4uFzotgdMEFkzQ5wZVXl2Pf1/Y6hLK0OnCNxBj3+nb0v72gZ6b0td+WOZsHHWxysSo/0y9D2K42SaB8Y/+aD2K42SaB8Y/+ahU+WOZsHcrxysooUeND\"}","biz_params":"{\"vid\":\"%s\",\"play_ability\":16782592,\"current_showid\":\"%s\",\"preferClarity\":99,\"extag\":\"EXT-X-PRIVINF\",\"master_m3u8\":1,\"media_type\":\"standard,subtitle\",\"app_ver\":\"2.1.69\",\"h265\":1}","ad_params":"{\"vs\":\"1.0\",\"pver\":\"2.1.69\",\"sver\":\"2.0\",\"site\":1,\"aw\":\"w\",\"fu\":0,\"d\":\"0\",\"bt\":\"pc\",\"os\":\"win\",\"osv\":\"10\",\"dq\":\"auto\",\"atm\":\"\",\"partnerid\":\"null\",\"wintype\":\"interior\",\"isvert\":0,\"vip\":1,\"emb\":\"%s\",\"p\":1,\"rst\":\"mp4\",\"needbf\":2,\"avs\":\"1.0\"}"}'''% (userid, t[:10], page_info["vid"], page_info["current_showid"], emb)
sign = self.youku_sign(t, params_data, user_info["token"])
self.m3u8_url(t, params_data, sign)
except Exception as e:
print('error:',e , " 或可能 cookie 設定錯誤 ")
break
if __name__ == '__main__':
print("="*35 +'>> 歡迎使用優酷影片 m3u8 地址解析工具 <<'+"="*35)
cookie = input("\n 使用前 , 請設定優酷的 cookie:\n")
print("\n 這是一個迴圈:可以不停的解析 ...")
youku = YouKu(cookie)
youku.start()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2850180/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP獲取動態跳轉後的真實連結PHP
- python 爬蟲之獲取標題和連結Python爬蟲
- 易優CMS根據aid獲取文件頁面連結AI
- 獲取客戶端真實IP客戶端
- .net 獲取客戶端真實ip客戶端
- jquery獲取圖片的真實大小jQuery
- JavaScript獲取圖片的真實大小JavaScript
- JS播放m3u8JS
- 優酷播放黑科技 | 自由視角技術體驗優化實踐優化
- Java獲取m3u8視訊時長Java
- PHP 獲取使用者真實的ipPHP
- java 匿名 泛型 獲取真實類名Java泛型
- laravel symfony request獲取真實ip的坑Laravel
- Selenium 獲取複製後的連結
- python四種方式解析網頁獲取頁面中的連結Python網頁
- 【Go】獲取使用者真實的ip地址Go
- 【Go】獲取使用者真實的 ip 地址Go
- 伺服器獲取真實客戶端 IP伺服器客戶端
- [半成品]群暉cups連結印表機
- python如何獲取最優輪廓係數Python
- python執行shell並獲取結果Python
- Python爬蟲-獲得某一連結下的所有超連結Python爬蟲
- js獲取帶#號連結後的引數JS
- 獲取支付連結轉化為二維碼
- vue裡a標籤從後臺獲取連結並實現跳轉Vue
- Python 連結串列實踐Python
- 關於 Laravel 使用了 CDN 獲取真實 IP 記錄Laravel
- Nginx 反向代理後如何獲取真實客戶端 IPNginx客戶端
- Java面試題-如何獲取客戶端真實IPJava面試題客戶端
- 華納雲:怎麼獲取伺服器真實ip伺服器
- Python實現單連結串列Python
- 7個獲取訪問者真實IP的方法,速學!!!
- nginx多級代理下如何獲取客戶端真實IPNginx客戶端
- 在容器服務中獲取客戶端真實源 IP客戶端
- nginx反向代理獲取客戶端的真實IP和域名Nginx客戶端
- ABP vNext 審計日誌獲取真實客戶端IP客戶端
- Flutter 入門與實戰(六十八):MobX 獲取網路資料來渲染酷炫的曲線Flutter
- 成品直播原始碼,JAVA獲取圖片的寬、高和大小原始碼Java