From 9a2d70f701f84096f4893a504f11dc2311fd8577 Mon Sep 17 00:00:00 2001 From: wbt5 Date: Sat, 13 Nov 2021 17:06:26 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8DB=E7=AB=99=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=20(#271,#254,#257,#250,#222)=20-=E6=9C=89=E4=BA=9B?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=97=A0=E6=B3=95=E5=9C=A8PotPlayer=E6=92=AD?= =?UTF-8?q?=E6=94=BE=EF=BC=8C=E5=BB=BA=E8=AE=AE=E6=8D=A2=E4=B8=AA=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=99=A8=E5=B0=9D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bilibili.py | 75 ++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/bilibili.py b/bilibili.py index 2b670a2..de83a02 100644 --- a/bilibili.py +++ b/bilibili.py @@ -9,17 +9,23 @@ import requests class BiliBili: def __init__(self, rid): + """ + 有些地址无法在PotPlayer播放,建议换个播放器试试 + Args: + rid: + """ rid = rid self.header = { - 'User-Agent': 'Mozilla/5.0 (iPod; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/87.0.4280.163 Mobile/15E148 Safari/604.1', + 'User-Agent': 'Mozilla/5.0 (iPod; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, ' + 'like Gecko) CriOS/87.0.4280.163 Mobile/15E148 Safari/604.1', } # 先获取直播状态和真实房间号 r_url = 'https://api.live.bilibili.com/room/v1/Room/room_init' param = { 'id': rid } - with requests.Session() as s: - res = s.get(r_url, headers=self.header, params=param).json() + with requests.Session() as self.s: + res = self.s.get(r_url, headers=self.header, params=param).json() if res['msg'] == '直播间不存在': raise Exception(f'bilibili {rid} {res["msg"]}') live_status = res['data']['live_status'] @@ -27,46 +33,43 @@ class BiliBili: raise Exception(f'bilibili {rid} 未开播') self.real_room_id = res['data']['room_id'] - def get_real_url(self, current_qn: int = 10000) -> list: + def get_real_url(self, current_qn: int = 10000) -> dict: url = 'https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo' param = { - 'device': 'pc', - 'platform': 'h5', - 'scale': 0, - 'build': '10000', + 'room_id': self.real_room_id, 'protocol': '0,1', 'format': '0,1,2', 'codec': '0,1', - 'room_id': self.real_room_id, - 'qn': current_qn + 'qn': current_qn, + 'platform': 'h5', + 'ptype': 8, } - with requests.Session() as session: - res = session.get(url, headers=self.header, params=param) - res = res.json() + res = self.s.get(url, headers=self.header, params=param).json() + stream_info = res['data']['playurl_info']['playurl']['stream'] + qn_max = 0 + + for data in stream_info: + accept_qn = data['format'][0]['codec'][0]['accept_qn'] + for qn in accept_qn: + qn_max = qn if qn > qn_max else qn_max + if qn_max != current_qn: + param['qn'] = qn_max + res = self.s.get(url, headers=self.header, params=param).json() stream_info = res['data']['playurl_info']['playurl']['stream'] - qn_max = 0 - for data in stream_info: - accept_qn = data['format'][0]['codec'][0]['accept_qn'] - for qn in accept_qn: - qn_max = qn if qn > qn_max else qn_max - if qn_max != current_qn: - param['qn'] = qn_max - res = session.get(url, headers=self.header, params=param) - res = res.json() - stream_info = res['data']['playurl_info']['playurl']['stream'] - stream_url_list = [] - for data in stream_info: - format_name = data['format'][0]['format_name'] - if format_name == 'flv': - base_url = data['format'][0]['codec'][0]['base_url'] - url_info = data['format'][0]['codec'][0]['url_info'] - for info in url_info: - host = info['host'] - extra = info['extra'] - # print(host + base_url + extra) - stream_url_list.append(host + base_url + extra) - break - return stream_url_list + + stream_urls = {} + # flv流无法播放,暂修改成获取hls格式的流, + for data in stream_info: + format_name = data['format'][0]['format_name'] + if format_name == 'ts': + base_url = data['format'][-1]['codec'][0]['base_url'] + url_info = data['format'][-1]['codec'][0]['url_info'] + for i, info in enumerate(url_info): + host = info['host'] + extra = info['extra'] + stream_urls[f'线路{i + 1}'] = f'{host}{base_url}{extra}' + break + return stream_urls def get_real_url(rid):