mirror of
https://github.com/wbt5/real-url.git
synced 2025-08-14 07:41:37 +08:00
Compare commits
No commits in common. "3c51cb5591fe314eaf12173249665cd147a37ea6" and "783722d1adc42989ac21b1e5a0647e0251b754e5" have entirely different histories.
3c51cb5591
...
783722d1ad
69
douyu.py
69
douyu.py
@ -1,31 +1,17 @@
|
|||||||
# 获取斗鱼直播间的真实流媒体地址,默认最高画质
|
# 获取斗鱼直播间的真实流媒体地址,默认最高画质。
|
||||||
# 使用 https://github.com/wbt5/real-url/issues/185 中两位大佬@wjxgzz @4bbu6j5885o3gpv6ss8找到的的CDN,在此感谢!
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
import execjs
|
import execjs
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class DouYu:
|
class DouYu:
|
||||||
"""
|
|
||||||
可用来替换返回链接中的主机部分
|
|
||||||
两个阿里的CDN:
|
|
||||||
dyscdnali1.douyucdn.cn
|
|
||||||
dyscdnali3.douyucdn.cn
|
|
||||||
墙外不用带尾巴的akm cdn:
|
|
||||||
hls3-akm.douyucdn.cn
|
|
||||||
hlsa-akm.douyucdn.cn
|
|
||||||
hls1a-akm.douyucdn.cn
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, rid):
|
def __init__(self, rid):
|
||||||
"""
|
# 房间号通常为1~7位纯数字,浏览器地址栏中看到的房间号不一定是真实rid.
|
||||||
房间号通常为1~7位纯数字,浏览器地址栏中看到的房间号不一定是真实rid.
|
|
||||||
Args:
|
|
||||||
rid:
|
|
||||||
"""
|
|
||||||
self.did = '10000000000000000000000000001501'
|
self.did = '10000000000000000000000000001501'
|
||||||
self.t10 = str(int(time.time()))
|
self.t10 = str(int(time.time()))
|
||||||
self.t13 = str(int((time.time() * 1000)))
|
self.t13 = str(int((time.time() * 1000)))
|
||||||
@ -39,6 +25,9 @@ class DouYu:
|
|||||||
else:
|
else:
|
||||||
raise Exception('房间号错误')
|
raise Exception('房间号错误')
|
||||||
|
|
||||||
|
def set_rate(self, rate):
|
||||||
|
self.live_rate = rate
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def md5(data):
|
def md5(data):
|
||||||
return hashlib.md5(data.encode('utf-8')).hexdigest()
|
return hashlib.md5(data.encode('utf-8')).hexdigest()
|
||||||
@ -56,13 +45,10 @@ class DouYu:
|
|||||||
'auth': auth
|
'auth': auth
|
||||||
}
|
}
|
||||||
res = self.s.post(url, headers=headers, data=data).json()
|
res = self.s.post(url, headers=headers, data=data).json()
|
||||||
error = res['error']
|
if 0 != res['error']:
|
||||||
data = res['data']
|
print('error at url:{}\nresponse:{}'.format(url, res))
|
||||||
key = ''
|
return None
|
||||||
if data:
|
return data['rtmp_url'] + '/' + data['rtmp_live']
|
||||||
rtmp_live = data['rtmp_live']
|
|
||||||
key = re.search(r'(\d{1,7}[0-9a-zA-Z]+)_?\d{0,4}(/playlist|.m3u8)', rtmp_live).group(1)
|
|
||||||
return error, key
|
|
||||||
|
|
||||||
def get_js(self):
|
def get_js(self):
|
||||||
result = re.search(r'(function ub98484234.*)\s(var.*)', self.res).group()
|
result = re.search(r'(function ub98484234.*)\s(var.*)', self.res).group()
|
||||||
@ -79,15 +65,14 @@ class DouYu:
|
|||||||
|
|
||||||
js = execjs.compile(func_sign)
|
js = execjs.compile(func_sign)
|
||||||
params = js.call('sign', self.rid, self.did, self.t10)
|
params = js.call('sign', self.rid, self.did, self.t10)
|
||||||
params += '&ver=219032101&rid={}&rate=-1'.format(self.rid)
|
# params += '&ver=219032101&rid={}&rate=-1'.format(self.rid)
|
||||||
|
params += '&ver=219032101&rid={}&rate={}'.format(self.rid, self.live_rate)
|
||||||
|
|
||||||
url = 'https://m.douyu.com/api/room/ratestream'
|
url = 'https://m.douyu.com/api/room/ratestream'
|
||||||
res = self.s.post(url, params=params).text
|
res = self.s.post(url, params=params).json()['data']
|
||||||
key = re.search(r'(\d{1,7}[0-9a-zA-Z]+)_?\d{0,4}(.m3u8|/playlist)', res).group(1)
|
return res['url']
|
||||||
|
|
||||||
return key
|
def get_pc_js(self, cdn='ws-h5'):
|
||||||
|
|
||||||
def get_pc_js(self, cdn='ws-h5', rate=0):
|
|
||||||
"""
|
"""
|
||||||
通过PC网页端的接口获取完整直播源。
|
通过PC网页端的接口获取完整直播源。
|
||||||
:param cdn: 主线路ws-h5、备用线路tct-h5
|
:param cdn: 主线路ws-h5、备用线路tct-h5
|
||||||
@ -110,26 +95,22 @@ class DouYu:
|
|||||||
js = execjs.compile(func_sign)
|
js = execjs.compile(func_sign)
|
||||||
params = js.call('sign', self.rid, self.did, self.t10)
|
params = js.call('sign', self.rid, self.did, self.t10)
|
||||||
|
|
||||||
params += '&cdn={}&rate={}'.format(cdn, rate)
|
params += '&cdn={}&rate={}'.format(cdn, self.live_rate)
|
||||||
url = 'https://www.douyu.com/lapi/live/getH5Play/{}'.format(self.rid)
|
url = 'https://www.douyu.com/lapi/live/getH5Play/{}'.format(self.rid)
|
||||||
res = self.s.post(url, params=params).json()
|
res = self.s.post(url, params=params).json()['data']
|
||||||
|
return res['rtmp_url'] + '/' + res['rtmp_live']
|
||||||
return res
|
|
||||||
|
|
||||||
def get_real_url(self):
|
def get_real_url(self):
|
||||||
error, key = self.get_pre()
|
ret = []
|
||||||
if error == 0:
|
ret.append(self.get_pre())
|
||||||
pass
|
ret.append(self.get_js())
|
||||||
elif error == 102:
|
ret.append(self.get_pc_js())
|
||||||
raise Exception('房间不存在')
|
return ret
|
||||||
elif error == 104:
|
|
||||||
raise Exception('房间未开播')
|
|
||||||
else:
|
|
||||||
key = self.get_js()
|
|
||||||
return "http://dyscdnali1.douyucdn.cn/live/{}.flv?uuid=".format(key)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
r = input('输入斗鱼直播间号:\n')
|
r = input('输入斗鱼直播间号:\n')
|
||||||
s = DouYu(r)
|
s = DouYu(r)
|
||||||
|
r = input('输入画质(1流畅;2高清;3超清;4蓝光4M;0蓝光8M或10M):\n')
|
||||||
|
s.set_rate(r)
|
||||||
print(s.get_real_url())
|
print(s.get_real_url())
|
||||||
|
@ -11,5 +11,5 @@ PyExecJS==1.5.1
|
|||||||
requests==2.24.0
|
requests==2.24.0
|
||||||
six==1.15.0
|
six==1.15.0
|
||||||
typing-extensions==3.7.4.3
|
typing-extensions==3.7.4.3
|
||||||
urllib3==1.26.5
|
urllib3==1.25.10
|
||||||
yarl==1.5.1
|
yarl==1.5.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user