1
0
mirror of https://github.com/wbt5/real-url.git synced 2025-06-16 15:59:57 +08:00

🎨 优化代码及注释 (#168)

This commit is contained in:
wbt5 2021-05-15 14:18:47 +08:00
parent 0e647a7bb0
commit f842ae68bc
No known key found for this signature in database
GPG Key ID: 92D5C42E815A2BD6

View File

@ -1,48 +1,75 @@
# 获取爱奇艺直播的真实流媒体地址。
# iqiyi.js是cmd5x加密函数
import execjs
import json
import requests
import re
import time
import urllib.parse
import execjs
import requests
class IQiYi:
"""获取爱奇艺 m3u8 格式直播源
输入房间号通常是数字比如链接 https://gamelive.iqiyi.com/w/74429 中的 74429
注意
爱奇艺有部分直播是来自pps的要打开房间看链接是否有跳转有则用pps.py
爱奇艺直播依赖js环境建议安装node.js
Attributes:
rid: 房间号
"""
def __init__(self, rid):
self.rid = rid
self.s = requests.Session()
def get_real_url(self):
try:
response = requests.get('https://m-gamelive.iqiyi.com/w/' + self.rid).text
# 获取直播间的qipuId
qipuId = re.findall(r'"qipuId":(\d*?),"roomId', response)[0]
callback = 'jsonp_' + str(int((time.time() * 1000))) + '_0000'
params = {
'lp': qipuId,
'src': '01010031010000000000',
'rateVers': 'H5_QIYI',
'qd_v': 1,
'callback': callback
}
"""
里面iqiyi.js是个加盐的md5execjs执行后获取cmd5x的返回值
# ba传参iqiyi.js
ba = '/jp/live?' + urllib.parse.urlencode(params)
with open('iqiyi.js', 'r') as f:
content = f.read()
Returns:
m3u8格式播放地址
Raises:
incorrect rid: 请确实是爱奇艺直播房间号爱奇艺有部分直播是来自pps的要打开房间看链接是否有跳转有则用pps.py
Could not find an available JavaScript runtime: 是否安装了js环境
"""
res = self.s.get('https://m-gamelive.iqiyi.com/w/' + self.rid).text
# 获取直播间的qipuId
try:
qipuid, = re.findall(r'"qipuId":(\d*?),"roomId', res)
except ValueError:
raise Exception('Incorrect rid.')
callback = 'jsonp_' + str(int((time.time() * 1000))) + '_0000'
params = {
'lp': qipuid,
'src': '01010031010000000000',
'rateVers': 'H5_QIYI',
'qd_v': 1,
'callback': callback
}
# ba传参iqiyi.js,返回vf
ba = '/jp/live?' + urllib.parse.urlencode(params)
with open('iqiyi.js', 'r') as f:
content = f.read()
try:
cmd5x = execjs.compile(content)
vf = cmd5x.call('cmd5x', ba)
except RuntimeError:
raise Exception('Could not find an available JavaScript runtime.')
# 请求
response = self.s.get('https://live.video.iqiyi.com' + ba, params={'vf': vf}).text
url_json = json.loads(re.findall(r'try{.*?\((.*)\);}catch\(e\){};', response)[0])
url = (url_json.get('data').get('streams'))[0].get('url')
url = url.replace('hlslive.video.iqiyi.com', 'm3u8live.video.iqiyi.com')
# 请求
response = requests.get('https://live.video.iqiyi.com' + ba, params={'vf': vf}).text
url_json = json.loads(re.findall(r'try{.*?\((.*)\);}catch\(e\){};', response)[0])
real_url = (url_json.get('data').get('streams'))[0].get('url')
real_url = real_url.replace('hlslive.video.iqiyi.com', 'm3u8live.video.iqiyi.com')
except:
raise Exception('直播间不存在或未开播')
return real_url
return url
def get_real_url(rid):