-
Notifications
You must be signed in to change notification settings - Fork 12
/
BilibiliLive.py
49 lines (45 loc) · 1.8 KB
/
BilibiliLive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from .BaseLive import BaseLive
class BiliBiliLive(BaseLive):
def __init__(self, room_id):
super().__init__()
self.room_id = room_id
self.site_name = 'BiliBili'
self.site_domain = 'live.bilibili.com'
def get_room_info(self):
data = {}
room_info_url = 'https://api.live.bilibili.com/room/v1/Room/get_info'
user_info_url = 'https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room'
response = self.common_request('GET', room_info_url, {
'room_id': self.room_id
}).json()
if response['msg'] == 'ok':
data['roomname'] = response['data']['title']
data['site_name'] = self.site_name
data['site_domain'] = self.site_domain
data['status'] = response['data']['live_status'] == 1
self.room_id = str(response['data']['room_id']) # 解析完整 room_id
response = self.common_request('GET', user_info_url, {
'roomid': self.room_id
}).json()
data['hostname'] = response['data']['info']['uname']
return data
def get_live_urls(self):
live_urls = []
url = 'https://api.live.bilibili.com/room/v1/Room/playUrl'
stream_info = self.common_request('GET', url, {
'cid': self.room_id,
'otype': 'json',
'quality': 0,
'platform': 'web'
}).json()
best_quality = stream_info['data']['accept_quality'][0][0]
stream_info = self.common_request(
'GET', url, {
'cid': self.room_id,
'otype': 'json',
'quality': best_quality,
'platform': 'web'
}).json()
for durl in stream_info['data']['durl']:
live_urls.append(durl['url'])
return live_urls