-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilibili_startlive.py
More file actions
110 lines (91 loc) · 3.79 KB
/
Copy pathbilibili_startlive.py
File metadata and controls
110 lines (91 loc) · 3.79 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import obspython, json, requests
room_id = ''
area_id: int = 610
cookie: str = ''
# csrf: str = ''
def startLive(csrf: str, area_id: int, room_id: int|str, cookie: str) -> dict:
resp = requests.post(
"https://api.live.bilibili.com/room/v1/Room/startLive",
data={
"room_id": room_id,
"platform": "pc",
"area_v2": area_id,
"backup_stream": 0,
"csrf_token": csrf,
"csrf": csrf,
},
headers={
"User-Agent": "hahaha",
"Cookie": cookie,
},
)
resp_d = resp.json()
if resp_d['code'] != 0:
raise Exception(f'error when post request to startLive \n {resp.content.decode()}')
print(resp.content.decode())
return resp.json()
def stopLive(csrf: str, room_id: int|str, cookie: str) -> dict:
resp = requests.post(
"https://api.live.bilibili.com/room/v1/Room/stopLive",
data={
"room_id": room_id,
"platform": "pc",
"csrf_token": csrf,
"csrf": csrf,
},
headers={
"User-Agent": "hahaha",
"Cookie": cookie,
},
)
resp_d = resp.json()
if resp_d['code'] != 0:
raise Exception(f'error when post request to startLive \n {resp.content.decode()}')
print(resp.content.decode())
return resp.json()
def set_service(server: str, key: str):
settings: dict = {'server': server, 'key': key}
settings: str = json.dumps(settings)
settings = obspython.obs_data_create_from_json(settings)
service = obspython.obs_service_create('rtmp_custom', 'rtmp', settings, None)
obspython.obs_frontend_set_streaming_service(service)
obspython.obs_service_release(service)
obspython.obs_data_release(settings)
def handle_start(props, prop):
'cookie string must contain "SESSDATA" and "bili_jct"'
global cookie, room_id, area_id
for item in cookie.replace(' ', '').split(';'):
key, value = item.split('=')
if key == 'bili_jct':
csrf = value
start_info = startLive(csrf, area_id, room_id, cookie)
addr: str = start_info['data']['rtmp']['addr']
code: str = start_info['data']['rtmp']['code']
set_service(server=addr, key=code)
obspython.obs_frontend_streaming_start()
def handle_stop(props, prop):
global cookie, room_id, area_id
for item in cookie.replace(' ', '').split(';'):
key, value = item.split('=')
if key == 'bili_jct':
csrf = value
stopLive(csrf, room_id, cookie)
obspython.obs_frontend_streaming_stop()
def script_description():
return 'start bilibili live from web api'
def script_update(settings):
global cookie, room_id, area_id
cookie = obspython.obs_data_get_string(settings, 'cookie')
area_id = obspython.obs_data_get_int(settings, 'area_id')
room_id = obspython.obs_data_get_string(settings, 'room_id')
obspython.obs_data_save_json(settings, script_path() + 'config.json')
def script_properties():
props = obspython.obs_properties_create()
obspython.obs_properties_add_text(props, "room_id", "room id", obspython.OBS_TEXT_DEFAULT)
obspython.obs_properties_add_int(props, "area_id", "area id", 0, 1000, 1)
obspython.obs_properties_add_text(props, "cookie", "cookie", obspython.OBS_TEXT_DEFAULT)
# obspython.obs_properties_add_text(props, "csrf", "csrf", obspython.OBS_TEXT_DEFAULT)
obspython.obs_properties_add_button(props, 'start_live', 'start live && pushing', handle_start)
obspython.obs_properties_add_button(props, 'stop_live', 'stop live && pushing', handle_stop)
# obspython.obs_properties_add_button(props, 'set_service', 'set service', handle_set_service)
return props