forked from wbt5/real-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili.py
64 lines (53 loc) · 1.89 KB
/
bilibili.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 获取哔哩哔哩直播的真实流媒体地址,默认获取直播间提供的最高画质
# qn=150高清
# qn=250超清
# qn=400蓝光
# qn=10000原画
import requests
class BiliBili:
def __init__(self, rid):
self.rid = rid
def get_real_url(self):
# 先获取直播状态和真实房间号
r_url = 'https://api.live.bilibili.com/room/v1/Room/room_init?id={}'.format(self.rid)
with requests.Session() as s:
res = s.get(r_url).json()
code = res['code']
if code == 0:
live_status = res['data']['live_status']
if live_status == 1:
room_id = res['data']['room_id']
def u(pf):
f_url = 'https://api.live.bilibili.com/xlive/web-room/v1/playUrl/playUrl'
params = {
'cid': room_id,
'qn': 10000,
'platform': pf,
'https_url_req': 1,
'ptype': 16
}
resp = s.get(f_url, params=params).json()
try:
durl = resp['data']['durl']
real_url = durl[-1]['url']
return real_url
except KeyError or IndexError:
raise Exception('获取失败')
return {
'flv_url': u('web'),
'hls_url': u('h5')
}
else:
raise Exception('未开播')
else:
raise Exception('房间不存在')
def get_real_url(rid):
try:
bilibili = BiliBili(rid)
return bilibili.get_real_url()
except Exception as e:
print('Exception:', e)
return False
if __name__ == '__main__':
r = input('请输入bilibili直播房间号:\n')
print(get_real_url(r))